SpringCloudConfig + CloudBus + WebHooks +RibbitMQ,实现配置集中管理和自动刷新
一、概述
springCloudConfig同一配置服务,其实就是单独创建一个服务,专门用来管理其它所有服务的配置文件,其它微服务通过与这个配置服务建立连接,拉取配置到各自的服务环境中,springCloudConfig与git仓库关联,相当于先将所有微服务的配置文件放在git远程仓库上,springCloudConfig服务从git拉取配置,其它微服务从springCloudConfig服务上拉取各自的配置。从而就实现了多个微服务配置文件的集中管理。
二、实现步骤和注意点:
1.创建服务端(用来从git仓库拉取配置文件)
microservice-config-server:
(1)、pom:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | <?xml version= "1.0" encoding= "UTF-8" ?> <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion> 4.0 . 0 </modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version> 1.5 . 4 .RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.base</groupId> <artifactId>microservice-config-server</artifactId> <version> 0.0 . 1 -SNAPSHOT</version> <name>microservice-config-server</name> <description>Demo project for Spring Boot</description> <properties> <java.version> 1.8 </java.version> <spring-cloud.version>Dalston.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <!-- 实现通过端点refresh手动刷新 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-monitor</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope> import </scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> |
(2)、启动类:
1 2 3 4 5 6 7 8 9 10 | @SpringBootApplication @EnableConfigServer @EnableDiscoveryClient public class MicroserviceConfigServerApplication { public static void main(String[] args) { SpringApplication.run(MicroserviceConfigServerApplication. class , args); } } |
主要是加上@EnableConfigServer,表示当前服务是个配置服务。
(3)、yml配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | server: port: 9009 #本服务的端口 spring: application: name: microservice-config-server #本服务名称 cloud: config: server: git: #git地址 uri: https: //github.com/47Gamer-github/spring-cloud-config.git username: 47Gamer-github password: ???? # clone-on-start: true rabbitmq: addresses: 127.0 . 0.1 : 5672 #mq服务器地址 username: 47Gamer #账号 password: ???????? #密码 management: #开放所有端口 endpoints: web: exposure: include: "*" security: #关闭安全验证 enabled: false eureka: #注册到eureka设置 client: serviceUrl: defaultZone: http: //localhost:1001/eureka/ instance: instance-id: ${spring.cloud.client.ipAddress}:${server.port} #设置服务在注册中心的实例id prefer-ip-address: true |
这个yml文件主要就是:
- 配置了git地址(因为此配置服务需要从git仓库上拉取所有的配置文件)
- 配置了rabbitMq仓库地址,因为此配置服务需要建立一个消息队列,用于自动刷新配置到各个与之关联的微服务中去(后面会说到的springCloudBus)
- 配置eureka注册中心地址,用于将此服务注册上去。
2.创建客户端(用来从配置服务端仓库拉取配置文件)
(1)、pom:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | <?xml version= "1.0" encoding= "UTF-8" ?> <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion> 4.0 . 0 </modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version> 1.5 . 4 .RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.base</groupId> <artifactId>microservice-config-client</artifactId> <version> 0.0 . 1 -SNAPSHOT</version> <name>microservice-config-client</name> <description>Demo project for Spring Boot</description> <properties> <java.version> 1.8 </java.version> <spring-cloud.version>Dalston.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 实现Config的客户端配置 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <!-- 实现通过端点refresh手动刷新 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope> import </scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> |
(2)、bootstrap.yml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | spring: application: name: microservice-foo #这里对应的是git仓库里面的配置文件application-profile中的application部分 cloud: config: # uri: http: //localhost:9009 profile: dev #这里对应的是git仓库里面的配置文件application-profile中的profile部分 label: master discovery: enabled: true # 表示使用服务发现组件中的Config Server,而不自己指定Config Server的uri,默认 false service-id: microservice-config-server # 指定Config Server在服务发现中的serviceId,默认是configserver bus: trace: enabled: true rabbitmq: addresses: 127.0 . 0.1 : 5672 #mq服务器地址 username: 47Gamer #账号 password: ???????? #密码 |
主要是配置了
<1>、从那个配置服务端拉取配置文件(service-id: microservice-config-server)
<2>、拉取仓库里面对应的文件中的属性(spring.application.name和config.profile和config.label)
(3)、application.yml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | server: port: 9008 management: #开放所有端点 endpoints: web: exposure: include: "*" security: #关闭安全验证 enabled: false eureka: client: serviceUrl: defaultZone: http: //localhost:1001/eureka/ instance: instance-id: ${spring.cloud.client.ipAddress}:${server.port} #设置服务在注册中心的实例id prefer-ip-address: true |
(4)、创建一个配置类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | @Component @RefreshScope public class ConfigProperties { @Value ( "${encrypt.key}" ) private String key; public String getKey() { return key; } public void setKey(String key) { this .key = key; } } |
@RefreshScope这个注解表示,git仓库的文件更新之后,也会更新被@RefreshScope标注的属性值(可以直接标注在类上,也可以直接标注到具体的属性值上)。
手动更新: post方式请求配置服务端的/bus/refresh端点,就可以更新所有与这个服务端想关联的微服务的配置属性。
自动更新:
也就是在git仓库修改文件属性后,不需要手动请求服务端的/bus/refresh端点,自动更新。比如是gitHub远程仓库,那么需要设置仓库的webhook地址为服务端的/bus/refresh端点地址,设置好了之后,修改git文件,直接触发 服务端ip:端口/bus/refresh这个请求.从而实现自动更新。
注意:webhook地址只能设置为公网地址,设置为内网地址是不能生效的。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具