1、简介

  Spring Cloud Bus 将分布式的节点用轻量的消息代理连接起来。它可以用于广播配置文件的更改或者服务之间的通讯,也可以用于监控。本文要讲述的是用Spring Cloud Bus实现通知微服务架构的配置文件的更改。

Spring Cloud Bus 可选的消息代理组建包括 RabbitMQ 、 AMQP 和Kafka 等。本节 讲述的是用 RabbitMQ 作为 Spring Cloud 的消息组件去刷新更改微服务的配置文件。 因此需要安装RabbitMQ,点击RabbitMQ下载。安装和使用方式请自行搜索。

2、改造config-client

  1、此功能只需要改造config-client工程,首先在pom.xml中引入基于rabbitMQ实现的 spring cloud bus的起步依赖spring-cloud-starter-bus-amqp,代码如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.lishun</groupId>
    <artifactId>config-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>config-client</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.lishun</groupId>
        <artifactId>cloud</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>


    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
    </dependencies>


</project>

  2、在配置文件bootstrap.properties中加上RabbitMq的配置,包括RabbitMq的地址、端口,用户名、密码。并需要加上spring.cloud.bus的三个配置,具体如下:

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672 
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

spring.cloud.bus.enabled=true
spring.cloud.bus.trace.enabled=true
management.endpoints.web.exposure.include=bus-refresh

  3、最后 需要在更新的配置类上加@ RefreshScope 注解 只有加上了该注解,才会在不重启服务的情况下更新配置 ,代码如下

@SpringBootApplication
@RestController
@RefreshScope
public class ConfigClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }


    @Value("${id}")
    String id;
    @RequestMapping(value = "/hi")
    public String hi(){
        return id;
    }
}

  4、依次启动eureka-server、confg-cserver,启动两个config-client,端口为:8881、8882。

  5、访问http://localhost:8881/hi 或者http://localhost:8882/hi 浏览器显示:

config-test

  6、将github上的配置文件id的值改为config-test-11,如果是传统的做法,需要重启服务,才能达到配置文件的更新。们只需要发送post请求:http://localhost:8881/actuator/bus-refresh,在控制台你会发现config-client会重新读取配置文件。

  7、这时我们再访问http://localhost:8881/hi 或者http://localhost:8882/hi 浏览器显示:

config-test-11

  /actuator/bus-refresh接口可以指定服务,即使用”destination”参数,比如 “/actuator/bus-refresh?destination=customers:**” 即刷新服务名为customers的所有服务。

posted on 2018-08-20 21:57  顺蝈蝈  阅读(301)  评论(0编辑  收藏  举报