SpringCloud Config 搭建分布式配置中心之第 2 篇 —— 基于消息总线 Bus 实时刷新配置(完结)

查看之前的博客可以点击顶部的【分类专栏】

基于第一篇博客:https://blog.csdn.net/BiandanLoveyou/article/details/117737490

 

什么是消息总线?

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

简单来说:消息总线其实通过消息中间主题模式,使用广播消息的机制被所有在注册中心微服务实例进行监听和消费。以广播形式将消息推送给所有注册中心服务列表。只需要手动刷新其中一台服务器,就会把刷新的通知发送到消息队列的交换机,交换机就把消息广播到微服务架构里的所有的集群服务(相同的微服务名,使用相同的配置文件),然后去获取最新的数据存入到服务的 JVM 中。

目前 SpringCloud 仅支持 RabbitMQ 和 Kafka。本文采用RabbitMQ实现这一功能。

 

先下载本篇博客代码:https://pan.baidu.com/s/1KAxmCEZ9tA8ioQARfFQJNw 提取码:4f8b

 

首先,我们先去 Gitee 创建仓库,即配置文件 user-server-test.yml

 

然后,我们改造代码。

1、改造最外层 pom.xml 把 spring-boot-starter-actuator  spring-cloud-starter-bus-amqp 放到最外部依赖,同时还要增加一个 parent 依赖,否则会引起包冲突的问题,无法使用 bus。

        <!-- spring-boot-starter-actuator 监控 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- 消息总线 bus 核心jar包 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>


    <dependencyManagement>
            <!-- bus-parent  -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-bus-parent</artifactId>
                <version>2.0.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

 

2、修改 config-server 的 bootstrap.yml 配置,开启 bus 刷新,完整配置如下:

server:
  port: 9001

eureka:
  instance:
    hostname: 127.0.0.1
  client:
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:8080/eureka/

spring:
  application:
    name: config-server

  cloud:
    config:
      server:
        git:
          # git 环境地址
          uri: https://gitee.com/biandanLoveyou/config.git
          # 搜索目录
          search-paths:
            - myConfig

      # 读取的分支 master是主分支
      label: master
      
# 开启 bus 刷新
management:
  endpoints:
    web:
      exposure:
        include: bus-refresh 

说明:

spring-cloud-starter-bus-amqp 默认整合了 RabbitMQ,如果我们没有在 yml 文件里显示的配置 RabbitMQ 的信息,会默认取我们本地 127.0.0.1 的信息。http://127.0.0.1:15672/   默认的用户名和密码都是:guest

我们也可以指定其它的信息。这里提供一段 RabbitMQ 的配置信息。

spring:
  rabbitmq:
    #主机名
    host: 127.0.0.1
    #端口号
    port: 5672
    #账号
    username: guest
    #密码
    password: guest
    #虚拟主机,这里配置的是我们的测试主机
    virtual-host: /test_host

 

 

这时候,启动注册中心和配置中心,浏览器地址输入:http://127.0.0.1:9001/user-server-test.yml

 

OK,配置中心已经可以读取到我们在 Gitee 上面的内容了。

这时候,我们打开我们 RabbitMQ 的管理后台:http://127.0.0.1:15672/

登录后,我们发现在交换机一栏、队列一栏,SpringCloud 默认给我们创建了交换机和队列。使用的 / 虚拟主机(virtual hosts)。

不懂 RabbitMQ?查看系列博客:https://blog.csdn.net/biandanloveyou/category_10893395.html

 

3、我们修改 user-server 的实体类

package com.study.entity;

import lombok.Data;
import lombok.ToString;

/**
 * @author biandan
 * @description
 * @signature 让天下没有难写的代码
 * @create 2021-06-06 下午 5:45
 */
@Data
@ToString
public class UserEntity {

    //用户ID
    private Integer userId;

    //用户名称
    private String userName;

    //端口号
    private Integer port;
}

修改 controller 类:

package com.study.controller;

import com.study.entity.UserEntity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author biandan
 * @description
 * @signature 让天下没有难写的代码
 * @create 2021-06-07 下午 1:25
 */
@RestController
@RefreshScope //完成 Bean 的热加载
public class UserController {

    @Value("${myconfig.userid}")
    private Integer userId;

    @Value("${myconfig.username}")
    private String userName;

    @Value("${server.port}")
    private Integer port;

    @GetMapping("/getInfo")
    public UserEntity getInfo(){
        UserEntity userEntity = new UserEntity();
        userEntity.setUserId(userId);
        userEntity.setUserName(userName);
        userEntity.setPort(port);
        System.out.println(userEntity.toString());
        return userEntity;
    }
}

修改 bootstrap.yml 配置。把 profile 改成 test,把端点的刷新改为 bus-refresh

server:
  port: 80

eureka:
  instance:
    hostname: 127.0.0.1
  client:
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:8080/eureka/

spring:
  application:
    name: user-server

  cloud:
    config:
      # 读取 configServer 信息
      discovery:
        service-id: config-server
        enabled: true
      # 读取 gitee 文件的后缀
      profile: test

# 开启监控端点
management:
  endpoints:
    web:
      exposure:
        include: bus-refresh

# 自定义配置信息
myconfig:
  userid: 1
  username: 代码配置的名字-user-server

 

4、然后启动用户服务 user-server、然后修改端口号 81,启动第二个。不懂在 IDEA 里启动多个微服务?查看博客:https://blog.csdn.net/BiandanLoveyou/article/details/103746170

这时候,我们看到 RabbitMQ 已经帮我们绑定了几个微服务

 

浏览器地址输入(用户服务 80 端口):http://127.0.0.1/getInfo

 

浏览器地址输入(用户服务 81 端口):http://127.0.0.1:81/getInfo

 

说明已经获取到了 Gitee 上面最新的配置。

这时候,我们去 Gitee 上修改:

 

然后我们调用【用户服务80端口】或者【用户服务81】的:/actuator/bus-refresh  接口,只需要调用其中一个微服务的刷新接口就好了。

比如我调用【用户服务80】接口:http://127.0.0.1/actuator/bus-refresh

 

如果你手速快,还可以看到 RabbitMQ 管理后台有待消费的消息。

 

然后,浏览器访问:http://127.0.0.1/getInfo   (发现我们的端口号变 81 了,因为我们部署到同一台机器,JVM 重新初始化的时候,把内存的端口号都改了。)

 

http://127.0.0.1:81/getInfo

OK,基于消息总线 Bus 实时刷新配置讲解到这。

 

本篇博客代码地址:https://pan.baidu.com/s/1KAxmCEZ9tA8ioQARfFQJNw 提取码:4f8b

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值