1 SpringCloudBus简介

如果我们更新码云中的配置文件,那客户端工程是否可以及时接受新的配置信息呢?我们现在来做有一个测试,修改一下码云中的配置文件中mysql的端口 ,然后测试http://localhost:9001/label 数据依然可以查询出来,证明修改服务器中的配置并没有更新立刻到工程,只有重新启动程序才会读取配置。 那我们如果想在不重启微服务的情况下更新配置如何来实现呢? 我们使用SpringCloudBus来实现配置的自动更新。也就是说spring cloud config集中配置需要结合消息总线SpringCloudBus一起才能使用
Snipaste_2021-02-03_13-46-06.png

2 消息总线入门

2.1 配置服务端

(1)修改tensquare_config(集中配置)工程的pom.xml,引用依赖

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring‐cloud‐bus</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.cloud</groupId>
  7. <artifactId>spring‐cloud‐stream‐binder‐rabbit</artifactId>
  8. </dependency>

(2)修改application.yml ,添加配置

rabbitmq:
    host: 192.168.184.135
management: #暴露触发消息总线的地址
  endpoints:
    web:
      exposure:
        include: bus‐refresh

2.2 配置客户端

以基础模块为例,加入消息总线
(1)修改tensquare_base工程 ,引入依赖

 <!--springCloudBus消息总线-->
<dependency>    
    <groupId>org.springframework.cloud</groupId>        
    <artifactId>spring‐cloud‐bus</artifactId>        
  </dependency> 
<!--rabbitMq-->
 <dependency>    
    <groupId>org.springframework.cloud</groupId>        
    <artifactId>spring‐cloud‐stream‐binder‐rabbit</artifactId>        
 </dependency>
<!--springCloud消息总线,rabbit监听-->
 <dependency>    
    <groupId>org.springframework.boot</groupId>        
    <artifactId>spring‐boot‐starter‐actuator</artifactId>        
 </dependency>

(2)在码云的配置文件中配置rabbitMQ的地址:

 rabbitmq:
    host: 192.168.184.135

修改后

server:
  port: 9001
spring:
  application:
    name: tensquare-base
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://192.168.108.140:3306/tensquare_base?characterEncoding=UTF8
    username: root
    password: root
  jpa:
    database: mysql
    show-sql: true
    generate-ddl: true
  rabbitmq:
    host: 192.168.108.140 # 消息总线监听rabbitMQ的地址
eureka:
  client:
    service-url:
      defaultZone: http://192.168.108.140:6868/eureka/
  instance:
    prefer-ip-address: true
sms: # 自定义配置
  ip: 192.168.108.140

(2)启动tensquare_eureka 、tensquare_config和tensquare_base 看是否正常运行
(3)修改码云上的配置文件 ,将数据库连接IP 改为127.0.0.1 ,在本地部署一份数据库。
(4)postman测试 Url: http://127.0.0.1:12000/actuator/bus-refresh Method:post
(5)再次观察输出的数据是否是读取了本地的mysql数据。

2.3 自定义配置的读取

(1)修改码云上的配置文件,增加自定义配置

sms:
  ip: 127.0.0.1

(2)在tensquare_base工程中新建controller

package com.tensquare.base.controller;

import com.tensquare.base.pojo.Label;
import com.tensquare.base.service.LabelService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: Luck-zb
 * description:标签处理器
 * <p>
 * 当使用消息总线更新配置文件的时候,如果是框架内部自己的配置文件属性,比如server.port以及一些数据库
 * 的配置信息,通过消息总线更新后能获取到最新数据,但是想要获取到最新的自定义配置文件属性,除了正常
 * 的@value注解之外,还需要使用@ReFreshScope注解来实现
 * <p>
 * <p>
 * Date:2021/1/23 - 14:15
 */
@RestController
@CrossOrigin // 跨域
@RequestMapping("/label")
@RefreshScope  # 集中配置和消息总线使用时,修改配置文件后能够获取到自定义配置信息
public class LabelController {

    private static Logger logger = LoggerFactory.getLogger(LabelController.class);

    @Autowired
    LabelService labelService;

    @Value("${sms.ip}")
    private String ip;

/**
     * 根据id查询
     *
     * @param labelId
     * @return
     */
    @RequestMapping(value = "/{labelId}", method = RequestMethod.GET)
    public Result findById(@PathVariable String labelId) {

        // 测试消息总线,更新自定义的配置文件属性
        if (logger.isInfoEnabled()) {
            logger.info("获取到自定义的配置文件属性ip:[{}]", ip);
        }
        Label label = labelService.findById(labelId);
        return new Result(true, StatusCode.OK, "查询成功!", label);
    }
}

(3)运行测试看是否能够读取配置信息 ,sms.ip
(4)修改码云上的配置文件中的自定义配置
(5)通过postman测试 Url: http://127.0.0.1:12000/actuator/bus-refresh Method:post
测试后观察,发现并没有更新信息。这是因为我们的 controller少了一个注解@RefreshScope 此注解用于刷新配置
添加后再次进行测试。