查看原文
其他

Spring Cloud Config Server迁移节点或容器化带来的问题

翟永超 程序猿DD 2019-07-13

如果您跟我一样,目前正在使用Spring Cloud Config做为配置中心的话,本篇将来要描述的问题,强烈推荐了解和关注!因为这个问题目前存在于所有的版本中,还没有修复,需要注意避开或设法解决。

问题现象

为了说明下面的内容,我们可以先尝试重现一下问题:在一个测试环境中,将Spring Cloud Config的配置中心迁移到另外一个节点上,即配置中心的IP地址发生了变化。在完成迁移之后,我们会发现该环境下各个微服务应用的健康状态会变得时好时坏,并且在日志中会出现类似下面的报错:

  1. 2018-05-13 17:01:28,569 WARN  [http-nio-9920-exec-1] org.springframework.cloud.config.client.ConfigServerHealthIndicator - Health check failed

  2. java.lang.IllegalStateException: Could not locate PropertySource and the fail fast property is set, failing

  3.    at org.springframework.cloud.config.client.ConfigServicePropertySourceLocator.locate(ConfigServicePropertySourceLocator.java:132)

  4.    at org.springframework.cloud.config.client.ConfigServicePropertySourceLocator$$FastClassBySpringCGLIB$$fa44b2a.invoke(<generated>)

  5.    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)

  6.    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738)

  7.    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)

  8.    at org.springframework.retry.interceptor.RetryOperationsInterceptor$1.doWithRetry(RetryOperationsInterceptor.java:91)

  9.    at org.springframework.retry.support.RetryTemplate.doExecute(RetryTemplate.java:287)

  10.    at org.springframework.retry.support.RetryTemplate.execute(RetryTemplate.java:164)

  11.    at org.springframework.retry.interceptor.RetryOperationsInterceptor.invoke(RetryOperationsInterceptor.java:118)

  12.    at org.springframework.retry.annotation.AnnotationAwareRetryOperationsInterceptor.invoke(AnnotationAwareRetryOperationsInterceptor.java:153)

  13.    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)

  14.    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673)

  15.    at org.springframework.cloud.config.client.ConfigServicePropertySourceLocator$$EnhancerBySpringCGLIB$$3a43a1f4.locate(<generated>)

  16.    at org.springframework.cloud.config.client.ConfigServerHealthIndicator.getPropertySource(ConfigServerHealthIndicator.java:54)

  17.    at org.springframework.cloud.config.client.ConfigServerHealthIndicator.doHealthCheck(ConfigServerHealthIndicator.java:35)

  18.    at org.springframework.boot.actuate.health.AbstractHealthIndicator.health(AbstractHealthIndicator.java:43)

  19.    at org.springframework.boot.actuate.health.CompositeHealthIndicator.health(CompositeHealthIndicator.java:68)

  20.    at org.springframework.boot.actuate.endpoint.HealthEndpoint.invoke(HealthEndpoint.java:85)

  21.    at org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint.getCurrentHealth(HealthMvcEndpoint.java:177)

  22.    at org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint.getHealth(HealthMvcEndpoint.java:166)

  23.    at org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint.invoke(HealthMvcEndpoint.java:143)

  24.    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

  25.    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

  26.    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

  27.    at java.lang.reflect.Method.invoke(Method.java:498)

  28.    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)

  29.    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)

  30.    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)

  31.    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)

  32.    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)

  33.    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)

  34.    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)

  35.    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)

  36.    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)

  37.    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)

  38.    at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)

  39.    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)

  40.    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)

  41.    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)

  42.    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

  43.    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

  44.    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)

  45.    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

  46.    at org.springframework.boot.web.filter.ApplicationContextHeaderFilter.doFilterInternal(ApplicationContextHeaderFilter.java:55)

  47.    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

  48.    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)

  49.    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

  50.    at com.yonghui.feign.filter.RequestOriginFilter.doFilter(RequestOriginFilter.java:41)

  51.    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)

  52.    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

  53.    at org.springframework.boot.actuate.trace.WebRequestTraceFilter.doFilterInternal(WebRequestTraceFilter.java:110)

  54.    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

  55.    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)

  56.    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

  57.    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)

  58.    at com.yonghui.rpc.feature.web.boot.RpcHolder4BootFilter.doFilterInternal(RpcHolder4BootFilter.java:29)

  59.    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

  60.    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)

  61.    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

  62.    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)

  63.    at com.yonghui.rpc.feature.web.boot.FeatureSupport4BootFilter.doFilterInternal(FeatureSupport4BootFilter.java:24)

  64.    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

  65.    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)

  66.    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

  67.    at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:108)

  68.    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

  69.    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)

  70.    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

  71.    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)

  72.    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

  73.    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)

  74.    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

  75.    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)

  76.    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

  77.    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)

  78.    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

  79.    at org.springframework.boot.actuate.autoconfigure.MetricsFilter.doFilterInternal(MetricsFilter.java:106)

  80.    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

  81.    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)

  82.    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

  83.    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)

  84.    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)

  85.    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)

  86.    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)

  87.    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)

  88.    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)

  89.    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)

  90.    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)

  91.    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)

  92.    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)

  93.    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)

  94.    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)

  95.    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)

  96.    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)

  97.    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)

  98.    at java.lang.Thread.run(Thread.java:748)

  99. Caused by: org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://192.168.5.103:9010/config-server/test": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)

  100.    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:674)

  101.    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:621)

  102.    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:539)

  103.    at org.springframework.cloud.config.client.ConfigServicePropertySourceLocator.getRemoteEnvironment(ConfigServicePropertySourceLocator.java:172)

  104.    at org.springframework.cloud.config.client.ConfigServicePropertySourceLocator.locate(ConfigServicePropertySourceLocator.java:93)

  105.    ... 95 more

  106. Caused by: java.net.ConnectException: Connection refused (Connection refused)

  107.    at java.net.PlainSocketImpl.socketConnect(Native Method)

  108.    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)

  109.    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)

  110.    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)

  111.    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)

  112.    at java.net.Socket.connect(Socket.java:589)

  113.    at java.net.Socket.connect(Socket.java:538)

  114.    at sun.net.NetworkClient.doConnect(NetworkClient.java:180)

  115.    at sun.net.www.http.HttpClient.openServer(HttpClient.java:463)

  116.    at sun.net.www.http.HttpClient.openServer(HttpClient.java:558)

  117.    at sun.net.www.http.HttpClient.<init>(HttpClient.java:242)

  118.    at sun.net.www.http.HttpClient.New(HttpClient.java:339)

  119.    at sun.net.www.http.HttpClient.New(HttpClient.java:357)

  120.    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1220)

  121.    at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1156)

  122.    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1050)

  123.    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:984)

  124.    at org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:78)

  125.    at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)

  126.    at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53)

  127.    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:660)

  128.    ... 99 more

可以看到类似上面的健康检查失败错误,但是并不是一直这样,这个环境下的微服务会出现时好时坏的情况,那么为什么会出现这种现象呢?

原因分析

从错误日志中我们可以发现一个非常关键的信息: I/O error on GET requestfor"http://192.168.5.103:9010/config-server/test"

报错说明了微服务检查配置中心获取配置的连接是否畅通的时候出现了连接不上的情况,但是这个链接信息其实并不是当前配置中心的地址,而是我们迁移之前的配置中心的地址。

从健康检查的实现源码 ConfigServerHealthIndicator中为入口去分析和调试,我们可以解答上面现象的两个疑问:

  1.    @Override

  2.    protected void doHealthCheck(Builder builder) throws Exception {

  3.        PropertySource<?> propertySource = getPropertySource();

  4.        builder.up();

  5.        if (propertySource instanceof CompositePropertySource) {

  6.            List<String> sources = new ArrayList<>();

  7.            for (PropertySource<?> ps : ((CompositePropertySource) propertySource).getPropertySources()) {

  8.                sources.add(ps.getName());

  9.            }

  10.            builder.withDetail("propertySources", sources);

  11.        } else if (propertySource!=null) {

  12.            builder.withDetail("propertySources", propertySource.toString());

  13.        } else {

  14.            builder.unknown().withDetail("error", "no property sources located");

  15.        }

  16.    }

  17.    private PropertySource<?> getPropertySource() {

  18.        long accessTime = System.currentTimeMillis();

  19.        if (isCacheStale(accessTime)) {

  20.            this.lastAccess = accessTime;

  21.            this.cached = locator.locate(this.environment);

  22.        }

  23.        return this.cached;

  24.    }

为什么会健康检查访问的还是老的配置中心地址?

真正导致健康检查失败的语句是 getPropertySource中的 this.cached=locator.locate(this.environment);而这里的具体实现在 org.springframework.cloud.config.client.ConfigServicePropertySourceLocator类中,具体实现如下:

  1. @Override

  2. @Retryable(interceptor = "configServerRetryInterceptor")

  3. public org.springframework.core.env.PropertySource<?> locate(

  4.            org.springframework.core.env.Environment environment) {

  5.    ConfigClientProperties properties = this.defaultProperties.override(environment);

  6.    CompositePropertySource composite = new CompositePropertySource("configService");

  7.    RestTemplate restTemplate = this.restTemplate == null ? getSecureRestTemplate(properties)

  8.                : this.restTemplate;

  9.    Exception error = null;

  10.    String errorBody = null;

  11.    logger.info("Fetching config from server at: " + properties.getRawUri());

  12.        ...

  13. }

可以看到,真正去访问的地址是直接从 properties.getRawUri()获取的,它已经是一个固化的值,而不是通过服务发现机制来动态获取的。这就导致了当我们把配置中心做了迁移,或者直接部署在容器中出现重启的时候,IP发生变化,而所有的微服务还以为访问的是原来的配置中心地址,就会出现健康检查失败的问题,导致服务不可用的现象。

为什么健康检查时好时坏?

上面的问题会导致健康检查失败,但是这个服务并不是一直都不好,而是间断性的出现不健康。这主要还是健康检查时间中的机制导致,这里可以具体看 ConfigServerHealthIndicator的 getPropertySource函数,该方法执行的时候中间并不是每一次检查都会去访问配置中心(执行 locator.locate(this.environment)方法),因此客户端的健康检查并不会每次都健康检查失败,从而出现了微服务健康检查时好时坏的情况。

如何解决

该问题目前也在官方的issue中被提出,还处于open状态

具体可见:https://github.com/spring-cloud/spring-cloud-config/issues/514

由于该问题目前并没有得到解决,虽然提交了一个PR,但是还有待完善以及提供一些测试,原本想完全处理好之后再写一篇文章,但是发现最近不少问过类似问题,所以索性先写一篇文章,提醒一下用户以及给一些相关的建议。

当前版本上不太容易通过扩展的方式去解决的这个问题,所以大家可以变通的去避免这个问题:

  • 部署在虚拟机上而不是容器上,避免IP的变动

  • 可以考虑关闭微服务队config客户端的健康检查,增加参数 management.health.config.enabled=false;但是这个操作有一个弊端,虽然迁移不会引发服务时好时坏的问题了,但是如果有动态配置刷新需求,如果迁移了配置中心,刷新配置操作也是会失败的。


热文推荐

蚂蚁金服蓝绿发布实践

Java开发神器Lombok的使用与原理

使用ShardingJdbc应对大数据量的案例(一)

微服务化小团队集群的组织和管理

SpringBoot中使用ShardingJdbc切分数据库表

Spring Boot 2.0选择HikariCP作为默认数据库连接池的五大理由

主流Java数据库连接池比较及前瞻

Hystrix的应用案例:多短信供应商的自动切换与恢复

长按指纹

一键关注

深入交流、更多福利

扫码加入我的知识星球



点击 “阅读原文” 看看本号其他精彩内容

    您可能也对以下帖子感兴趣

    文章有问题?点此查看未经处理的缓存