博客详情

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

作者: 朝如青丝暮成雪
发布时间:2018-08-24 07:30:09  文章分类:springcloud   阅读(1098)  评论(0)


文章参考: 

https://www.cnblogs.com/ityouknow/p/6931958.html


Spring Cloud bus 是什么? 


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

Spring cloud bus被国内很多都翻译为消息总线,也挺形象的。大家可以将它理解为管理和传播所有分布式项目中的消息既可,其实本质是利用了MQ的广播机制在分布式的系统中传播消息,目前常用的有Kafka和RabbitMQ。利用bus的机制可以做很多的事情,其中配置中心客户端刷新就是典型的应用场景之一。

 

本篇,我们就利用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: zhangsan@example.com
   
 



分别启动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广播刷新配置。

局部刷新 /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:**"
        }
    }
]

 








关键字:  springcloud  消息总线  bus
评论信息
暂无评论
发表评论

亲,您还没有登陆,暂不能评论哦! 去 登陆 | 注册

博主信息
   
数据加载中,请稍候...
文章分类
   
数据加载中,请稍候...
阅读排行
 
数据加载中,请稍候...
评论排行
 
数据加载中,请稍候...

Copyright © 叮叮声的奶酪 版权所有
备案号:鄂ICP备17018671号-1

鄂公网安备 42011102000739号