
作者:付政委
读书不觉已春深,一寸光阴一寸金。 不是道人来引笑,周情孔思正追寻。
微信公众号:bugstack虫洞栈
沉淀、分享、成长,专注于原创专题案例,以最易学习编程的方式分享知识,让自己和他人都能有所收获。目前已完成的专题有;Netty4.x实战专题案例、用Java实现JVM、基于JavaAgent的全链路监控、手写RPC框架、架构设计专题案例[Ing]等。
前言介绍
在微服务架构中,为了更方便的向微服务实例广播消息,我们通常会构建一个消息中心,让所有的服务实例都连接上来,而该消息中心所发布的消息都会被微服务实例监听和消费,我们把这种机制叫做消息总线(SpringCloud Bus)
当我们的微服务达到是几个到百个以上,在更新配置时,不太可能一个个刷新或者重启,这样既不能保证效率也容易导致遗漏造成事故。因此我们需要SpringCloud Bus 提供总线服务,在我们push代码到Git的时候,通过Webhooks(http://localhost:port/actuator/bus-refresh/)执行刷新,消息总线会通知各个实例更新配置,以达到自动更新全服务配置。

微信公众号:bugstack虫洞栈 & 消息总线配置更新
环境准备
- jdk 1.8、idea2018、Maven3 
- Spring Boot 2.0.6.RELEASE 
- Spring Cloud Finchley.SR2 
- 需要有一个Git帐号,用来创建配置中心以及开启Webhooks服务,添加回调 
- RabbitMQ服务端环境安装 
- 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 
- 下载Erlang;http://www.erlang.org/downloads {安装后配置环境变量:D:\Program Files\erl10.5} 
- 下载rabbitMQ;http://www.rabbitmq.com/download.html {安装后CMD依次执行} 
代码示例
 1itstack-demo-springcloud-07 2├── itstack-demo-springcloud-config-client 3│   └── src 4│       └── main 5│           ├── java 6│           │   └── org.itstack.demo 7│           │        ├── web 8│           │        │   └── ConfigClientController.java       9│           │        └── ConfigClientApplication.java10│           └── resources   11│               ├── application.yml12│               └── bootstrap.yml13├── itstack-demo-springcloud-config-server14│   └── src15│       └── main16│           ├── java17│           │   └── org.itstack.demo   18│           │        └── ConfigServerApplication.java19│           └── resources   20│               └── application.yml21└── itstack-demo-springcloud-eureka-server22     └── src23        └── main24            ├── java25            │   └── org.itstack.demo   26            │        └── EurekaServerApplication.java27            └── resources   28                └── application.yml
完整代码欢迎关注公众号:bugstack虫洞栈 回复“SpringCloud专题”进行下载
itstack-demo-springcloud-config-client | 配置获取客户端方,提供自动刷新Http
web/ConfigClientController.java & 添加注解@RefreshScope自动刷新配置
 1/** 2 * 微信公众号:bugstack虫洞栈 | 沉淀、分享、成长,专注于原创专题案例 3 * 论坛:http://bugstack.cn 4 * Create by 付政委 on @2019 5 */ 6@RestController 7@RefreshScope 8public class ConfigClientController { 910    @Value("${info.profile:error}")11    private String profile;1213    @GetMapping("/config")14    public Mono<String> config() {15        return Mono.justOrEmpty(profile);16    }1718}
1
ConfigClientApplication.java & 普通配置即可
 1/** 2 * 微信公众号:bugstack虫洞栈 | 沉淀、分享、成长,专注于原创专题案例 3 * 论坛:http://bugstack.cn 4 * Create by 付政委 on @2019 5 */ 6@SpringBootApplication 7public class ConfigClientApplication { 8 9    public static void main(String[] args) {10        SpringApplication.run(ConfigClientApplication.class, args);11    }1213}
application.yml & 需要配置endpoints,这样才可以暴漏刷新服务
 1spring: 2  application: 3    name: itstack-demo-springcloud-config-client 4  cloud: 5    bus: 6      trace: 7        enabled: true 8      enabled: true 9server:10  port: 90011112# 如果不使用消息总线,则开启如下配置 /actuator/refresh 这个 Endpoint 暴露出来13#management:14#  endpoints:15#    web:16#      exposure:17#        include: refresh
bootstrap.yml & 配置中心服务配置,http://localhost:7397 添加配置服务
 1spring: 2  cloud: 3    config: 4      name: config-client         # 对应 {application} 部分,例如;config-client-dev = 只取最后一个符号'-'之前的 5      profile: dev                # 对应 {profile} 部分 6      label: master               # 对应 {label} 部分,即 Git 的分支。如果配置中心使用的是本地存储,则该参数无用 7      discovery: 8        enabled: true             # 开启 config 服务发现支持 9        service-id: itstack-demo-springcloud-config-server        # 配置服务name1011#配置文件会被转换成 Web,访问规则如下;12#/{application}/{profile}[/{label}]13#/{application}-{profile}.yml14#/{label}/{application}-{profile}.yml15#/{application}-{profile}.properties16#/{label}/{application}-{profile}.properties1718eureka:19  client:20    service-url:21      defaultZone: http://localhost:7397/eureka/
itstack-demo-springcloud-config-server | 配置提供服务端方,链接Git配置工程地址
ConfigServerApplication.java & 添加注解@EnableConfigServer设置成配置服务中心
 1/** 2 * 微信公众号:bugstack虫洞栈 | 沉淀、分享、成长,专注于原创专题案例 3 * 论坛:http://bugstack.cn 4 * Create by 付政委 on @2019 5 */ 6@SpringBootApplication 7@EnableConfigServer 8public class ConfigServerApplication { 910    public static void main(String[] args) {11        SpringApplication.run(ConfigServerApplication.class, args);12    }1314}
application.yml & 配置信息,消息总线刷新
 1server: 2  port: 8080 3 4spring: 5  application: 6    name: itstack-demo-springcloud-config-server 7  cloud: 8    config: 9      server:10        git:11          uri: https://github.com/fuzhengwei/itstack-demo-config  # 换成自己的配置Git仓库的地址,如果没有可以新建工程地址,也可以克隆我的;https://github.com/fuzhengwei/itstack-demo-config12          search-paths: config-repo                               # Git仓库地址下的底层配置文件名称,如果配置多个用逗号','分割。1314# 如果配置中心需要访问权限,则开启配置15# spring.cloud.config.server.git.username:Github账户16# spring.cloud.config.server.git.password:Github密码1718eureka:19  client:20    service-url:21      defaultZone: http://localhost:7397/eureka/22management:23  endpoints:24    web:25      exposure:26        include: bus-refresh
itstack-demo-springcloud-eureka-server | 服务注册发现
EurekaServerApplication.java & 添加注解@EnableEurekaServer启动服务发现
 1/** 2 * 微信公众号:bugstack虫洞栈 | 沉淀、分享、成长,专注于原创专题案例 3 * 论坛:http://bugstack.cn 4 * Create by 付政委 on @2019 5 */ 6@SpringBootApplication 7@EnableEurekaServer 8public class EurekaServerApplication { 910    public static void main(String[] args) {11        SpringApplication.run( EurekaServerApplication.class, args );12    }1314}
application.yml & 配置信息
 1server: 2  port: 7397 3 4eureka: 5  instance: 6    hostname: localhost 7  client: 8    registerWithEureka: false 9    fetchRegistry: false10    serviceUrl:11      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/1213spring:14  application:15    name: itstack-demo-springcloud-eureka-server
测试验证
- 准备好自己Github的配置仓库,也可以克隆我的Git;https://github.com/fuzhengwei/itstack-demo-config {有一组配置配置文件} 
- 配置Webhooks,在https://github.com/换你自己的fuzhengwei/换你自己的itstack-demo-netty/settings/hooks/new 
- 分别启动服务 
- 启动RabbitMQ服务;http://127.0.0.1:15672/#/ 
- itstack-demo-springcloud-eureka-server 服务注册发现 
- itstack-demo-springcloud-config-server 配置Server 
- itstack-demo-springcloud-config-client 配置Client 
- 访问配置服务,端口7397;http://localhost:8080/config-client/dev - 1{ 2 "name": "config-client", 3 "profiles": [ 4 "dev" 5 ], 6 "label": null, 7 "version": "ea0b1a1017595d542aa01b8b2bda68f9620dd81a", 8 "state": null, 9 "propertySources": [10 {11 "name": "https://github.com/fuzhengwei/itstack-demo-config/config-repo/config-client-dev.yml",12 "source": {13 "info.profile": "dev bus"14 }15 }16 ]17} 1info:2 profile: dev bus
- /{application}/{profile}[/{label}] 
- /{application}-{profile}.yml 
- /{label}/{application}-{profile}.yml 
- /{application}-{profile}.properties 
- /{label}/{application}-{profile}.properties 
- 访问配置文件;http://localhost:8080/config-client-dev.yml {可以直接访问查看配置信息} 
- 访问规则{配置文件会被转换成 Web 接口,规则如下} 
- 访问结果 
- 访问使用配置的客户端 
- 访问端口9001;http://localhost:9001/config - dev bus
- 更改配置,POST请求刷新配置总线;http://localhost:8080/actuator/bus-refresh/ {如果配置Git的Webhooks则更新代码自动刷新} 
- 访问端口9001;http://localhost:9001/config 
`dev`
综上总结
- Spring Cloud Bus 可以更加方便的控制全局信息,用于统一刷新并通过MQ方式通过客户端 
- 如果你的内网想进行Git的Webhooks配置,可以使用http://natapp.cn进行内网穿透映射,他会给你提供免费外网调用服务 
- 消息总线方式不只是应用于配置刷新,在一起同步信息请求中都可以使用,以及自己的项目架设上 

微信公众号:bugstack虫洞栈,欢迎关注&获取源码
本文分享自微信公众号 - bugstack虫洞栈(bugstack)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。
 
  
  
  
 
 
  
 
 
 