本例重新创建项目,构建一个空的mavan工程。
一、Config Server 从本地读取配置文件
新建一个moudle config_server ,pom添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
启动类添加 @EnableConfigServer 依赖开启配置服务功能,application.yaml添加配置如下:
server:
port: 9001
spring:
profiles:
active: native #从本地读取配置文件
cloud:
config:
server:
native:
search-locations: classpath:/shared #读取classpath下shared目录下的配置
在resource目录下新建shared目录,shared下新建 config-client-dev.yaml配置文件:
server:
port: 3000
foo: foo-version-v1
继续新建一个modulr config_client ,添加config 客户端依赖,如:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
添加配置文件application.yaml:
spring:
application:
name: config-client
cloud:
config:
uri: http://localhost:9001 #读取配置文件的config服务地址
fail-fast: true #读取未成功则快速失败
profiles:
active: dev #读取dev配置文件
启动类添加代码,获取服务端配置的foo属性:
@Value("${foo}")
String foo ;
@GetMapping("/foo")
public String foo(){
return " 读取的远程服务的配置文件foo:"+foo;
}
此时编码完成,启动config-server、config-client,然而启动config-client的时候控制台一直报错:
一直访问的是默认的端口,并没有使用我们配置的端口,说明配置文件没有被正常加载。
通过调研发现,config-client中配置文件会首先加载bootstrap.yaml,将配置文件application.yaml 修改为 bootstrap.yaml,重新启动发现正常,在浏览器访问:http://localhost:3000/foo
会看到浏览器输出我们在服务端配置的信息:
二、Config Server从Git仓库读取配置文件
spring cloud config 支持从远程Git仓库读取配置文件,既config server可以不从本地读取,而是从远程仓库读取,这样便于统一管理,并且可以通过 Spring Cloud Bus 在不人工启动的情况下对config client的配置进行刷新。
首先修改:ConfigServer的application.yaml
server:
port: 9001
spring:
# profiles:
# active: native #从本地读取配置文件
cloud:
config:
server:
# native:
# search-locations: classpath:/shared #读取classpath下shared目录下的配置
git:
uri: https://gitee.com/ssdate/spring-test #仓库地址
search-paths: study_0313_config/config_server/src/main/resources/shared #搜索远程仓库的文件夹地址
# username: #公开仓库不需要账户密码,私人仓库必要要账户密码
# password:
default-label: master #仓库分支名
重新启动两个客户端,在浏览器输入:http://localhost:3000/foo
能看到浏览器显示:读取的远程服务的配置文件foo:foo-version-v1
说明已经成功读取git上面的配置文件。
三、搭建高可用Config Server集群
当服务实例很多时,所有的服务同时从配置中心获取服务时,就可以将配置中心作为一个微服务,并将其集群化,达到高可用的效果。
1.新建一个eureka_server服务,端口9002。
2.修改config_server,添加eureka客户端配置依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.5.RELEASE</version>
</dependency>
开始eureka客户端功能 @EnableEurekaClient ,修改config-server的application.yaml 配置:
server:
port: 9001
spring:
# profiles:
# active: native #从本地读取配置文件
cloud:
config:
server:
# native:
# search-locations: classpath:/shared #读取classpath下shared目录下的配置
git:
uri: https://gitee.com/ssdate/spring-test #仓库地址
search-paths: study_0313_config/config_server/src/main/resources/shared #搜索远程仓库的文件夹地址
# username: #公开仓库不需要账户密码,私人仓库必要要账户密码
# password:
default-label: master #仓库分支名
application:
name: config-server
eureka:
client:
service-url:
defaultZone: http://eureka-server:9002/eureka
3.同上将config-client也作为eureka客户端进行改造 ,修改配置文件bootstrap.yaml:
spring:
application:
name: config-client
cloud:
config:
# uri: http://localhost:9001 #读取配置文件的config服务地址
fail-fast: true #读取未成功则快速失败
discovery:
enabled: true
service-id: config-server #向serviceId为config-server的服务读取配置
profiles:
active: dev #读取dev配置文件
eureka:
client:
service-url:
defaultZone: http://eureka-server:9002/eureka
依次启动eureka-sever、config-server、config-client ,浏览器访问:http://localhost:3000/foo
能看到浏览器显示:读取的远程服务的配置文件foo:foo-version-v1
说明已经通过service-id 正常访问到config-server服务。
如果是搭建高可用的configserver集群,我们只需要给config-server创建多个实例,配置不同的端口即可。
四、使用Spring Cloud Bus刷新配置
1.Spring Cloud Bus 是轻量的消息代理将分布式节点连接起来,可以用于广播配置文件的更改或服务的监控管理。一个关键的思想就是,消息总线可以为微服务做监控,也可以实现应用程序之间相互通信。 Spring Cloud Bus 可选的消息代理组建包括 RabbitMQ、 AMQP、 Kafka 等。本次是用 RabbitMQ 作为 Spring Cloud 的消息组件去刷新更改微服务的配置 文件。
2.在config-client的pom添加依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
添加注解 @RefreshScope 开启此功能。
修改config-client配置:
spring:
application:
name: config-client
cloud:
config:
# uri: http://localhost:9001 #读取配置文件的config服务地址
fail-fast: true #读取未成功则快速失败
discovery:
enabled: true
service-id: config-server #向serviceId为config-server的服务读取配置
profiles:
active: dev #读取dev配置文件
rabbitmq:
host: localhost #RabbitMQ服务器IP地址
port: 5672 #RabbitMQ服务器端口
username: guest
password: guest
management:
security:
enable: false #屏蔽安全验证:通过消息总线更改配置需要安全验证,这里先不进行验证
eureka:
client:
service-url:
defaultZone: http://eureka-server:9002/eureka
server:
port: 9003
重新启动config-client,浏览器访问:http://localhost:3000/foo
能看到浏览器显示:读取的远程服务的配置文件foo:foo-version-v1
此时更改远程配置文件:
刷新一下浏览器会看到配置没有变化。
此时执行:http://localhost:9003/bus/refresh
然后再次刷新http://localhost:3000/foo ,应该就会有所改变。
(这里没有完成实际验证,因为没有RabbitMQ 服务,后面空了尝试一下。)