温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringCloud Bus消息总线怎么用

发布时间:2021-08-09 11:11:44 来源:亿速云 阅读:113 作者:小新 栏目:编程语言

这篇文章主要介绍了SpringCloud Bus消息总线怎么用,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

什么是消息总线

1. 概念

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

2. SpringCloud Bus

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

3. 其他

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

搭建分布式配置中心

1. Config 架构

SpringCloud Bus消息总线怎么用

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

2. Git 环境搭建

使用 码云 环境搭建 git

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

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

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

SpringCloud Bus消息总线怎么用

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方式

感谢你能够认真阅读完这篇文章,希望小编分享的“SpringCloud Bus消息总线怎么用”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI