700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > SpringCloud学习系列之三-----配置中心(Config)文件修改后 客户端动态刷新(Refresh)

SpringCloud学习系列之三-----配置中心(Config)文件修改后 客户端动态刷新(Refresh)

时间:2021-11-23 02:15:51

相关推荐

SpringCloud学习系列之三-----配置中心(Config)文件修改后 客户端动态刷新(Refresh)

注意啦,注意啦,注意啦

根据大神的教导来实现的:想看大神的去–>我是大神,神龙见首不见尾?

前言

在上一篇里了解简单的Spring Cloud Config的使用。不过还是不完整,各位看官请多多指教。本篇主要介绍基于Spring Cloud 分布式配置中心(Spring Cloud Config)的配置刷新消息总线(RabbitMQ/和Kafka)的使用

Spring Cloud Config Refresh(配置刷新)

业务需求:

在上一篇中主要讲了Spring Cloud Config的本地与GIT使用的用法,但是当配置文件修改后重新提交后,客户端获取的仍然是修改前的配置信息,需要重启客户端才能获取最新的配置信息。

因此我们需要客户端能够动态进行更新,幸好springcloud官方已经给出方案,所以我们只需要使用就行了。

开发准备

开发环境:

idea (其他也OK)JDK:1.8 (底线了,在低就用不了了)SpringBoot:2.1.7.RELEASE(这个你随意,只要在2X版本以上就行,1x版本和2x区别挺大的。)

服务项目

1.cloud-config-bus-eureka :注册中心

2.cloud-config-bus-server: 配置中心

3.cloud-config-bus-client :客户端

cloud-config-bus-eureka :注册中心

pom.xml配置和代码如下:

<dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>

application.properties配置和代码如下:

spring.application.name=cloud-config-bus-eurekaserver.port=1000eureka.client.fetch-registry=falseeureka.client.register-with-eureka=falseeureka.client.service-url.defaultZone=http://localhost:1000/eureka/

** 启动类代码如下:**

@EnableEurekaServer@SpringBootApplicationpublic class CloudConfigbusEurekaApplication {public static void main(String[] args) {SpringApplication.run(CloudConfigbusEurekaApplication .class, args);System.out.println("注册中心启动。。。。");}}

cloud-config-bus-server: 配置中心

pom.xml配置和代码如下:

<dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>

application.properties配置和代码如下:

spring.application.name=cloud-config-bus-serverserver.port=1100eureka.client.service-url.defaultZone=http://localhost:1000/eureka/# 读取git的路径#git仓库的地址spring.cloud.config.server.git.uri = /xpf0321/cloud-config-test/# git仓库地址下的相对地址 多个用逗号","分割spring.cloud.config.server.git.search-paths = /springcloud-config/config-repo# git仓库的账号spring.cloud.config.server.git.username =用户名是你的# git仓库的密码spring.cloud.config.server.git.password =密码是你的

上面的git配置应该是你自己的git用的是数据,如果你看到的话,需要将gitl配置信息改为你git里的信息

** 启动类代码如下:**

@EnableDiscoveryClient@EnableConfigServer@SpringBootApplicationpublic class CloudConfigBusServerApplication {public static void main(String[] args) {SpringApplication.run(CloudConfigBusServerApplication.class, args);System.out.println("配置中心启动。。。");}}

如果你细心的话发现这两个其实和上一篇的是一样的,没有啥变化

cloud-config-bus-client :客户端

我要将上一篇的cloud-config-client进行改造,改为cloud-config-bus-client

算了,还是一步步来把。

pom.xml配置和代码如下:

<dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><!--spring-boot-starter-actuator表示对该程序进行监控,可以通过http接口得到该程序的各种信息--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>

application.properties配置和代码如下:

spring.application.name=cloud-config-bus-clientserver.port=1200management.endpoints.web.exposure.include=refresh

management.endpoints.web.exposure.include=refresh //该配置表示暴露刷新的地址为refresh。

注:如果是SpringBoot1.x的版本,那么配置改成management.security.enabled=false即可。

bootstrap.properties配置和代码如下:

spring.cloud.config.name=configtestspring.cloud.config.profile=devspring.cloud.config.label=masterspring.cloud.config.fail-fast=falsespring.cloud.config.discovery.enabled=truespring.cloud.config.discovery.service-id=cloud-config-bus-servereureka.client.service-url.defaultZone=http://localhost:1000/eureka/

配置说明:

是啥呢?想知道就到上一篇看去。

controller代码如下:

@RefreshScope@RestControllerpublic class BusClientController {/*** name*/@Value("${mi}")private String name;@RequestMapping("hello")public String hello(@RequestParam("info")String info){return name+","+info;}}

Controller增加一个@RefreshScope注解,该注解表示在接到SpringCloud配置中心配置刷新的时候,自动将新的配置更新到该类对应的字段中。

** 启动类代码如下:**

@EnableDiscoveryClient@SpringBootApplicationpublic class CloudConfigBusClientApplication {public static void main(String[] args) {SpringApplication.run(CloudConfigBusClientApplication.class, args);System.out.println("客户端启动。。。");}}

注意:端口不要用6000,端口不要用6000,端口不要用6000

测试

依次启动

1.cloud-config-bus-eureka 端口:1000

2.cloud-config-bus-server 端口:1100

3.cloud-config-bus-client 端口:1200

在浏览器输入:

http://localhost:1200/hello?info=ooo

页面返回显示:

hello beibi ooo

可以正常得到服务端configtest-pro.properties的配置信息。

然后在把configtest-dev.properties的配置更改为:

mi=hello beibi!!Oh. No NO

再次在浏览器输入:

http://localhost:1200/hello?info=ooo

页面返回显示:

hello beibi ooo

可以发现配置没有实时刷新,官方文档上说,需要客户端通过Post请求触发各自的/refresh.

我用PostMan工具模仿post请求刷新,然后在查看信息。

["config.client.version","mi"]

说明完成mi的配置刷新了,再次在浏览器输入:

http://localhost:1200/hello?info=ooo

页面返回显示:

hello beibi!!Oh. No NO,ooo

在来几张结果图

PostMan

浏览器

注意啦,注意啦,注意啦

根据大神博客的教导来实现的:想看大神的去–>我是大神,神龙见首不见尾?

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。