refresh

引入依赖

<!-- 内省 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

打开端点

### 端点控制
management:
  endpoints:
    web:
      exposure:
        # 开启指定端点、所有端点
        include: '*'
  endpoint:
    health:
      # 总是表示详细信息的显示
      show-details: always

  进阶上一节的代码,无需更改,如果配置中心的配置文件更改之后,只需访问 refresh 接口即可:

Config Server refresh 端点

bus-refresh

Config Server refresh 端点

  用户更新配置信息时,检查到 Git Hook 变化,触发 Hook 配置地址的调用,Config Server 接收到请求并发布消息,Bus 将消息发送到 Config Client,当 Config Client 接收到消息后会重新发送请求加载配置信息,大体流程就是这样。这里使用的事 RabbitMQ 作为消息中间件,自行安装。

启动 RabbitMQ

# 后台启动 RabbitMQ
$ rabbitmq-server -detached
Warning: PID file not written; -detached was passed.

  查看启动结果,Web 管理页面需要启动插件。

Config Server refresh 端点

Config Server

引入依赖

<!-- Spring Cloud Bus Amqp -->
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

配置

spring:
  # RabbitMQ 配置
  rabbitmq:
    # 地址
    host: localhost
    # 端口
    port: 5672
    # 用户名
    username: admin
    # 密码
    password: nS8KiyIu0Y7LGbvE

  启动 Config Server 之后查看 RabbitMQ 管理界面,可以看到新增的队列:

Config Server refresh 端点

Config Client

引入依赖

<!-- Spring Cloud Bus Amqp -->
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

配置

spring:
  # RabbitMQ 配置
  rabbitmq:
    # 地址
    host: localhost
    # 端口
    port: 5672
    # 用户名
    username: admin
    # 密码
    password: nS8KiyIu0Y7LGbvE

  启动 Config Client 之后查看 RabbitMQ 管理界面,可以看到新增的队列:

Config Server refresh 端点

  配置完毕,当配置中心的配置更爱之后,只需访问 Config Server 的 /bus-refresh 接口即可刷新配置:

Config Server refresh 端点

源码:https://github.com/SlowSlicing/demo-spring-cloud-finchley/tree/ConfigServer入门

相关文章:

  • 2021-09-18
  • 2021-08-03
  • 2021-10-18
  • 2022-03-09
  • 2022-12-23
  • 2021-10-15
  • 2021-07-25
  • 2021-07-31
猜你喜欢
  • 2022-01-16
  • 2022-12-23
  • 2022-12-23
  • 2021-03-30
  • 2021-08-11
  • 2021-06-21
  • 2021-06-25
相关资源
相似解决方案