十一:Spring Cloud 之消息总线-

1. 简介

Spring Cloud Bus links the nodes of a distributed system with a lightweight message broker. This broker can then be used to broadcast state changes (such as configuration changes) or other management instructions. A key idea is that the bus is like a distributed actuator for a Spring Boot application that is scaled out. However, it can also be used as a communication channel between apps. This project provides starters for either an AMQP broker or Kafka as the transport.


事件、消息总线,用于在集群(例如,配置变化事件)中传播状态变化,可与Spring Cloud Config联合实现热部署。

基于RabbitMQ实现上一篇笔记中的配置手动刷新,RabbitMQ的安装请自行搜索,本片记录中的RabbitMQ使用的都是默认配置。

2. 代码实现

2.1 涉及的模块及整体步骤

2.1.1 涉及的模块

  • eureka-server-singleton:eureka服务发布注册中心
  • config-server-ha:配置中心服务端,通过指定不同端口启动两个实例模拟服务端集群
  • config-client-bus:新建的通过HA版服务配置中心访问远程配置信息模块,也可以使用原有模块
  • config-repository:放置于GitHub的配置,config-client-bus-test.properties是对应的本次测试的保存配置信息的文件名称

2.1.2 整体步骤

  1. GitHub创建存放配置信息的config-repository目录与配置信息config-client-bus-test.properties,可通过demo中的config-repository模块关联GitHub上的配置。
  2. 实现eureka-server-singleton:eureka服务发布注册中心,与前面没有任何区别
  3. 实现config-server-ha:关键是启动Spring Cloud Config Server功能,指定配置仓库的配置信息
  4. 实现config-client-bus:从配置仓库读取配置信息,引入spring-boot-starter-actuator,spring-cloud-starter-bus-amqp
  5. 通过为config-client-bus指定8775、8776端口实现多实例启动
  6. 修改Github上远程配置文件config-client-bus-test.properties,通过8775或者8776刷新配置,再次访问两个实例读取的相同配置项的值,观察修改是否生效
  7. config-repository中添加config-client-bus-test.properties:放置4步骤的配置信息

2.2 源代码

2.2.1 Github地址

https://github.com/andyChenHuaYing/spring-cloud-demo

2.2.2 配置信息地址

配置信息在dev-20180827、与master分支都有,但是代码中使用的是dev-20180827分支的代码,如果想修改值验证,记得确认客户端bootstrap.properties中配置信息与想要访问的仓库地址一致。
https://github.com/andyChenHuaYing/spring-cloud-demo/tree/dev-20180827/config-repository

2.3 eureka-server-singleton

Spring Cloud 之服务发现与调用-Ribbon#2.3 eureka-server-singleton 没有任何区别

2.4 config-server-ha

十:Spring Cloud 之配置中心HA版-config 没有任何区别

2.5 config-client-bus

2.5.1 整体实现

  1. pom.xml文件中引入依赖spring-cloud-starter-configspring-cloud-starter-netflix-eureka-clientconfig-client-bus-test.propertiesspring-boot-starter-actuator
  2. bootstrap.yml中指定Config Server连接信息以及需要访问配置中心的具体配置信息
  3. application-8775.yml、application-8776.yml指定当前模块的配置信息,通过profile指定不同端口模拟启动多实例
  4. ConfigClientBusApplication常规Spring Boot启动类

2.5.2 pom.xml

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-cloud-finchley-demo</artifactId>
        <groupId>org.oscar.scd</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>config-client-bus</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>
</project>

2.5.3 bootstrap.yml

下方信息与配置中心的配置文件的对应关系见后续验证部分
spring:
  application:
    name: config-client-bus
  cloud:
    config:
      label: dev-20180827
      profile: test
      discovery:
        enabled: true
        serviceId: config-server-ha

2.5.4 application-8775.yml

application-8776.yml 只是端口不一样

spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
  cloud:
    bus:
      enabled: true
      trace:
        enabled: true
management:
  endpoints:
    web:
      exposure:
        include: bus-refresh

server:
  port: 8775
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

2.6 config-repository

保存配置文件

2.6.1 config-client-bus-test.properties配置文件

foo=config-client-bus foo value updated.

3. 验证

3.1 创建SpringBoot启动类

简单创建Spring Boot启动类即可

3.1.1 EurekaServerSingletonApplication

最简单的方式添加一个SpringBoot启动类型的启动类就行。

这里写图片描述

3.1.2 ConfigServerHaApplication-8772

在这里插入图片描述

3.1.3 ConfigServerHaApplication-8773

参考上一节,修改Active profiles:8773

3.1.4 ConfigClientBusApplication-8775

参考上一节,指定Active profiles:8775。

3.1.5 ConfigClientBusApplication-8776

参考上一节,指定Active profiles:8776。

3.2 启动

  1. EurekaServerSingletonApplication
  2. ConfigServerHaApplication-8772
  3. ConfigServerHaApplication-8773
  4. ConfigClientBusApplication-8775
  5. ConfigClientBusApplication-8776

3.3查看eureka服务信息界面

在这里插入图片描述

3.4 读取远程配置信息

3.4.1 查看指定配置项的值

3.4.2 查看指定配置项修改后的值

  1. 本地修改资源文件config-client-bus-test.properties配置项foo=config-client-bus foo value,并push到远程仓库,查看 https://github.com/andyChenHuaYing/spring-cloud-demo/blob/dev-20180827/config-repository/config-client-bus-test.properties 有没有成功(这里可以换成自己的Github测试一下)修改后的值foo=config-client-bus foo value updated again 2018-09-20.

  2. 再次访问 http://localhost:8775/readFooProp,返回值不变,值为:config-client-bus foo value updated.

  3. 访问http://localhost:8775/actuator/bus-refresh 手动刷新配置项,可看到后台重新请求Github地址,拉取配置。注意:这里需要使用post请求,并且header中的Content-Type值为application/json。可以使用Intellij 的HTTP Client。
    在这里插入图片描述

  4. 观察8775控制台输出的日子,发现重新拉取了远程的配置信息.

  5. 再次访问 http://localhost:8775/readFooProp,返回值变成修改后的值config-client-bus foo value,说明成功读取到新配置值再次访问 http://localhost:8775/readFooProp,返回值变成修改后的值config-client-bus foo value,说明成功读取到新配置值
    在这里插入图片描述

  6. 访问http://localhost:8776/readFooProp,返回值变成修改后的值config-client-bus foo value,说明成功读取到新配置值
    在这里插入图片描述

  7. 通过消息总线实现变更配置信息同步功能生效

4. 思考

  • 消息总线是如何接收消息并广播的
  • SpringCloud消息总线还支持哪些消息中间件
  • 如果基于消息中线设计一套基于事件驱动的系统架构,需要解决哪些核心关键点

5. 补充

5.1 资料

https://springcloud.cc/spring-cloud-bus.html

http://cloud.spring.io/spring-cloud-static/Finchley.SR1/multi/multi__spring_cloud_bus.html

http://cloud.spring.io/spring-cloud-static/Finchley.SR1/multi/multi__spring_cloud_config_client.html

http://cloud.spring.io/spring-cloud-static/Finchley.SR1/multi/multi__spring_cloud_config_server.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值