springcloud(十)--分布式消息总线Spring Cloud Bus

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jasnet_u/article/details/82010825

Spring Cloud Bus 是什么?

Spring cloud bus通过轻量消息代理连接各个分布的节点。这会用在广播状态的变化(例如配置变化)或者其他的消息指令。Spring bus的一个核心思想是通过分布式的启动器对spring boot应用进行扩展,也可以用来建立一个多个应用之间的通信频道。目前唯一实现的方式是用AMQP消息代理作为通道,同样特性的设置(有些取决于通道的设置)在更多通道的文档中。

本篇,我们介绍下使用rabbitmq作为消息总线服务器,实现应用配置的自动刷新。

sim-configServer 微服务改造

1、pom.xml中引入

<!-- actuator 监控  -->
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-actuator</artifactId>
       </dependency>
     
        
        <!-- bus amqp消息总线-->
        <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-bus-amqp</artifactId>
       </dependency>

2 、application.yml  

server:
  port: 8888
  context-path: /
 
spring:
  application:
    name: sim-configServer
  rabbitmq:
    host: 192.168.9.130
    port: 5672
    username: rabbitadmin
    password: 123456         
  cloud:
    bus:
      trace:
        enabled: true
    config:
      label: master
      server:
        git:
          uri:  https://gitee.com/mmxl/sim-git-config
         # searchPaths: repos    /**
          search-paths: /**
          username: xxxxx
          password: xxxxx
eureka:
  instance:
    hostname: localhost  #eureka客户端主机实例名称
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka
 
management:
  security:
    enabled: false

sim-serviceC 微服务改造

1、pom.xml中引入

<!-- actuator 监控  -->
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-actuator</artifactId>
       </dependency>
         
        
        <!-- bus amqp消息总线-->
        <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-bus-amqp</artifactId>
       </dependency>

2、application.yml配置

server:
  port: 6004
 
spring:
  application:
    name: sim-serviceC
  profiles:
    active: dev,devFtp
#rabbitmq 在git仓库中配置了,这里无须再配置 
#rabbitmq:  
#    host: 192.168.9.130
#    port: 5672
#    username: rabbitadmin
#    password: 123456 
    
  
  cloud:
    config:
      label: master
      #profile: dev
      #uri:  http://localhost:8888/
      discovery:
        enabled: true
        serviceId: sim-configServer
 
 
eureka:
  instance:
    hostname: localhost  #eureka客户端主机实例名称
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka
       
management:
  security:
    enabled: false
 
info:
   groupId: com.tingcream.sim
   artifactId: sim-serviceC
   version: 0.0.1-SNAPSHOT
   developer: 张三aaa
   email: [email protected]

分别启动sim-eureka 、sim-configServer、 sim-serviceC (端口6004) 、sim-serviceC (端口6005)

访问http://localhost:6004/   ,http://localhost:6005/

尝试修改git仓库中sim-serviceC-dev.yml 文件,如app.msg= 你好,这是开发环境配置信息 ver 9 。

curl -X POST http://localhost:8888/bus/refresh  ,post方式请求sim-configServer 通知消息总线rabbitmq广播刷新配置。

注: 局部刷新 post请求  /bus/refresh?destination=sim-serviceC:**  ,则表示只通知刷新所有的sim-serviceC的微服务(不论ip) 。

curl -X GET http://localhost:8888/trace  ,获取跟踪消息总线:

[
    {
        "timestamp": "2018-08-23T23:27:42.197+0000", 
        "info": {
            "signal": "spring.cloud.bus.ack", 
            "event": "RefreshRemoteApplicationEvent", 
            "id": "e58379f2-3b35-40ce-908d-e2f26d7486d2", 
            "origin": "sim-serviceC:dev,devFtp:6004", 
            "destination": "**"
        }
    }, 
    {
        "timestamp": "2018-08-23T23:27:41.925+0000", 
        "info": {
            "method": "GET", 
            "path": "/sim-serviceC/dev,devFtp/master", 
            "headers": {
                "request": {
                    "accept": "application/json, application/*+json", 
                    "user-agent": "Java/1.8.0_161", 
                    "host": "192.168.9.1:8888", 
                    "connection": "keep-alive"
                }, 
                "response": {
                    "X-Application-Context": "sim-configServer:8888", 
                    "Content-Type": "application/json;charset=UTF-8", 
                    "Transfer-Encoding": "chunked", 
                    "Date": "Thu, 23 Aug 2018 23:27:41 GMT", 
                    "status": "200"
                }
            }, 
            "timeTaken": "4985"
        }
    }, 
    {
        "timestamp": "2018-08-23T23:27:40.274+0000", 
        "info": {
            "signal": "spring.cloud.bus.ack", 
            "event": "RefreshRemoteApplicationEvent", 
            "id": "e58379f2-3b35-40ce-908d-e2f26d7486d2", 
            "origin": "sim-serviceC:dev,devFtp:6005", 
            "destination": "**"
        }
    }, 
    {
        "timestamp": "2018-08-23T23:27:40.119+0000", 
        "info": {
            "method": "GET", 
            "path": "/sim-serviceC/dev,devFtp/master", 
            "headers": {
                "request": {
                    "accept": "application/json, application/*+json", 
                    "user-agent": "Java/1.8.0_161", 
                    "host": "192.168.9.1:8888", 
                    "connection": "keep-alive"
                }, 
                "response": {
                    "X-Application-Context": "sim-configServer:8888", 
                    "Content-Type": "application/json;charset=UTF-8", 
                    "Transfer-Encoding": "chunked", 
                    "Date": "Thu, 23 Aug 2018 23:27:40 GMT", 
                    "status": "200"
                }
            }, 
            "timeTaken": "3197"
        }
    }, 
    {
        "timestamp": "2018-08-23T23:27:35.519+0000", 
        "info": {
            "method": "POST", 
            "path": "/bus/refresh", 
            "headers": {
                "request": {
                    "host": "localhost:8888", 
                    "user-agent": "curl/7.53.1", 
                    "accept": "*/*"
                }, 
                "response": {
                    "X-Application-Context": "sim-configServer:8888", 
                    "status": "200"
                }
            }, 
            "timeTaken": "3281"
        }
    }, 
    {
        "timestamp": "2018-08-23T23:27:32.251+0000", 
        "info": {
            "signal": "spring.cloud.bus.sent", 
            "type": "RefreshRemoteApplicationEvent", 
            "id": "e58379f2-3b35-40ce-908d-e2f26d7486d2", 
            "origin": "sim-configServer:8888", 
            "destination": "**:**"
        }
    }, 
    {
        "timestamp": "2018-08-23T23:27:32.251+0000", 
        "info": {
            "signal": "spring.cloud.bus.ack", 
            "event": "RefreshRemoteApplicationEvent", 
            "id": "e58379f2-3b35-40ce-908d-e2f26d7486d2", 
            "origin": "sim-configServer:8888", 
            "destination": "**"
        }
    }, 
    {
        "timestamp": "2018-08-23T23:07:13.469+0000", 
        "info": {
            "method": "GET", 
            "path": "/trace", 
            "headers": {
                "request": {
                    "host": "localhost:8888", 
                    "connection": "keep-alive", 
                    "cache-control": "no-cache", 
                    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36", 
                    "postman-token": "31aa15c0-4746-a6b6-ea7d-7e308b59339f", 
                    "accept": "*/*", 
                    "accept-encoding": "gzip, deflate, br", 
                    "accept-language": "zh-CN,zh;q=0.9"
                }, 
                "response": {
                    "X-Application-Context": "sim-configServer:8888", 
                    "Content-Type": "application/vnd.spring-boot.actuator.v1+json;charset=UTF-8", 
                    "Transfer-Encoding": "chunked", 
                    "Date": "Thu, 23 Aug 2018 23:07:13 GMT", 
                    "status": "200"
                }
            }, 
            "timeTaken": "2"
        }
    }, 
    {
        "timestamp": "2018-08-23T23:07:00.781+0000", 
        "info": {
            "method": "GET", 
            "path": "/trace", 
            "headers": {
                "request": {
                    "host": "localhost:8888", 
                    "connection": "keep-alive", 
                    "cache-control": "no-cache", 
                    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36", 
                    "postman-token": "e5466261-c208-8d12-3bde-ef0243a4b6e0", 
                    "accept": "*/*", 
                    "accept-encoding": "gzip, deflate, br", 
                    "accept-language": "zh-CN,zh;q=0.9"
                }, 
                "response": {
                    "X-Application-Context": "sim-configServer:8888", 
                    "Content-Type": "application/vnd.spring-boot.actuator.v1+json;charset=UTF-8", 
                    "Transfer-Encoding": "chunked", 
                    "Date": "Thu, 23 Aug 2018 23:07:00 GMT", 
                    "status": "200"
                }
            }, 
            "timeTaken": "8"
        }
    }, 
    {
        "timestamp": "2018-08-23T23:06:33.884+0000", 
        "info": {
            "method": "POST", 
            "path": "/trace", 
            "headers": {
                "request": {
                    "host": "localhost:8888", 
                    "connection": "keep-alive", 
                    "content-length": "0", 
                    "cache-control": "no-cache", 
                    "origin": "chrome-extension://llfjcibadknnmokdiepoehblceoeadll", 
                    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36", 
                    "postman-token": "695036c1-ffa3-b21a-9697-6fcab00052c8", 
                    "accept": "*/*", 
                    "accept-encoding": "gzip, deflate, br", 
                    "accept-language": "zh-CN,zh;q=0.9"
                }, 
                "response": {
                    "X-Application-Context": "sim-configServer:8888", 
                    "Allow": "GET", 
                    "status": "405"
                }
            }, 
            "timeTaken": "1"
        }
    }, 
    {
        "timestamp": "2018-08-23T23:06:27.937+0000", 
        "info": {
            "method": "POST", 
            "path": "/bus/trace", 
            "headers": {
                "request": {
                    "host": "localhost:8888", 
                    "connection": "keep-alive", 
                    "content-length": "0", 
                    "cache-control": "no-cache", 
                    "origin": "chrome-extension://llfjcibadknnmokdiepoehblceoeadll", 
                    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36", 
                    "postman-token": "fae72d35-9b71-2289-c66a-0af6ac429f16", 
                    "accept": "*/*", 
                    "accept-encoding": "gzip, deflate, br", 
                    "accept-language": "zh-CN,zh;q=0.9"
                }, 
                "response": {
                    "X-Application-Context": "sim-configServer:8888", 
                    "Allow": "GET", 
                    "status": "405"
                }
            }, 
            "timeTaken": "4"
        }
    }, 
    {
        "timestamp": "2018-08-23T23:06:05.308+0000", 
        "info": {
            "signal": "spring.cloud.bus.ack", 
            "event": "RefreshRemoteApplicationEvent", 
            "id": "30cb0315-e078-45f5-a143-83661cdd2fc9", 
            "origin": "sim-serviceC:dev,devFtp:6005", 
            "destination": "sim-serviceC:**"
        }
    }, 
    {
        "timestamp": "2018-08-23T23:06:04.988+0000", 
        "info": {
            "method": "GET", 
            "path": "/sim-serviceC/dev,devFtp/master", 
            "headers": {
                "request": {
                    "accept": "application/json, application/*+json", 
                    "user-agent": "Java/1.8.0_161", 
                    "host": "192.168.9.1:8888", 
                    "connection": "keep-alive"
                }, 
                "response": {
                    "X-Application-Context": "sim-configServer:8888", 
                    "Content-Type": "application/json;charset=UTF-8", 
                    "Transfer-Encoding": "chunked", 
                    "Date": "Thu, 23 Aug 2018 23:06:04 GMT", 
                    "status": "200"
                }
            }, 
            "timeTaken": "4569"
        }
    }, 
    {
        "timestamp": "2018-08-23T23:06:03.425+0000", 
        "info": {
            "signal": "spring.cloud.bus.ack", 
            "event": "RefreshRemoteApplicationEvent", 
            "id": "30cb0315-e078-45f5-a143-83661cdd2fc9", 
            "origin": "sim-serviceC:dev,devFtp:6004", 
            "destination": "sim-serviceC:**"
        }
    }, 
    {
        "timestamp": "2018-08-23T23:06:03.053+0000", 
        "info": {
            "method": "GET", 
            "path": "/sim-serviceC/dev,devFtp/master", 
            "headers": {
                "request": {
                    "accept": "application/json, application/*+json", 
                    "user-agent": "Java/1.8.0_161", 
                    "host": "192.168.9.1:8888", 
                    "connection": "keep-alive"
                }, 
                "response": {
                    "X-Application-Context": "sim-configServer:8888", 
                    "Content-Type": "application/json;charset=UTF-8", 
                    "Transfer-Encoding": "chunked", 
                    "Date": "Thu, 23 Aug 2018 23:06:03 GMT", 
                    "status": "200"
                }
            }, 
            "timeTaken": "2826"
        }
    }, 
    {
        "timestamp": "2018-08-23T23:05:59.161+0000", 
        "info": {
            "method": "POST", 
            "path": "/bus/refresh", 
            "headers": {
                "request": {
                    "host": "localhost:8888", 
                    "connection": "keep-alive", 
                    "content-length": "0", 
                    "cache-control": "no-cache", 
                    "origin": "chrome-extension://llfjcibadknnmokdiepoehblceoeadll", 
                    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36", 
                    "postman-token": "c6f7530d-bcd6-47de-dd71-7edd0ca73dc1", 
                    "accept": "*/*", 
                    "accept-encoding": "gzip, deflate, br", 
                    "accept-language": "zh-CN,zh;q=0.9"
                }, 
                "response": {
                    "X-Application-Context": "sim-configServer:8888", 
                    "status": "200"
                }
            }, 
            "timeTaken": "5729"
        }
    }, 
    {
        "timestamp": "2018-08-23T23:05:53.623+0000", 
        "info": {
            "signal": "spring.cloud.bus.sent", 
            "type": "RefreshRemoteApplicationEvent", 
            "id": "30cb0315-e078-45f5-a143-83661cdd2fc9", 
            "origin": "sim-configServer:8888", 
            "destination": "sim-serviceC:**"
        }
    }
]

猜你喜欢

转载自blog.csdn.net/jasnet_u/article/details/82010825