Spring Cloud Config2.x 版本使用webhooks无法刷新client配置的解决方案

本文详细介绍了在Spring Boot和Spring Cloud环境下,使用Spring Cloud Bus时遇到的配置刷新问题。特别关注了通过webhooks访问/monitor无法刷新配置的情况,并提供了具体的解决方案,包括如何根据serviceID的匹配规则设置参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言:本次开发环境为SpringBoot 2.1.4.RELEASE、SpringCloud Greenwich.SR1、SpringCloudConfig 2.1.1.RELEASE

发现问题

使用config手动通过访问/actuator/bus-refresh可以正常刷新,但是通过配置webhooks访问/monitor无法刷新配置。

解决问题

官方文档排查

https://cloud.spring.io/spring-cloud-bus/spring-cloud-bus.html bus的文档中对spring.cloud.bus.id有如下描述:
在这里插入图片描述
应用有一个ServiceID,默认的值是app:index:id的组装。
规则是:

 app :如果vcap.application.name存在,使用vcap.application.name,否则使用spring.application.name(默认值为application)
 index :配置值的情况下优先使用vcap.application.instance_index,否则依次使用spring.application.index、local.server.port、server.port(默认值0)
 id: 如果vcap.application.instance_id存在,使用vcap.application.instance_id,否则给一个随机值
代码排查

设置客户端的打印日志级别

logging:
  level:
    org.springframework.cloud.bus: debug

控制台会打印出org.springframework.cloud.bus.DefaultBusPathMatcher中匹配规则的日志(如果客户端不刷新,一般这里的日志打印出的匹配规则和待匹配字符串是不一致的),webhooks端的过来匹配规则由三部分数据组成,使用“:”拼接,得到的结果如下:

spring.application.name:spring.cloud.config.profile:**

如果我们serviceID不进行设置,当前服务那么会使用默认配置(默认配置代码体现在:org.springframework.cloud.bus.BusEnvironmentPostProcessor#getDefaultServiceId),如下:

private String getDefaultServiceId(ConfigurableEnvironment environment) {
		return "${vcap.application.name:${spring.application.name:application}}:${vcap.application.instance_index:${spring.application.index:${local.server.port:${server.port:0}}}}:${vcap.application.instance_id:${random.value}}";
	}

对应官方文档,以及我们的配置文件,我们可以依据serviceID的匹配规则来设置对应的参数去匹配webhooks。

如果发现app:index:id中的index不一致, 举例yml配置:

vcap:
  application:
    instance_index: ${spring.cloud.config.profile}

或者直接修改bus.id的配置,如下:

spring:
  application:
    name: client
  cloud:
    config:
      discovery:
        service-id: CONFIG
        enabled: true
      profile: dev 
    bus:
      id: ${spring.application.name}:${spring.cloud.config.profile}:${random.value}
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值