apollo client 获取meta url 为服务器内网地

2021-05-07  本文已影响0人  devLiao

正常安装apollo在服务器之后,会在本地去注册的时候 ,明明meta填的时候是公网地址,结果日志打出来是服务器的内网地址

根据日志定位到 ConfigServiceLocator类


  private synchronized void updateConfigServices() {
    String url = assembleMetaServiceUrl();

    HttpRequest request = new HttpRequest(url);
    int maxRetries = 2;
    Throwable exception = null;

    for (int i = 0; i < maxRetries; i++) {
      Transaction transaction = Tracer.newTransaction("Apollo.MetaService", "getConfigService");
      transaction.addData("Url", url);
      try {
        HttpResponse<List<ServiceDTO>> response = m_httpUtil.doGet(request, m_responseType);
        transaction.setStatus(Transaction.SUCCESS);
        List<ServiceDTO> services = response.getBody();
        if (services == null || services.isEmpty()) {
          logConfigService("Empty response!");
          continue;
        }
        setConfigServices(services);
        return;
      } catch (Throwable ex) {
        Tracer.logEvent("ApolloConfigException", ExceptionUtil.getDetailMessage(ex));
        transaction.setStatus(ex);
        exception = ex;
      } finally {
        transaction.complete();
      }

      try {
        m_configUtil.getOnErrorRetryIntervalTimeUnit().sleep(m_configUtil.getOnErrorRetryInterval());
      } catch (InterruptedException ex) {
        //ignore
      }
    }

    throw new ApolloConfigException(
        String.format("Get config services failed from %s", url), exception);
  }

根据本地断点调试得到这个httprequest的请求接口的地址,这个接口返回的meta url 就是为meta的内网地址

/services/config?appi=&ip=

根据这个接口定位到apollo 服务端的源码

ServiceController.getConfigService 这个接口

继续往下走

  public List<ServiceDTO> getServiceInstances(String serviceId) {
    Application application = eurekaClient.getApplication(serviceId);
    if (application == null || CollectionUtils.isEmpty(application.getInstances())) {
      Tracer.logEvent("Apollo.Discovery.NotFound", serviceId);
      return Collections.emptyList();
    }
    return application.getInstances().stream().map(instanceInfoToServiceDTOFunc)
        .collect(Collectors.toList());
  }

看到这里基本上真相大白了,这里只要把eureka的client 注册地址改成公网的就可以了

于是去script 里面的start.sh 找到

export JAVA_OPTS="$JAVA_OPTS -Dserver.port=$SERVER_PORT  -Dlogging.file.name=$LOG_DIR/$SERVICE_NAME.log -XX:HeapDumpPath=$LOG_DIR/HeapDumpOnOutOfMemoryError/"

加上这个参数就可以了

-Deureka.instance.ip-address=meta的公网地址
上一篇下一篇

猜你喜欢

热点阅读