第九章 Spring Cloud 消息总线-Final版(Spring Cloud Bus)

在上一篇文章当中,我们利用消息总线触发一个客户端(/actuator/bus-refresh),然后刷新所有客户端而重新请求获得配置属性的目的。
它有以下缺点:
1、打破了微服务职责的单一性,微服务本身是业务模块,它不应该承担配置刷新的责任。
2、破坏了微服务各节点的对等性。
在上一篇文章的基础之上我们稍微对它做一些改动,架构图如下:
在这里插入图片描述
步骤如下:
1、触发post请求(/actuator/bus-refresh)
2、Config Server 服务端接收请求并发送给Spring Cloud Bus
3、Spring Cloud 接收到消息并通知其它客户端。
4、其它客户端接收到消息,请求Config Server端获得配置信息。
5、全部的客户端均获得最新的配置信息。

1、修改Config Client

spring-cloud-config-client-1和spring-cloud-config-client-2两个都需要修改,我们拿其中一个进行说明。

第一步:修改pom.xml依赖

因为“bus-refresh”我们不需要这个组件了,我们把它放在了Config Server当中了,因此我们把它去掉即可,就变成如下:

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <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-bus-amqp</artifactId>
        </dependency>
    </dependencies>

第二步:去掉application.properties当中的“bus-refresh”

配置如下:

#1、rabbitmq的主机地址
spring.rabbitmq.host=192.168.1.128
#2、rabbitmq的端口
spring.rabbitmq.port=5672
#3、rabbitmq的账号
spring.rabbitmq.username=brimen
#4、rabbitmq的密码
spring.rabbitmq.password=123456

#5、消息总线可用
spring.cloud.bus.enabled=true
#6、开启消息总线跟踪
spring.cloud.bus.trace.enabled=true

2、修改Config Server(spring-cloud-config)

第一步:添加依赖

在上一步当中,我们把客户端的“bus-refresh”删掉了,在这一步当中,我们把它放进来,并且添加rabbitmq相关依赖,如下:

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <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.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

第二步:在application.yml当中进行修改

spring:
  application:
    name: spring-cloud-config
  cloud:
    config:
      server:
        git:
          username: #1
          password: #2
          uri: https://github.com/runday/spring-cloud-brimen-file-config #3
          search-paths: /brimen #4
          default-label: master #5
    bus:
      enabled: true
      trace:
        enabled: true #10
  rabbitmq:
    host: 192.168.1.128 #6
    port: 5672          #7
    username: brimen    #8
    password: 123456    #9
server:
  port: 8090
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8080/eureka/

management:
  endpoints:
    web:
      exposure:
        include: bus-refresh

#1、配置git的用户名(公共仓库不需要配置,可放心使用)
#2、配置git的密码
#3、配置git的地址
#4、配置访问路径
#5、配置分支,默认为master分支
#6、主机地址
#7、端口号
#8、用户名
#9、密码
#10、开启消息总线跟踪

3、依次启动项目

依次启动spring-cloud-eureka-serve、spring-cloud-config、spring-cloud-config-client-1
和spring-cloud-config-client-2,然后访问 http://localhost:8091/getvalue ,可以正常获得值。按照上一篇文章的测试方法,修改git当中配置文件的值,然后使用postman访问 http://localhost:8090/actuator/bus-refresh, 然后两个客户端重新请求接口,发现值已经变为修改以后的值,OK,没问题,到此已经结束。

猜你喜欢

转载自blog.csdn.net/qq_24630433/article/details/87718926