SpringCloud Bus 消息总线 SpringCloud Bus 消息总线的具体使用

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

SpringCloud Bus 消息总线 SpringCloud Bus 消息总线的具体使用

郭鹏飞   2021-03-30 我要评论

什么是消息总线

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
@EnableConfigServer
public 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_server
management:
 endpoints:
  web:
   exposure:
    include: "*"

3. 启动

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class App {
  public static void main(String[] args) {
    SpringApplication.run(App.class,args);
  }
}
/**
 * 在读取配置文件信息的地方进行添加 @RefreshScope 注解
 */
@RestController
@RefreshScope
public 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方式

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们