关于spring cloud config自动刷新配置

使用spring cloud config统一配置中心,对于配置文件的修改,可以通过/refresh端点进行手动刷新,但是随着微服务节点的增多,实现自动刷新是有必要的。–摘自某博客语录

使用spring cloud bus实现配置的自动刷新。
spring cloud bus使用轻量级的消息代理,例如RabbitMQ,Kafka,redis连接分布式系统中的其他节点,通过广播的方式,将配置信息的修改传播给其他节点。

查看spring cloud官方文档,可以看到如下描述:

Spring Cloud Bus works by adding Spring Boot autconfiguration if it detects itself on the classpath. 
To enable the bus, add spring-cloud-starter-bus-amqp or spring-cloud-starter-bus-kafka to your 
dependency management. Spring Cloud takes care of the rest. Make sure the broker (RabbitMQ or 
Kafka) is available and configured. When running on localhost, you need not do anything. If you run 
remotely, use Spring Cloud Connectors or Spring Boot conventions to define the broker credentials, 
as shown in the following example for Rabbit:

application.yml. 

spring:
  rabbitmq:
    host: mybroker.com
    port: 5672
    username: user
    password: secret
The bus currently supports sending messages to all nodes listening or all nodes for a particular 
service (as defined by Eureka). The /bus/* actuator namespace has some HTTP endpoints. 
Currently, two are implemented. The first, /bus/env, sends key/value pairs to update each node’s 
Spring Environment. The second, /bus/refresh, reloads each application’s configuration, as though 
they had all been pinged on their /refresh endpoint.

server端:
引入依赖:spring-cloud-starter-bus-amqp或spring-cloud-starter-bus-kafka,远程情况下,配置消息代理的相关参数(本地运行无需配置)。
启动类添加@EnableConfigServer,开启支持。
client端:
引入依赖:spring-cloud-starter-bus-amqp或spring-cloud-starter-bus-kafka,远程情况下,配置消息代理的相关参数(本地运行无需配置)。
启动类添加@EnableConfigServer,开启支持。
在需要同步配置信息的类上加上注解@RefreshScope

服务端application.yml

server:
  port: 18851
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/xchuang-li/config-infomation.git
          ###有时,配置文件可能在仓库的子目录下,此配置可以搜索对应的子目录,如foo,bar*
          search-paths: /**
          username: 
          password: 
          ###默认在第一次请求时克隆仓库,此配置可在启动时就克隆仓库
          clone-on-start: true
        ###全局默认仓库分支,也可以写在对应匹配模式下
        default-label: master
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8000/eureka/
management:
  endpoints:
    web:
      exposure:
        include: "*"

客户端application.yml

server:
  port: 18842
spring:
  application:
    name: global-foo
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

客户端bootstrap.yml

spring:
  cloud:
    config:
#      uri: http://localhost:18841/
      profile: dev
      label: master
      ###如果客户端无法连接上服务端,则客户端将以异常停止
      fail-fast: true
      ###利用服务发现组件,将服务端注册到eureka上,这样,就可以保证服务端的高可用
      ###此处,开启客户端的服务发现支持,并指定服务端的serviceId
      discovery:
        enabled: true
        service-id: config-server
###注意,因为bootstrap.yml的加载优先于application.yml,所以服务注册要写在bootstrap.yml中
###否则,加载bootstrap.yml时,因为尚未注册到eureka server,也就无法找到服务端的serviceId,就会抛出错误,无法找到serviceId:config-server
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8000/eureka/

测试:
启动config server和config client,eureka server
修改github仓库中的配置文件

43. Bus Endpoints
Spring Cloud Bus provides two endpoints, /actuator/bus-refresh and /actuator/bus-env that 
correspond to individual actuator endpoints in Spring Cloud Commons, /actuator/refresh and 
/actuator/env respectively.	

从官方文档看出,访问的端点是 /actuator/bus-refresh。

发送post请求:http://localhost:18851/actuator/bus-refresh刷新配置
发送get请求:http://localhost:18842/hello 可以发现,github仓库修改配置文件后,只刷新了server 端,但是client也更新了相关的配置信息。
通过webhooks实现自动刷新
在这里插入图片描述
新增webhook,配置url和Content type
问题1
起初,配置url为:http://localhost:18851/actuator/bus-refresh
然而,发现一直报错:cound not connet to server
经过查找网上资源,说是需要做内网穿透。教程参考:https://natapp.cn/article/natapp_newbie

配置完内网穿透后,url地址改写为上图所示,此时,不再报 cound not connet to server错误,却出现了另外一个错误:404 {“timestamp”:“2019-07-03T11:53:55.180+0000”,“status”:400,“error”:“Bad Request”,“message”:“JSON parse error: Cannot deserialize instance of java.lang.String out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of java.lang.String out of START_ARRAY token\n at [Source: (PushbackInputStream); line: 1, column: 300] (through reference chain: java.util.LinkedHashMap[“commits”])”,“path”:"/actuator/bus-refresh"}

查找资料,说是GitHub在进行post请求的同时默认会在body加上这么一串载荷(payload),导致spring boot因为无法正常反序列化这串载荷而报了400错误。

在https://blog.csdn.net/m0_37556444/article/details/82812816这篇博客里,提供了一种解决方式,然而感觉略显麻烦,有人提出通过/monitor端点也可以解决该问题,于是,再次查找文档

官方文档

9. Push Notifications and Spring Cloud Bus
Many source code repository providers (such as Github, Gitlab, Gitea, Gitee, Gogs, or Bitbucket) 
notify you of changes in a repository through a webhook. You can configure the webhook through the 
provider’s user interface as a URL and a set of events in which you are interested. For instance, 
Github uses a POST to the webhook with a JSON body containing a list of commits and a header (X-
Github-Event) set to push. If you add a dependency on the spring-cloud-config-monitor library and 
activate the Spring Cloud Bus in your Config Server, then a /monitor endpoint is enabled.

When the webhook is activated, the Config Server sends a RefreshRemoteApplicationEvent 
targeted at the applications it thinks might have changed. The change detection can be strategized. 
However, by default, it looks for changes in files that match the application name (for example, 
foo.properties is targeted at the foo application, while application.properties is targeted at all 
applications). The strategy to use when you want to override the behavior is 
PropertyPathNotificationExtractor, which accepts the request headers and body as parameters and 
returns a list of file paths that changed.

The default configuration works out of the box with Github, Gitlab, Gitea, Gitee, Gogs or Bitbucket. In 
addition to the JSON notifications from Github, Gitlab, Gitee, or Bitbucket, you can trigger a change 
notification by POSTing to /monitor with form-encoded body parameters in the pattern of path=
{name}. Doing so broadcasts to applications matching the {name} pattern (which can contain 
wildcards).

引入依赖spring-cloud-config-monitor,然后postman发送请求http://localhost:18851/monitor
在这里插入图片描述
通过message看出,缺少body元素’path’
这一点,通过上面官方文档的解释也能看出。

修改:
在这里插入图片描述
这次,可以看到发送成功。

再次修改github仓库的配置文件,然后发送该请求,可以看到client端同步了修改。

重新配置webhooks

在这里插入图片描述
然后修改gihub仓库配置文件,这次,没有手动发送post刷新请求,可以看到webhooks上正确发送了该请求,
请求客户端http://localhost:18842/hello,可以看到客户端获取的配置信息确实自动刷新了。

此时,已经实现了通过webhooks实现自动刷新配置信息。path=* 含义尚未揣摩透,期待有老师能做解答。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值