一个完整的配置服务方案包含服务端和客户端两部分。
//TODO: 例子未完
#Server端
此模块就是一个例子。
增加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>增加如下内容
spring:
# 使用文件配置
profiles.active: native
cloud.config.server.native.search-locations: /web/config
# 使用gitserver内配置
# cloud:
# config:
# server:
# git:
# uri: file://web/config-repo
# uri: http://127.0.0.1:8071可以在 /web/config 目录下放置如下命名的配置文件
application.yml
application-{profile}.yml
{application-name}.yml
{application-name}-{profile}.yml
增加注解
@EnableConfigServer可以通过浏览器访问config-server的url来测试
http://config-server/{application}/{profile}[/{label}]
http://config-server/{application}-{profile}.yml
http://config-server/{label}/{application}-{profile}.yml
http://config-server/{application}-{profile}.properties
http://config-server/{label}/{application}-{profile}.properties
#Client端
增加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>在src/main/resources 目录下创建 bootstrap.yml 文件,内容如下:
spring:
application:
name: configclient # 此客户端名字
cloud:
config:
uri: http://127.0.0.1:8071 # config server的url客户端会使用服务端的 configclient-{profile}.yml
bootstrap.yml 不能像application.yml那样分别使用 application-prod.yml application-dev.yml,如果服务器上部署时需要的congfig-server和本地不同,可以在命令行指定,例如:
java -jar /web/webapps/xxx-api-1.0.0.jar --spring.profiles.active=prod --spring.cloud.config.uri=http://127.0.0.1:8888 --spring.cloud.config.profile=prod从congif server获取的配置信息优先级高于本地的application.yml文件
config server内的配置发生改变时,config client并不会自动刷新。如果需要自动刷新并应用新配置,需要和actuator 一起使用,具体参照这里