美文网首页diboot
Spring Cloud Config2.x 版本使用webho

Spring Cloud Config2.x 版本使用webho

作者: 一个鸡蛋壳儿 | 来源:发表于2019-06-14 13:50 被阅读0次

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

    发现问题

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

    解决问题

    官方文档排查

    https://cloud.spring.io/sprin... bus的文档中对spring.cloud.bus.id有如下描述:

    image.png

    应用有一个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}
    

    Diboot 轻代码开发框架

    相关文章

      网友评论

        本文标题:Spring Cloud Config2.x 版本使用webho

        本文链接:https://www.haomeiwen.com/subject/vqsufctx.html