K8s部署apollo配置中心configServices内网IP问题

mtain 2022年07月07日 53次浏览

说明

K8s部署apollo配置中,springboot启动后,从容器IP上获取配置,因为是K8s内部IP,所以外部无法连接

springboot集成apollo配置,如下

app:
  id: 100001
apollo:
  cacheDir: /tmp/apollo/
  cluster: default
  meta: http://192.168.1.165:30002
  autoUpdateInjectedSpringProperties: true
  bootstrap:
    enabled: true
    namespaces: application
    eagerLoad:
      enabled: false

springboot启动后获取配置的日志,可以看到从10的地址上获取配置,实际外部访问地址是192,所以网络不通,需要自定义configService

[2022-07-07 16:42:23,637 DEBUG]  Loading config from http://10.42.2.105:8080/configs/femix/default/application?ip=192.168.9.104  				{@ com.ctrip.framework.apollo.internals.RemoteConfigRepository.loadApolloConfig(210)} ReqTrace: 
[2022-07-07 16:42:24,642 WARN ]  Load config failed, will retry in 1 SECONDS. appId: femix, cluster: default, namespaces: application  				{@ com.ctrip.framework.apollo.internals.RemoteConfigRepository.loadApolloConfig(196)} ReqTrace: 
[2022-07-07 16:42:25,648 DEBUG]  Loading config from http://10.42.3.252:8080/configs/femix/default/application?ip=192.168.9.104  				{@ com.ctrip.framework.apollo.internals.RemoteConfigRepository.loadApolloConfig(210)} ReqTrace: 
[2022-07-07 16:42:26,650 WARN ]  Load config failed, will retry in 1 SECONDS. appId: femix, cluster: default, namespaces: application  				{@ com.ctrip.framework.apollo.internals.RemoteConfigRepository.loadApolloConfig(196)} ReqTrace: 
[2022-07-07 16:42:27,656 DEBUG]  Loading config from http://10.42.2.105:8080/configs/femix/default/application?ip=192.168.9.104  				{@ com.ctrip.framework.apollo.internals.RemoteConfigRepository.loadApolloConfig(210)} ReqTrace: 

源码分析

com.ctrip.framework.apollo.internals.ConfigServiceLocator

    // 1. Get from System Property
    String configServices = System.getProperty("apollo.configService");
    if (Strings.isNullOrEmpty(configServices)) {
      // 2. Get from OS environment variable
      configServices = System.getenv("APOLLO_CONFIGSERVICE");
    }
    if (Strings.isNullOrEmpty(configServices)) {
      // 3. Get from server.properties
      configServices = Foundation.server().getProperty("apollo.configService", null);
    }

可以看到,先通过以上三种方式获取自定义的configService地址,如果未设置就从eureka中获取地址(在k8s中获取的是内部地址)

解决方法

设置启动时,系统环境变量
APOLLO_CONFIGSERVICE=http://192.168.1.165:30002

当然一共三种方式,任选其一