Spring Cloud Bus 实现配置实时更新 Java Spring Cloud Bus 实现配置实时更新详解

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Spring Cloud Bus 实现配置实时更新 Java Spring Cloud Bus 实现配置实时更新详解

lbl2018   2021-09-15 我要评论
想了解Java Spring Cloud Bus 实现配置实时更新详解的相关内容吗,lbl2018在本文为您仔细讲解Spring Cloud Bus 实现配置实时更新的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Java,Spring,Cloud,Spring,Cloud,Bus,实时更新,下面大家一起来学习吧。

背景

使用Spring Cloud Config Server,启动Service时会从配置中心取配置文件,并注入到应用中,如果在Service运行过程中想更新配置,需要使用Spring Cloud Bus配合实现实时更新。

实现原理

需要借助RabbitMQ等消息中间件来实现服务间的通讯

这里写图片描述

ConfigServer改造

目标:使ConfigServer暴露bus-refresh接口,通过bus通知服务更新配置

1. pom.xml增加以下依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

2. 配置文件中配置暴露接口

management.endpoints.web.exposure.include=bus-refresh

Service改造

1. pom.xml增加以下依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

2. 通过@RefreshScope声明配置刷新时需要重新注入

@RefreshScope
@Controller
public class LblController {

测试

1. 修改git仓库上的配置文件

Service配置没有更新

2. 调用http://localhost:8081/actuator/bus-refresh(POST)

Service配置更新

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注的更多内容!

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们