RabbitMQ消息总线方式刷新配置服务全过程

2025-07-17 19:50

本文主要是介绍RabbitMQ消息总线方式刷新配置服务全过程,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

《RabbitMQ消息总线方式刷新配置服务全过程》SpringCloudBus通过消息总线与MQ实现微服务配置统一刷新,结合GitWebhooks自动触发更新,避免手动重启,提升效率与可靠性,适用于配...

前言介绍

在微服务架构中,为了更方便的向微服务实例广播消息,我们通常会构建一个消息中心,让所有的服务实例都连接上来,而该消息中心所发布的消息都会被微服务实例监听和消费,我们把这种机制叫做消息总线(SpringCloud
Bus)

当我们的微服务达到是几个到百个以上,在更新配置时,不太可能一个个刷新或者重启,这样既不能保证效率也容易导致遗漏造成事故。因此我们需要SpringCloud Bus 提供总线服务,在我们push代码到Git的时候,通过Webhooks(http://localhost:port/actuator/bus-refresh/)执行刷新,消息总线会通知各个实例更新配置,以达到自动更新全服务配置。

RabbitMQ消息总线方式刷新配置服务全过程

环境准备

  • jdk 1.8、idea2018、Maven3
  • Spring Boot 2.0.6.RELEASE
  • Spring Cloud Finchley.SR2

需要有一个Git帐号,用来创建配置中心以及开启Webhooks服务,添加回调

RabbitMQ服务端环境安装

  1. 下载Erlang;http://www.erlang.org/downloads {安装后配置环境变量:D:\Program Files\erl10.5}
  2. 下载rabbitMQ;http://www.rabbitmq.com/download.html {安装后CMD依次执行}
  • cd D:\Program Files\RabbitMQ Server\rabbitmq_server-3.8.1\sbin
  • rabbitmq-plugins.BAT enable rabbitmq_management
  • rabbitmq-serpythonvice.bat stop
  • rabbitmq-service.bat start
  • 浏览器访问;http://127.0.0.1:15672
  • 服务端口5672

代码示例

itstack-demo-springcloud-07
├── itstack-demo-springcloud-config-client
│   └── src
│       └── main
│           ├── Java
│           │   └── org.itstack.demo
│           │        ├── web
│           │        │   └── ConfigClien编程tController.java      
│           │        └── ConfigClientApplication.java
│           └── resources   
│               ├── application.yml
│               └── bootstrap.yml
├── itstack-demoChina编程-springcloud-config-server
│   └── src
│       └── main
│           ├── java
│           │   └── org.itstack.demo   
│           │        └── ConfigServerApplication.java
│           └── resources   
│               └── application.yml
└── itstack-demo-springcloud-eureka-server
     └── src
        └── main
            ├── java
            │   └── org.itstack.demo   
            │        └── EurekaServerApplication.java
            └── resources   
                └── application.yml

itstack-demo-springcloud-config-client | 配置获取客户端方,提供自动刷新Http

web/ConfigClientController.java & 添加注解@RefreshScope自动刷新配置

@RestController
@RefreshScope
public class ConfigClientController {

    @Value("${info.profile:error}")
    private String profile;

    @GetMapping("/config")
    public Mono<String> config() {
        return Mono.justOrEmpty(profile);
    }

}

ConfigClientApplication.java & 普通配置即可

@SpringBootApplication
public class ConfigClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplicationphp.class, args);
    }

}

application.yml & 需要配置endpoints,这样才可以暴漏刷新服务

spring:
  application:
    name: itstack-demo-springcloud-config-client
  cloud:
    bus:
      trace:
        enabled: true
      enabled: true
server:
  port: 9001

# 如果不使用消息总线,则开启如下配置 /actuator/refresh 这个 Endpoint 暴露出来
#management:
#  endpoints:
#    web:
#      exposure:
#        include: refresh

bootstrap.yml & 配置中心服务配置,http://localhost:7397 添加配置服务

spring:
  cloud:
    config:
      name: config-client         # 对应 {application} 部分,例如;config-client-dev = 只取最后一个符号'-'之前的
      profile: dev                # 对应 {profile} 部分
      label: master               # 对应 {label} 部分,即 Git 的分支。如果配置中心使用的是本地存储,则该参数无用
      discovery:
        enabled: true             # 开启 config 服务发现支持
        service-id: itstack-demo-springcloud-config-server        # 配置服务name

#配置文件会被转换成 Web,访问规则如下;
#/{application}/{profile}[/{label}]
#/{application}-{profile}.yml
#/{label}/{application}-{profile}.yml
#/{application}-{profile}.properties
#/{label}/{application}-{profile}.properties

eureka:
  client:
    service-url:
      defaultZone: http://localhost:7397/eureka/

itstack-demo-springcloud-config-server | 配置提供服务端方,链接Git配置工程地址

ConfigServerApplication.java & 添加注解@EnableConfigServer设置成配置服务中心

@Sphttp://www.chinasem.cnringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }

}

application.yml & 配置信息,消息总线刷新

server:
  port: 8080

spring:
  application:
    name: itstack-demo-springcloud-config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/fuzhengwei/itstack-demo-config  # 换成自己的配置Git仓库的地址,如果没有可以新建工程地址,也可以克隆我的;https://github.com/fuzhengwei/itstack-demo-config
          search-paths: config-repo                               # Git仓库地址下的底层配置文件名称,如果配置多个用逗号','分割。

# 如果配置中心需要访问权限,则开启配置
# spring.cloud.config.server.git.username:Github账户
# spring.cloud.config.server.git.password:Github密码

eureka:
  client:
    service-url:
      defaultZone: http://localhost:7397/eureka/
management:
  endpoints:
    web:
      exposure:
        include: bus-refresh

itstack-demo-springcloud-eureka-server | 服务注册发现

EurekaServerApplication.java & 添加注解@EnableEurekaServer启动服务发现

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run( EurekaServerApplication.class, args );
    }

}

application.yml & 配置信息

server:
  port: 7397

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

spring:
  application:
    name: itstack-demo-springcloud-eureka-server

测试验证

1.准备好自己Github的配置仓库,也可以克隆我的Git;https://github.com/fuzhengwei/itstack-demo-config {有一组配置配置文件}

2.配置Webhooks,在https://github.com/换你自己的fuzhengwei/换你自己的itstack-demo-netty/settings/hooks/new

3.分别启动服务

1.启动RabbitMQ服务;http://127.0.0.1:15672/#/
2.itstack-demo-springcloud-eureka-server 服务注册发现
3.itstack-demo-springcloud-config-server 配置Server
4.itstack-demo-springcloud-config-client 配置Client

4.访问配置服务,端口7397;http://localhost:8080/config-client/dev

4.1.访问结果

{
	"name": "config-client",
	"profiles": [
		"dev"
	],
	"label": null,
	"version": "ea0b1a1017595d542aa01b8b2bda68f9620dd81a",
	"state": null,
	"propertySources": [
		{
			"name": "https://github.com/fuzhengwei/itstack-demo-config/config-repo/config-client-dev.yml",
			"source": {
				"info.profile": "dev bus"
			}
		}
	]
}

4.2.访问规则{配置文件会被转换成 Web 接口,规则如下}

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

4.3.访问配置文件;http://localhost:8080/config-client-dev.yml {可以直接访问查看配置信息}

info: profile: dev bus

5.访问使用配置的客户端

5.1.访问端口9001;http://localhost:9001/config

dev bus

5.2更改配置,POST请求刷新配置总线;http://localhost:8080/actuator/bus-refresh/ {如果配置Git的Webhooks则更新代码自动刷新}

5.3访问端口9001;http://localhost:9001/config

dev

总结

Spring Cloud Bus 可以更加方便的控制全局信息,用于统一刷新并通过MQ方式通过客户端

如果你的内网想进行Git的Webhooks配置,可以使用http://natapp.cn进行内网穿透映射,他会给你提供免费外网调用服务

消息总线方式不只是应用于配置刷新,在一起同步信息请求中都可以使用,以及自己的项目架设上

以上为个人经验,希望能给大家一个参考,也希望大家多多支持China编程(www.chinasem.cn)。

这篇关于RabbitMQ消息总线方式刷新配置服务全过程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1155410

相关文章

SpringBoot 多环境开发实战(从配置、管理与控制)

《SpringBoot多环境开发实战(从配置、管理与控制)》本文详解SpringBoot多环境配置,涵盖单文件YAML、多文件模式、MavenProfile分组及激活策略,通过优先级控制灵活切换环境... 目录一、多环境开发基础(单文件 YAML 版)(一)配置原理与优势(二)实操示例二、多环境开发多文件版

Vite 打包目录结构自定义配置小结

《Vite打包目录结构自定义配置小结》在Vite工程开发中,默认打包后的dist目录资源常集中在asset目录下,不利于资源管理,本文基于Rollup配置原理,本文就来介绍一下通过Vite配置自定义... 目录一、实现原理二、具体配置步骤1. 基础配置文件2. 配置说明(1)js 资源分离(2)非 JS 资

MySQL8 密码强度评估与配置详解

《MySQL8密码强度评估与配置详解》MySQL8默认启用密码强度插件,实施MEDIUM策略(长度8、含数字/字母/特殊字符),支持动态调整与配置文件设置,推荐使用STRONG策略并定期更新密码以提... 目录一、mysql 8 密码强度评估机制1.核心插件:validate_password2.密码策略级

ShardingProxy读写分离之原理、配置与实践过程

《ShardingProxy读写分离之原理、配置与实践过程》ShardingProxy是ApacheShardingSphere的数据库中间件,通过三层架构实现读写分离,解决高并发场景下数据库性能瓶... 目录一、ShardingProxy技术定位与读写分离核心价值1.1 技术定位1.2 读写分离核心价值二

HTTP 与 SpringBoot 参数提交与接收协议方式

《HTTP与SpringBoot参数提交与接收协议方式》HTTP参数提交方式包括URL查询、表单、JSON/XML、路径变量、头部、Cookie、GraphQL、WebSocket和SSE,依据... 目录HTTP 协议支持多种参数提交方式,主要取决于请求方法(Method)和内容类型(Content-Ty

QT Creator配置Kit的实现示例

《QTCreator配置Kit的实现示例》本文主要介绍了使用Qt5.12.12与VS2022时,因MSVC编译器版本不匹配及WindowsSDK缺失导致配置错误的问题解决,感兴趣的可以了解一下... 目录0、背景:qt5.12.12+vs2022一、症状:二、原因:(可以跳过,直奔后面的解决方法)三、解决方

SpringBoot路径映射配置的实现步骤

《SpringBoot路径映射配置的实现步骤》本文介绍了如何在SpringBoot项目中配置路径映射,使得除static目录外的资源可被访问,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一... 目录SpringBoot路径映射补:springboot 配置虚拟路径映射 @RequestMapp

RabbitMQ 延时队列插件安装与使用示例详解(基于 Delayed Message Plugin)

《RabbitMQ延时队列插件安装与使用示例详解(基于DelayedMessagePlugin)》本文详解RabbitMQ通过安装rabbitmq_delayed_message_exchan... 目录 一、什么是 RabbitMQ 延时队列? 二、安装前准备✅ RabbitMQ 环境要求 三、安装延时队

sysmain服务可以禁用吗? 电脑sysmain服务关闭后的影响与操作指南

《sysmain服务可以禁用吗?电脑sysmain服务关闭后的影响与操作指南》在Windows系统中,SysMain服务(原名Superfetch)作为一个旨在提升系统性能的关键组件,一直备受用户关... 在使用 Windows 系统时,有时候真有点像在「开盲盒」。全新安装系统后的「默认设置」,往往并不尽编

Python 基于http.server模块实现简单http服务的代码举例

《Python基于http.server模块实现简单http服务的代码举例》Pythonhttp.server模块通过继承BaseHTTPRequestHandler处理HTTP请求,使用Threa... 目录测试环境代码实现相关介绍模块简介类及相关函数简介参考链接测试环境win11专业版python