其他分享
首页 > 其他分享> > SpringCloud Config: Fetching config from server at

SpringCloud Config: Fetching config from server at

作者:互联网

出现场景

使用 SpringCloud Config 配置中心时,客户端每间隔5分钟会输出如下日志:

2020-11-28 18:19:59,541 INFO http-nio-8051-exec-9 (ConfigServicePropertySourceLocator.java:82)- Fetching config from server at: http://10.10.19.150:8011/
2020-11-28 18:19:59,896 INFO http-nio-8051-exec-9 (ConfigServicePropertySourceLocator.java:96)- Located environment: name=vau-trade-schedule, profiles=[prod], label=master, version=dae019860c62b88cfebe14d0a1c530591daa6f71, state=null

情况分析

通过官网查找到如下介绍:

Health Indicator

The Config Client supplies a Spring Boot Health Indicator that attempts to load configuration from the Config Server. The health indicator can be disabled by setting health.config.enabled=false. The response is also cached for performance reasons. The default cache time to live is 5 minutes. To change that value, set the health.config.time-to-live property (in milliseconds).

解释说明:由于 Config 客户端提供一个Spring引导运行状况指示器,默认5分钟进行一次状态检查。同时给出禁用的配置方式:health.config.enabled=false

查看源码:ConfigClientHealthProperties.java 文件中 timeToLive 属性确实默认是5分钟。

@ConfigurationProperties("health.config")
public class ConfigClientHealthProperties {
	/**
	 * Flag to indicate that the config server health indicator should be installed.
	 */
	boolean enabled;

	/**
	 * Time to live for cached result, in milliseconds. Default 300000 (5 min).
	 */
	private long timeToLive = 60 * 5 * 1000;

	public boolean isEnabled() {
		return this.enabled;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public long getTimeToLive() {
		return timeToLive;
	}

	public void setTimeToLive(long timeToLive) {
		this.timeToLive = timeToLive;
	}

解决:在 bootstrap.yml 或 配置中心里的配置文件里配置即可。

health:
  config:
    enabled: false

我的项目配置方式:在项目里有 bootstrap.yml,这里面配置了应用名、使用环境、配置中心要拉取配置的文件名、环境、分支等信息;再就是 Eureka 服务地址及相关信息。由于我们的项目是多个品牌、多个环境、一套代码的方式,很多属性都是动态配置,部分代码如下:

spring:
  application:
    name: schedule
  profiles:
    active: dev
  cloud:
    config:
      name: ${proj}-trade-${spring.application.name}
      profile: ${spring.profiles.active}
      label: master
      discovery:
        enabled: true
        service-id: config

health:
  config:
    enabled: false

---
spring:
  profiles: dev

eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:8001/eureka/
  instance:
    instance-id: ${instanceId:${spring.application.name}-01}
    prefer-ip-address: true

---
spring:
  profiles: test

......

---
spring:
  profiles: prod

......

在 gitLab 的配置文件中具体再配置项目中需要的 MySQL、Redis、RabbitMQ 等等信息即可,那些基础的使用就不在此赘述。

标签:Fetching,SpringCloud,enabled,profiles,health,spring,timeToLive,config
来源: https://blog.csdn.net/u013062791/article/details/110289038