springcloud之基于RabbitMQ消息总线方式刷新配置服务

前言

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

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

1:jdk 1.8、idea2018、Maven3

2:Spring Boot 2.0.6.RELEASE

3:Spring Cloud Finchley.SR2

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

5:RabbitMQ服务端环境安装

下载Erlang;http://www.erlang.org/downloads {安装后配置环境变量:D:\Program Files\erl10.5}

下载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-service.bat stop

rabbitmq-service.bat start

浏览器访问;http://127.0.0.1:15672

服务端口5672

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

java 复制代码
@RestController
@RefreshScope
public class ConfigClientController {

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

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

}

ConfigClientApplication.java & 普通配置即可

java 复制代码
@SpringBootApplication
public class ConfigClientApplication {

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

}

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

yaml 复制代码
spring:
  application:
    name: 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 添加配置服务

yaml 复制代码
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/

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

yaml 复制代码
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

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

}

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

yaml 复制代码
server:
  port: 8080

spring:
  application:
    name: springcloud-config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/xxx/itstack-demo-config  # 换成自己的配置Git仓库的地址,如果没有可以新建工程地址
          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

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

java 复制代码
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

}
 

application.yml & 配置信息

yaml 复制代码
server:
  port: 7397

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

spring:
  application:
    name: springcloud-eureka-server

测试验证

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

2:配置Webhooks,在https://github.com/

分别启动服务

启动RabbitMQ服务;http://127.0.0.1:15672/#/

springcloud-eureka-server 服务注册发现

springcloud-config-server 配置Server

springcloud-config-client 配置Client

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

yaml 复制代码
{
	"name": "config-client",
	"profiles": [
		"dev"
	],
	"label": null,
	"version": "ea0b1a1017595d542aa01b8b2bda68f9620dd81a",
	"state": null,
	"propertySources": [
		{
			"name": "https://github.com/xxx/config/config-repo/config-client-dev.yml",
			"source": {
				"info.profile": "dev bus"
			}
		}
	]
}

综上总结

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

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

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

好了到这里就结束了springcloud之基于RabbitMQ消息总线方式刷新配置服务的学习,大家一定要跟着动手操作起来。需要源码的 可si我获取;

相关推荐
java1234_小锋3 小时前
RabbitMQ如何实现消息的持久化?
java·rabbitmq·java-rabbitmq
lllsure5 小时前
【Docker】镜像
java·spring cloud·docker
AAA修煤气灶刘哥7 小时前
ES数据同步大乱斗:同步双写 vs MQ异步,谁才是王者?
分布式·后端·elasticsearch
u0104058368 小时前
电商导购平台的搜索引擎优化:基于Elasticsearch的商品精准推荐系统
elasticsearch·搜索引擎·jenkins
在未来等你9 小时前
Elasticsearch面试精讲 Day 16:索引性能优化策略
大数据·分布式·elasticsearch·搜索引擎·面试
AD钙奶-lalala10 小时前
RabbitMQ的核心使用示例
java·rabbitmq·java-rabbitmq
T_Ghost10 小时前
SpringCloud微服务网关Gateway
spring cloud·微服务·gateway
山城码农笑松哥10 小时前
国产凝思debian系Linux离线安装rabbitmq教程步骤
linux·debian·rabbitmq
小句11 小时前
RabbitMQ对接MQTT消息发布指南
分布式·rabbitmq·ruby
咖啡Beans11 小时前
SpringBoot集成ELK实现数据存储和日志管理
spring boot·elasticsearch·kibana