一、代碼示例
說明:此處使用的SpringBoot版本為2.1.13.RELEASE,SpringCloud版本為Greenwich.SR5
前面介紹了SpringCloud Config的Server端和Client端,通過某以微服務(wù)實(shí)現(xiàn)刷新配置,但是實(shí)際中微服務(wù)有但多,我們不可能每一個(gè)都去刷新。下面我們將Config添加到Eureka Server并使用SpringCloud Bus進(jìn)行刷新。
使用SpringCloud Bus需要使用RabbitMQ或kafka,此文使用的是RabbitMQ。安裝方式參照:https://www.cnblogs.com/nongzihong/p/11578255.html
注意Erlang和RabbitMQ的版本一定要一致,不然會(huì)很坑。。。
本文的github地址為https://github.com/panli1988/config-server
此例讀取的配置文件為/cloud02下的

此處我們創(chuàng)建三個(gè)工程

具體代碼如下:
parent.pom
<?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>org.example</groupId>
<artifactId>cloud02</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>eureka-server-7001</module>
<module>provider-8001</module>
<module>provider-8002</module>
</modules>
<packaging>pom</packaging>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR5</spring-cloud.version>
<spring-boot.version>2.1.13.RELEASE</spring-boot.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<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>
<finalName>cloud02</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<delimiters>
<delimit>$</delimit>
</delimiters>
</configuration>
</plugin>
</plugins>
</build>
</project>
eureka-server-7001
1、maven依賴
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
</dependencies>
2、application.yml
server:
port: 7001
spring:
application:
name: eureka-server
cloud:
config:
server:
git:
search-paths: /cloud02
username:
password:
default-label: master
uri: https://github.com/panli1988/config-server
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
eureka:
client:
#要不要去注冊(cè)中心獲取其他服務(wù)的地址
fetch-registry: false
#自己就是注冊(cè)中心,不用注冊(cè)自己
register-with-eureka: false
service-url:
defaultZone: http://localhost:7001/eureka
instance:
hostname: localhost
instance-id: eureka-server
management:
endpoints:
web:
exposure:
include: "*"
3、啟動(dòng)類
package org.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
@EnableConfigServer
public class EurekaServer7001Application {
public static void main(String[] args) {
SpringApplication.run(EurekaServer7001Application.class,args);
}
}
provider-8001
1、maven依賴
2、application.yml
3、啟動(dòng)類
4、其他類
provider-8002
1、maven依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
</dependencies>
2、application.yml
#info信息
info:
app:
name: provider-8001
company:
name: www.xxx.com
build:
artifactId: ${project.artifactId}
version: ${project.version}
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
3、bootstrap.yml
spring:
application:
#要讀取的配置文件:如git上的文件為provider2-dev.yml
name: provider
cloud:
config:
label: master
#環(huán)境
profile: dev
uri: http://localhost:7001/config
3、啟動(dòng)類
package org.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Provider8001Application {
public static void main(String[] args) {
SpringApplication.run(Provider8001Application.class,args);
}
}
4、其他類
controller類
package org.example.controller;
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.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RefreshScope
public class ProviderController {
@Value("${spring.application.name}")
public String name;
@Value("${text}")
public String text;
@GetMapping("/hello/{name}")
public String hello(@PathVariable("name") String name){
return "hello,"+name;
}
@GetMapping("/name")
public String name(){
return name+"===="+text;
}
}
provider-8002
1、maven依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
</dependencies>
2、application.yml
#info信息
info:
app:
name: provider-8002
company:
name: www.xxx.com
build:
artifactId: ${project.artifactId}
version: ${project.version}
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
3、bootstrap.yml
spring:
application:
#要讀取的配置文件:如git上的文件為provider2-dev.yml
name: provider2
cloud:
config:
label: master
#環(huán)境
profile: dev
uri: http://localhost:7001/config
4、啟動(dòng)類
package org.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Provider8002Application {
public static void main(String[] args) {
SpringApplication.run(Provider8002Application.class,args);
}
}
5、其他類
package org.example.controller;
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.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RefreshScope
public class ProviderController {
@Value("${spring.application.name}")
public String name;
@Value("${text}")
public String text;
@GetMapping("/hello/{name}")
public String hello(@PathVariable("name") String name){
return "hello2,"+name;
}
@GetMapping("/name")
public String name(){
return name+"===="+text;
}
}
其實(shí)provider-8001和provider-8002區(qū)別很小,此處可以不貼代碼,為了完整性還是貼一些,且方便后續(xù)測(cè)試。
2、測(cè)試驗(yàn)證
先啟動(dòng)RabbitMQ,再一次啟動(dòng)eureka-server-7001、provider-8001、provider-8002。啟動(dòng)后測(cè)試下服務(wù)是否正常,不然就笑了。
先訪問下http://localhost:7001/

再訪問http://localhost:8001/hello/zs
http://localhost:8001/hello/zs

還有http://localhost:8002/hello/zs

以上服務(wù)均正常。
下面測(cè)試下獲取的配置:


此處讀取了spring.application.name和text兩個(gè)屬性,均正常獲取。
特別說明一下:provider-8001和provider-8002分別讀取的是provider-profile.yml和provider2-profile.yml,所以我們要修改兩個(gè)配置文件,此處我們修改text屬性:


修改后同樣需要手動(dòng)刷新,以post方式訪問http://localhost:7001/actuator/bus-refresh,還是用postman進(jìn)行

再次訪問http://localhost:8001/name
和http://localhost:8002/name


provider-80001和provider-8002配置均已更新。
每次手動(dòng)刷新比較麻煩,github支持Webhooks方式在每次commit時(shí)去通知SpringCloud去刷新配置。

本人沒有試過,感興趣的可以自己試下。
github:
https://github.com/panli1988/cloud01
https://github.com/panli1988/cloud02
參考:
https://blog.csdn.net/forezp/article/details/70148833
http://www.itmuch.com/spring-cloud/spring-cloud-index/
還有尚硅谷周陽(yáng)老師的視頻