Spring Cloud Bus 将分布式的节点用轻量的消息代理连接起来。

它可以用于

  • 广播配置文件的更改
  • 者服务之间的通讯
  • 用于监控。

本文要讲述的是用Spring Cloud Bus实现通知微服务架构的配置文件的更改。

一、准备工作

本文还是基于上一篇文章来实现。按照官方文档,我们只需要在配置文件中配置 spring-cloud-starter-bus-amqp ;这就是说我们需要装rabbitMq,点击rabbitmq下载。至于怎么使用 rabbitmq,搜索引擎下。

也可以参考我这篇centos7安装rabbitmq

二、改造config-client

在pom文件加上起步依赖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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
 
    <groupId>com.forezp</groupId> 
    <artifactId>config-client</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <name>config-client</name> 
    <description>Demo project for Spring Boot</description> 
    <parent> 
        <groupId>com.forezp</groupId> 
        <artifactId>sc-f-chapter8</artifactId> 
        <version>1.0-SNAPSHOT</version> 
    </parent> 
 
    <dependencies> 
        <dependency> 
            <groupId>org.springframework.cloud</groupId> 
            <artifactId>spring-cloud-starter-config</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-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> 
 
    </dependencies> 
 
    <build> 
        <plugins> 
            <plugin> 
                <groupId>org.springframework.boot</groupId> 
                <artifactId>spring-boot-maven-plugin</artifactId> 
            </plugin> 
        </plugins> 
    </build> 
 
 
</project>

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

spring.rabbitmq.host=你的地址 
spring.rabbitmq.port=5672 
spring.rabbitmq.username=你的名字 
spring.rabbitmq.password=你的密码 
 
spring.cloud.bus.enabled=true 
spring.cloud.bus.trace.enabled=true 
management.endpoints.web.exposure.include=bus-refresh

ConfigClientApplication启动类代码如下:

package com.forezp.configclient; 
 
import org.springframework.beans.factory.annotation.Value; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 
import org.springframework.cloud.context.config.annotation.RefreshScope; 
import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RestController; 
 
@SpringBootApplication 
@RestController 
@EnableDiscoveryClient 
@EnableEurekaClient 
@RefreshScope 
public class ConfigClientApplication { 
 
    public static void main(String[] args) { 
        SpringApplication.run(ConfigClientApplication.class, args); 
    } 
    @Value("${foo}") 
    String foo; 
 
    @RequestMapping(value = "/hi") 
    public String hi(){ 
        return "config is:"+foo; 
    } 
 
 
 
}

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

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

foo version 3

这时我们去代码仓库将foo的值改为“foo version 4”,即改变配置文件foo的值。如果是传统的做法,需要重启服务,才能达到配置文件的更新。此时,我们只需要发送post请求:http://localhost:8881/actuator/bus-refresh,你会发现config-client会重新读取配置文件,重新读取配置文件:

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

foo version 4

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

三、分析

当git文件更改的时候,通过pc端用post 向端口为8882的config-client发送请求/bus/refresh/;此时8882端口会发送一个消息,由消息总线向其他服务传递,从而使整个微服务集群都达到更新配置文件。

源码:IT虾米网


评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

SpringCloud教程第7篇:高可用的分布式配置中心(F版本)