一、代码示例
说明:此处使用的SpringBoot版本为2.1.13.RELEASE,SpringCloud版本为Greenwich.SR5
前面介绍了SpringCloud Config的Server端和Client端,通过某以微服务实现刷新配置,但是实际中微服务有但多,我们不可能每一个都去刷新。下面我们将Config添加到Eureka Server并使用SpringCloud Bus进行刷新。
使用SpringCloud Bus需要使用RabbitMQ或kafka,此文使用的是RabbitMQ。安装方式参照:https://www.cnblogs.com/nongzihong/p/11578255.html
注意Erlang和RabbitMQ的版本一定要一致,不然会很坑。。。
本文的github地址为https://github.com/panli1988/config-server
此例读取的配置文件为/cloud02下的
此处我们创建三个工程
image.png
具体代码如下:
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:
#要不要去注册中心获取其他服务的地址
fetch-registry: false
#自己就是注册中心,不用注册自己
register-with-eureka: false
service-url:
defaultZone: http://localhost:7001/eureka
instance:
hostname: localhost
instance-id: eureka-server
management:
endpoints:
web:
exposure:
include: "*"
3、启动类
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、启动类
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
#环境
profile: dev
uri: http://localhost:7001/config
3、启动类
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
#环境
profile: dev
uri: http://localhost:7001/config
4、启动类
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;
}
}
其实provider-8001和provider-8002区别很小,此处可以不贴代码,为了完整性还是贴一些,且方便后续测试。
2、测试验证
先启动RabbitMQ,再一次启动eureka-server-7001、provider-8001、provider-8002。启动后测试下服务是否正常,不然就笑了。
先访问下http://localhost:7001/
再访问http://localhost:8001/hello/zs
http://localhost:8001/hello/zs
image.png
还有http://localhost:8002/hello/zs
image.png
以上服务均正常。
下面测试下获取的配置:
image.png
image.png
此处读取了spring.application.name和text两个属性,均正常获取。
特别说明一下:provider-8001和provider-8002分别读取的是provider-profile.yml和provider2-profile.yml,所以我们要修改两个配置文件,此处我们修改text属性:
image.png
image.png
修改后同样需要手动刷新,以post方式访问http://localhost:7001/actuator/bus-refresh,还是用postman进行
image.png
再次访问http://localhost:8001/name
和http://localhost:8002/name
image.png
image.png
provider-80001和provider-8002配置均已更新。
每次手动刷新比较麻烦,github支持Webhooks方式在每次commit时去通知SpringCloud去刷新配置。
image.png
本人没有试过,感兴趣的可以自己试下。
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/
还有尚硅谷周阳老师的视频
网友评论