首页 > 编程 > Java > 正文

SpringCloud Bus 消息总线的具体使用

2019-11-26 09:26:24
字体:
来源:转载
供稿:网友

什么是消息总线

1. 概念

在微服务架构中,通常会使用轻量级的消息代理来构建一个共用的消息主题来连接各个微服务实例, 它广播的消息会被所有在注册中心的微服务实例监听和消费,也称消息总线

2. SpringCloud Bus

SpringCloud中也有对应的解决方案,SpringCloud Bus 将分布式的节点用轻量的消息代理连接起来, 可以很容易搭建消息总线,配合SpringCloud config 实现微服务应用配置信息的动态更新。

3. 其他

消息代理属于中间件。设计代理的目的就是为了能够从应用程序中传入消息,并执行一些特别的操作。 开源产品很多如ActiveMQ、Kafka、RabbitMQ、RocketMQ等 目前springCloud仅支持RabbitMQ和Kafka。本文采用RabbitMQ实现这一功能。

搭建分布式配置中心

1. Config 架构

当一个系统中的配置文件发生改变的时候,我们需要重新启动该服务,才能使得新的配置文件生效,spring cloud config可以实现微服务中的所有系统的配置文件的统一管理,而且还可以实现当配置文件发生变化的时候,系统会自动更新获取新的配置。

2. Git 环境搭建

使用 码云 环境搭建 git

码云环境地址: https://gitee.com/guopf/springcloud_bus

3. Git服务器上传配置文件

命名规范 服务名称-版本.yml 例如configclient_dev.yml

4. 搭建 Eureka 服务注册中心

具体搭建环境随后补充,可以使用我自己部署的 http://47.105.86.222:8100 (配置地址http://47.105.86.222:8100/eureka)

5. 搭建 config-server 服务

1. maven 依赖

<dependencies>    <!-- SpringBoot整合Web组件 -->    <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-web</artifactId>    </dependency>    <!--spring-cloud 整合 config-server -->    <dependency>      <groupId>org.springframework.cloud</groupId>      <artifactId>spring-cloud-config-server</artifactId>      <version>2.0.2.RELEASE</version>    </dependency>    <!-- SpringBoot整合eureka客户端 -->    <dependency>      <groupId>org.springframework.cloud</groupId>      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>      <version>2.0.2.RELEASE</version>    </dependency> </dependencies>

2. 配置文件

### 服务地址 端口server: port: 8800### 服务名称spring: application:  name: config_server cloud:  config:   server:    git:     ### git 地址     uri: https://gitee.com/guopf/springcloud_bus.git     username:     password:     ### 配置读取文件夹     search-paths: config   ### 分支   label: master### eureka 配置eureka: client:  service-url:   defaultZone: http://47.105.86.222:8100/eureka  register-with-eureka: true  fetch-registry: true

3. 启动

/** * @EnableEurekaClient : 开启 eureka 客户端 * @EnableConfigServer : 开启 config 服务端 * */@SpringBootApplication@EnableEurekaClient@EnableConfigServerpublic class App {  public static void main(String[] args) {    SpringApplication.run(App.class,args);  }}

搭建 config-client 服务

1. 手动更新 

1. maven 依赖

  <dependencies>    <!-- SpringBoot整合Web组件 -->    <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-web</artifactId>    </dependency>    <dependency>      <groupId>org.springframework.cloud</groupId>      <artifactId>spring-cloud-config-client</artifactId>      <version>2.0.2.RELEASE</version>    </dependency>    <!-- SpringBoot整合eureka客户端 -->    <dependency>      <groupId>org.springframework.cloud</groupId>      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>      <version>2.0.2.RELEASE</version>    </dependency>    <!--核心jar包,集成rabbitMQ 消息总线 bus    <dependency>      <groupId>org.springframework.cloud</groupId>      <artifactId>spring-cloud-starter-bus-amqp</artifactId>    </dependency>    -->    <!-- actuator监控中心 -->    <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-actuator</artifactId>    </dependency>  </dependencies>

2. 配置文件(bootstrap.yml)

### 端口server: port: 8801 ### eureka 配置中心eureka: client:  service-url:   defaultZone: http://47.105.86.222:8100/eureka  fetch-registry: true  register-with-eureka: true### 配置服务名称,要和config 配置中心文件保持一致spring: application:  name: configclient cloud:  config:   ### 读取配置   profile: dev   discovery:    ###    enabled: true    ### config 配置中心配置的服务名称    service-id: config_servermanagement: endpoints:  web:   exposure:    include: "*"

3. 启动

@SpringBootApplication@EnableEurekaClient@EnableDiscoveryClientpublic class App {  public static void main(String[] args) {    SpringApplication.run(App.class,args);  }}
/** * 在读取配置文件信息的地方进行添加 @RefreshScope 注解 */@RestController@RefreshScopepublic class AppController {  @Value("${userAge}")  private String userAge;  @GetMapping("/userAge")  public String config(){    System.out.println("userAge : " + userAge);    return userAge;  }}

修改git仓库中的配置,进行手动更新,post请求

http://127.0.0.1:8801/actuator/refresh  启动刷新器 从cofnig_server读取

2. 使用消息总线 bus 更新

1. 添加依赖信息

在 config_server,config_client 中添加 

    <dependency>      <groupId>org.springframework.cloud</groupId>      <artifactId>spring-cloud-starter-bus-amqp</artifactId>    </dependency>    <!-- actuator监控中心 -->    <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-actuator</artifactId>    </dependency>

2. 配置文件修改

添加对 rabbbitMQ的配置, rabbitMQ服务和config_server,config_client 在一个服务器上,使用默认配置即可

### rabbitmq 配置信息 rabbitmq:  addresses: 47.105.86.222  username: guest  password: guest  port: 5672  virtual-host: /

3. 刷新

http://127.0.0.1:8801/actuator/bus-refresh   post方式

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持武林网。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表