spring cloud config入门,动态更新配置,spring cloud bus amqp刷新配置
发布日期:2021-05-07 13:21:55 浏览次数:23 分类:精选文章

本文共 4808 字,大约阅读时间需要 16 分钟。

前面介绍了spring cloud config 基于本地配置文件,git,mysql的配置开发示例,但是存在一个问题,如果我们要更新配置,按照之前的处理,则需要修改完配置之后重启配置,这样不适合生产环境,为此,spring cloud提供了基于spring cloud bus的机制来实现配置更改后的动态通知和刷新。下面进行这块的入门尝试, 以spring cloud config git为例说明

建立config-server-git,大体与之前相同,但是增加了spring cloud bus相关依赖:

spring-cloud-test-001
com.leo.test
1.0.0-snapshot
4.0.0
config-consumer-001
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-config
org.springframework.cloud
spring-cloud-starter-bus-amqp
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-maven-plugin

其中主要改变在于引入了spring cloud bus amqp依赖,以及spring cloud actuator暴露对外刷新接口。

编写配置application.yml

spring:  rabbitmq:    host: xxxx    port: 5672  profiles:    active: remote  cloud:    config:      server:        git:          uri: https://github.com/hanxueming126/open-config-source          search-paths: config          username:xxx          password: xxxx          default-label: develop          force-pull: true #本地与git有冲突时,强制拉取覆盖本地  application:    name: config-server-gitserver:  port: 9060management:  endpoints:    web:      exposure:        include: "*"  #需要开放才能通过接口请求刷新#debug: true

建立启动类:

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.config.server.EnableConfigServer;@SpringBootApplication@EnableConfigServerpublic class ConfigServerGitApplication {    // 需要 post请求 /actuator/bus-refresh才能刷新, 返回 204响应    public static void main(String[] args) {        SpringApplication.run(ConfigServerGitApplication.class);    }}

这样spring cloud cofnig git 服务端搭建完毕

搭建spring cloud config客户端,pom依赖如下:

spring-cloud-test-001
com.leo.test
1.0.0-snapshot
4.0.0
config-consumer-001
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-config
org.springframework.cloud
spring-cloud-starter-bus-amqp
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-maven-plugin

模块配置文件:bootstrap.xml

spring:  application:    name: config-consumer-001  cloud:    config:      uri: http://localhost:9060      fail-fast: true  profiles:    active: dev  rabbitmq:    host: 10.201.83.207    port: 5672management:  endpoints:    web:      exposure:        include: '*'#debug: true

编写启动类:

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class ConfigConsumerApplication {    public static void main(String[] args) {        SpringApplication.run(ConfigConsumerApplication.class);    }}

编写controller:

import org.springframework.beans.factory.annotation.Value;import org.springframework.cloud.context.config.annotation.RefreshScope;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/config/consumer")@RefreshScopepublic class ConsumerController {    @Value("${hello}")    private String hello;    @RequestMapping("/test")    public String testConfig(){        return String.format("hello is %s ",hello);    }}

开始时,git上config/下配置如下:

在这里插入图片描述
请求端会以 applicationName-profile来获取对应配置。
启动config-server和confi-consumer
访问: http://localhost:9080/config/consumer/test
在这里插入图片描述

更改git上配置如下:

在这里插入图片描述

我这里没有配置webhook,所以手动刷新config-server来刷新配置信息:

在这里插入图片描述

post请求config-server端: http://localhost:9060//actuator/bus-refresh

再次重新访问:http://localhost:9080/config/consumer/test
显示如下:
在这里插入图片描述
配置刷新成功,更新配置成功。

如果测试需要rabbitmq,安装可参考

上一篇:spring cloud consul入门,服务注册和发现,配置中心使用入门示例
下一篇:spring cloud config入门,spring cloud config mysql数据库配置配置中心

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2025年03月31日 03时02分47秒