美文网首页
Bus消息总线

Bus消息总线

作者: 昨日已逝去 | 来源:发表于2019-02-27 09:46 被阅读8次

Bus消息总线

原文在github,有些相对路径连接不能跳转,如想看原文项目地址 spingboot2.1.3加springcloud G版本,如果觉的不错给个star 谢谢!

Bus简介

Spring Cloud Bus 将分布式的节点用轻量的消息代理连接起来。它可以用于广播配置文件的更改或者服务之间的通讯,也可以用于监控。
本文要讲述的是用Spring Cloud Bus实现通知微服务架构的配置文件的更改。

示例演示

功能说明,修改配置文件而不重启服务,进行刷新加载。

结合config 我们对spring-cloud-config-server改造成我们sprign-cloud-bus

前期准备工作

  • 和config相同,在config-repo下创建一个bus-client-dev.properties
foo = foo version 22

服务端改造(增加eureka注册)

  • 添加maven依赖(由于本次使用eureka)
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
  • bootstrap.properties配置
spring.application.name=bus-server
server.port=8888

spring.cloud.config.server.git.uri=https://github.com/DespairYoke/java-advance
spring.cloud.config.server.git.search-paths=spring-cloud/config-repo
spring.cloud.config.server.git.username=username
spring.cloud.config.server.git.password=password

eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
  • 启动类示例
@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class SpringCloudBusApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringCloudBusApplication.class, args);
    }

}

客户端改造(重点)

  • 添加消息总线和eureka依赖
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<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-actuator</artifactId>
</dependency>

此处使用amqp来做消息传播介质,也可使用kafaka。

  • 添加bootstrap.properties配置属性
spring.application.name=bus-client

server.port=8111
spring.cloud.config.label=master

spring.cloud.config.uri=http://localhost:8888

spring.cloud.config.profile=dev

spring.rabbitmq.host=47.96.127.51
spring.rabbitmq.port=5672
spring.rabbitmq.username=springcloud
spring.rabbitmq.password=123456

spring.cloud.bus.enabled=true
## 开启消息跟踪
spring.cloud.bus.trace.enabled=true
## 启用bus-refresh的监控端点
management.endpoints.web.exposure.include=bus-refresh

eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
  • 启动类示例
@SpringBootApplication
@RestController
@EnableEurekaClient
@RefreshScope
public class SpringCloudBusClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringCloudBusClientApplication.class, args);
    }

    @Value(value = "${foo}")
    public String foo;

    @RequestMapping("/")
    public String home() {
        return "Hello World!"+foo;
    }
}

环境搭建完成

访问 http://localhost:8000/ ,查看注册中心信息。

bus-eureka.png

访问http://localhost:8111/http://localhost:8112/ 返回内容都是

Hello World!foo version 33

修改config-repo下的bus-client-dev.properties内容。

重点:向8111或8112发送bus-refresh请求,不同版本请求路径可能有所不同,这里请求地址为
http://localhost:8112/actuator/bus-refresh ,而且请求一定要是post请求才可以。如下

bus-postman.png

再次访问http://localhost:8111/http://localhost:8112/ 返回内容都是

Hello World!foo version 44

项目架构

bus-archtive.png

总结: 可见服务通过消息总线实现不重启更新配置文件,并且通知其他相关服务更新配置。

相关文章

  • Bus消息总线

    Bus消息总线 原文在github,有些相对路径连接不能跳转,如想看原文项目地址 spingboot2.1.3加s...

  • Bus 消息总线

    Bus概述 Spring Cloud Bus 是用轻量的消息中间件将分布式的节点连接起来,可以用于广播配置文件的更...

  • Spring Cloud Bus:消息总线

    Spring Cloud Bus:消息总线 Spring Cloud Bus 使用轻量级的消息代理来连接微服务架构...

  • 高级框架第十三天Config:分布式配置中心

    第一部分 Bus:消息总线 主要内容 1.消息总线简介 2.基于消息总线实现全局热刷新 一.消息总线简介 1.什么...

  • Chapter Nine《SpringCloud微服务实战》

    消息总线: Spring Cloud Bus 1.什么是BUS? spring cloud是按照spring的配置...

  • 消息总线-Springcloud Bus

    什么是消息总线 在微服务架构的系统中, 我们通常会使用轻量级的消息代理来构建一个共用的消息主题让系统中所有微服务实...

  • SpringCloud Bus 消息总线

    上一篇文章我们介绍了配置中心,但是现在更新配置还是要重启啊!?所以我们本章就来介绍一个不重启的方法。 相关资料以及...

  • 54 消息总线bus

    在微服务架构中,通常会使用轻量级的消息代理来构建一个共用的消息主题来连接各个微服务实例,它 广播的消息会被所有在注...

  • spring cloud bus

    Bus是一个消息总线,config-server和其他服务都通过Bus监听一个消息队列(本例用Kafka), 通过...

  • Spring Cloud 10 -- 通过 Bus 刷新从配置中

    一、对 client01 添加 Bus 相关依赖 二、修改配置文件,添加配置 提示:使用消息总线 Bus ,需要 ...

网友评论

      本文标题:Bus消息总线

      本文链接:https://www.haomeiwen.com/subject/oohxuqtx.html