简介
Spring Cloud Bus 将分布式的节点用轻量的消息代理连接起来。它可以用于广播配置文件的更改或者服务之间的通讯,也可以用于监控。
本文要讲述的是用Spring Cloud Bus实现通知微服务架构的配置文件的更改。
前提
你的电脑的提前安装好RabbitMq
改造Ahut-Config-Client项目
项目pom文件添加依赖
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</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>
完整的pom.xml文件代码如下:
<?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.ahut</groupId>
<artifactId>config-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>config-client</name>
<description>config client</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.12.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Edgware.SR3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
在读取配置文件的类上面添加注解@RefreshScope:
package com.ahut.action;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author cheng
* @className: HelloAction
* @description: 演示获取配置文件
* @dateTime
@RestController
@RequestMapping(value = "/v1")
@RefreshScope
public class HelloAction
@Value(value = "${token}")
private String token;
/**
* @description: 获取token
* @author cheng
* @dateTime
@RequestMapping(value = "/token")
public String getToken() {
return
在配置文件bootstrap.properties中加上RabbitMq的配置,包括RabbitMq的地址、端口,用户名、密码,代码如下:
# 不需要验证
management.security.enabled=false
# 配置rabbitmq的主机名
spring.rabbitmq.host=localhost
# 配置rabbitmq的端口
spring.rabbitmq.port=5672
# 配置rabbitmq的用户名
spring.rabbitmq.username=guest
# 配置rabbitmq的密码
spring.rabbitmq.password=guest
创建Ahut-Config-Client-Two项目
这个项目和Ahut-Config-Client基本一模一样,唯一的区别是服务器的端口不同
bootstrap.properties中不同的点:
# 配置服务器端口
server.port=8980
开启RabbitMq服务
启动项目:
- Ahut-Eureka-Server(注册中心)
- Ahut-Config-Server(配置服务)
- Ahut-Config-Client(读取配置)
- Ahut-Config-Client-Two(读取配置)
访问eureka
此时访问
Ahut-Config-Client http://localhost:8970/v1/token
Ahut-Config-Client-Two http://localhost:8980/v1/token
修改github仓库中的属性
token=the token change to
此时访问
Ahut-Config-Client http://localhost:8970/v1/token
Ahut-Config-Client-Two http://localhost:8980/v1/token
页面没有任何变化
使用postman 发送如下请求
注意:
- 使用的是post方法
- 访问的是config client,而不是config server
- 另外,/bus/refresh接口可以指定服务,即使用”destination”参数,比如 “/bus/refresh?destination=customers:**” 即刷新服务名为customers的所有服务,不管ip
http://localhost:8970/bus/refresh
等同于
http://localhost:8970/bus/refresh?destination=AHUT-CONFIG-CLIENT:**
此时再次访问
Ahut-Config-Client http://localhost:8970/v1/token
Ahut-Config-Client-Two http://localhost:8980/v1/token
两个页面的属性都已经改变
此时的项目架构
当git文件更改的时候
通过pc端用post 向端口为8970的AHUT-CONFIG-CLIENT发送请求/bus/refresh