iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Java Spring Cloud Bus 实现配置实时更新详解
  • 708
分享到

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

2024-04-02 19:04:59 708人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

目录背景实现原理ConfigServer改造1. pom.xml增加以下依赖2. 配置文件中配置暴露接口Service改造1. pom.xml增加以下依赖2. 通过@RefreshS

背景

使用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配置更新

总结

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

--结束END--

本文标题: Java Spring Cloud Bus 实现配置实时更新详解

本文链接: https://www.lsjlt.com/news/135729.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作