首页 > 解决方案 > 为什么 Spring Boot 应用程序不断从配置服务中获取配置?

问题描述

我正在使用 Spring Boot 和 Spring Cloud Config 构建微服务。从日志文件中,我可以看到我的一个应用程序每五分钟向配置服务发送一个请求以进行配置,而其他应用程序则不执行相同的行为。

06:04:28.586 [serviceId:xxx-service/traceId:545d584f10da3123/spanId:545d584f10da3123/parentId:] INFO  o.s.c.c.c.ConfigServicePropertySourceLocator.getRemoteEnvironment - Fetching config from server at : http://config-service
06:04:30.135 [serviceId:xxx-service/traceId:545d584f10da3123/spanId:545d584f10da3123/parentId:] INFO  o.s.c.c.c.ConfigServicePropertySourceLocator.log - Located environment: name=xxxservice, profiles=[dev], label=master, version=00ed4aebcd466c88b4a7df61296652944642cdc8, state=null
06:09:30.367 [serviceId:xxx-service/traceId:519fdde3a2d8ce5a/spanId:519fdde3a2d8ce5a/parentId:] INFO  o.s.c.c.c.ConfigServicePropertySourceLocator.getRemoteEnvironment - Fetching config from server at : http://config-service
06:09:31.928 [serviceId:xxx-service/traceId:519fdde3a2d8ce5a/spanId:519fdde3a2d8ce5a/parentId:] INFO  o.s.c.c.c.ConfigServicePropertySourceLocator.log - Located environment: name=xxxservice, profiles=[dev], label=master, version=00ed4aebcd466c88b4a7df61296652944642cdc8, state=null
06:14:32.035 [serviceId:xxx-service/traceId:e32c45c3a5c3fcca/spanId:e32c45c3a5c3fcca/parentId:] INFO  o.s.c.c.c.ConfigServicePropertySourceLocator.getRemoteEnvironment - Fetching config from server at : http://config-service
06:14:33.611 [serviceId:xxx-service/traceId:e32c45c3a5c3fcca/spanId:e32c45c3a5c3fcca/parentId:] INFO  o.s.c.c.c.ConfigServicePropertySourceLocator.log - Located environment: name=xxxservice, profiles=[dev], label=master, version=00ed4aebcd466c88b4a7df61296652944642cdc8, state=null
06:19:32.033 [serviceId:xxx-service/traceId:16fc982b0638d4a6/spanId:16fc982b0638d4a6/parentId:] INFO  o.s.c.c.c.ConfigServicePropertySourceLocator.getRemoteEnvironment - Fetching config from server at : http://config-service
06:19:33.495 [serviceId:xxx-service/traceId:16fc982b0638d4a6/spanId:16fc982b0638d4a6/parentId:] INFO  o.s.c.c.c.ConfigServicePropertySourceLocator.log - Located environment: name=xxxservice, profiles=[dev], label=master, version=00ed4aebcd466c88b4a7df61296652944642cdc8, state=null
06:24:32.036 [serviceId:xxx-service/traceId:f4eae364e2624d58/spanId:f4eae364e2624d58/parentId:] INFO  o.s.c.c.c.ConfigServicePropertySourceLocator.getRemoteEnvironment - Fetching config from server at : http://config-service
06:24:33.640 [serviceId:xxx-service/traceId:f4eae364e2624d58/spanId:f4eae364e2624d58/parentId:] INFO  o.s.c.c.c.ConfigServicePropertySourceLocator.log - Located environment: name=xxxservice, profiles=[dev], label=master, version=00ed4aebcd466c88b4a7df61296652944642cdc8, state=null

我试图理解这种行为。

1bootstrap.yml该服务是否有别于其他服务,导致独特的行为?

不,所有服务bootstrap.yml似乎都是相同的结构,没有不同的配置键:

spring:
  profiles:
    active: dev
  cloud:
    config:
      name: xxxservice
      label: master
      uri: http://config-service

2 该服务是否试图动态且定期地刷新其配置?

不,为了动态刷新一个spring boot应用的配置,我们需要:

所以不应该有周期性的自动刷新行为。

问题仍未解决,希望有人直接解释这种情况或指出我的陈述中的任何错误。

标签: spring-bootspring-cloud-config

解决方案


我不知道为什么您在其他服务中没有看到这种情况,但我相信这种行为是由于客户端想要定期检查服务器的运行状况。来自 Spring Cloud Config Client 文档:

7.8.1 健康指标

Config Client 提供了一个 Spring Boot Health Indicator,它尝试从 Config Server 加载配置。可以通过设置禁用运行状况指示器health.config.enabled=false。出于性能原因,响应也会被缓存。默认缓存时间为 5 分钟。要更改该值,请设置 health.config.time-to-live 属性(以毫秒为单位)。

所以只需 set health.config.enabled=false,这种行为应该停止。


推荐阅读