Spring Cloud Config为Spring Cloud子项目,提供一套分布式的配置管理方案,『官方文档传送门』
分为服务端Config Server和客户端Config Client,其中Server端连接git或svn,并对外提供配置信息服务。Client端从Server端获取配置信息。当配置变更时,只需要借助git的push操作来触发更新操作。
git配置文件
首先需要在git上放置一些yml或properties文件,命名规则如下: application用于Client端连接时配置的application.name,用于过滤配置文件。profile用于Client端连接时配置的spring.cloud.config.profile,用于区分生产测试等不同环境。label一般都master,代表分支。
配置文件命名规则如下:
* /{application}/{profile}[/{label}]
* /{application}-{profile}.yml
* /{label}/{application}-{profile}.yml
* /{application}-{profile}.properties
* /{label}/{application}-{profile}.properties
按照以上规则,我们配置了3个yml文件如下(也可使用properties文件): 1、herr.yml
server.port: 8082
#远程配置,默认8082
profile: herr-default
2、herr-dev.yml
#server.port=8082
#不设置server.port,则以herr.yml的8082为default
profile: herr-dev
3、herr-pro.yml
server.port: 8086
profile: herr-pro
Server端搭建
1、pom.xml依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2、application入口类添加注解@EnableConfigServer
3、application.yml配置
server:
port: 8100
spring:
application:
name: config
http:
encoding:
charset: UTF-8
enabled: true
force: true
cloud:
config:
label: master #为默认分支
server:
git:
#用于连接本地gitlab,需要密码
#uri: http://127.0.0.1:10080/root/config.git
#username: root
#password: 12345678
#用于连接码云
uri: https://gitee.com/iherr/spring-cloud-trip-config.git
searchPaths: config-git-static #如配置文件不在根目录,则需要设置文件夹路径
security:
user:
password: 1a1a2b2b3c3c4d4d
#如需要密码,需要pom添加security依赖,否则不起作用
运行并测试 使用浏览器访问,查看数据是否正常,如有找不到的属性,会默认去{application}.properties或[.yml]找。
#以下url都可以访问,如启用了密码,会弹出页面需要输入密码,默认用户名为user:
http://localhost:8100/herr-pro.properties
http://localhost:8100/herr-dev.properties
http://localhost:8100/herr-pro.yml
http://localhost:8100/herr-dev.yml
http://localhost:8100/master/herr-dev.properties
Client端搭建
1、pom.xml依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
2、接口实现:profile接口,从配置文件中获取profile并显示
@RestController
public class HelloController {
@Value("${profile}")
private String profile;
@GetMapping("/profile")
public String getProfile(){
return this.profile;
}
}
3、提供默认application.yml文件
server:
port: 8080
profile: default
#如果不需要bootstrap.yml,可将spring.cloud.config.uri在EditConfiguration中override parameter中设置,打开注释设置其余参数
#spring:
# cloud:
# config:
# profile: dev #如不设置,则走default
# label: master
# name: herr
4、提供初始化文件bootstrap.yml(名称不能改,默认),必须要这个文件,不然运行初始化时找不到server地址,会使用默认8888端口来获取server。
如省略掉该环节,会发现启动Client模块出现错误,报错信息为:Fetching config from server at: http://localhost:8888 等。SpringCloud里面有个“启动上下文”,主要是用于加载远端的配置,也就是加载Server里面的配置,默认加载顺序为:加载bootstrap.yml里面的配置 --> 链接Server,加载远程配置 --> 加载application.yml里面的配置;
spring:
cloud:
config:
#uri: http://127.0.0.1:8100
uri: http://user:1a1a2b2b3c3c4d4d@127.0.0.1:8100
#区分是否输入密码,密码为server端配置
profile: pro #如不设置,则走default
label: master
name: herr #取 herr-dev.yml 这个文件的 application 名字,即为 herr 名称
# application: #application.name和spring.cloud.config.name字段相同
# name: herr
如确实不想要bootstrap.yml文件,可将spring.cloud.config.uri在EditConfiguration中override parameter中设置,或者通过代码在入口main函数中代码设置。
public static void main(String[] args) {
if (StringUtils.isBlank(System.getProperty("spring.cloud.config.uri"))) {
System.setProperty("spring.cloud.config.uri", "http://user:1a1a2b2b3c3c4d4d@127.0.0.1:8100");
}
System.out.println(System.getProperty("spring.cloud.config.uri"));
SpringApplication.run(ConfigClientApplication.class, args);
}
5、运行并测试
由于配置的application.name为herr,profile为pro,因此会自动先去找herr-pro.yml[.properties]文件,查到server.port为8086,profile为herr-pro,因此访问的地址为:http://localhost:8086/profile 返回的文字为herr-pro。可修改这两个参数做其他验证。
额外说明
哪怕第4步项目application.yml有server.port为8082,profile为 test1。在Server服务启动的时候,bootstrap拿到远端配置注入到profile的属性中的话,那么就不会再次覆盖这个属性了,所以只会选择远端配置的内容。
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。