<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
一样在启动项中须要添加注解 @EnableConfigServer 标记为服务配置中心git
@SpringBootApplication @EnableConfigServer public class ConfigserviceApplication { public static void main(String[] args) { SpringApplication.run(ConfigserviceApplication.class, args); } }
修改配置文件web
spring.cloud.config.server.git.uri=https://XXXX/Text/ spring.cloud.config.server.git.searchPaths=respo spring.cloud.config.label=master spring.application.name=config-server server.port=1006
spring cloud config 服务支持git仓库url读取配置spring
spring.cloud.config.server.git.url 指向githu配置仓库地址bootstrap
spring.cloud.config.servier.git.searchpaths 设置搜索配置文件目录,能够同时指定多个固定路径,或者/**等匹配符服务器
spring.cloud.config.label 配置仓库的分支app
spring.cloud.config.servier.git.username 配置git访问的用户名 (若是配置仓库是公开项目,就不须要配置)运维
spring.cloud.config.servier.git.password 配置git访问的密码(若是配置仓库是公开项目,就不须要配置)分布式
新建两个配置文件,内容以下spring-boot
testconfgi = version 1.1.1 democonfigclient.message=hello spring io
testconfgi = version 2.2.2 democonfigclient.message=hello spring io
请求资源文件格式以下:微服务
根据规则,定义配置文件,将配置push到远程的git仓库中
而后启动config-service项目,访问配置资源 http://localhost:1006/config-client-dev.properties 页面打印dev文件的配置,说明配置读取成功
democonfigclient.message: hello spring io testconfgi: version 1.1.1
能够修改路径http://localhost:1006/config-client-pro.properties 打印pro配置
democonfigclient.message: hello spring io testconfgi: version 2.2.2
测试各个配置文件是否能够正常使用只须要在http://localhost:1006/配置文件名称 (包含文件后缀)就能够查看
confg客户端
新建一个configclient项目一样先添加引用,须要注意client这里添加的是spring-cloud-starter-config包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
新建bootstrap.properties配置文件,boostrap 由父 ApplicationContext 加载,比 applicaton 优先加载
spring.application.name=config-client spring.cloud.config.label=master spring.cloud.config.profile=pro spring.cloud.config.uri= http://localhost:1006/ server.port=1007
spring.cloud.config.uri 设置config服务端
spring.cloud.config.profile 设置加载环境
spring.cloud.config.label 设置配置中心的分支
改造一下启动项将配置打印出来
@Value("${testconfgi}") String testconfgi; @Value("${democonfigclient.message}") String message; @RequestMapping(value = "/hi") public String hi(){ return testconfgi+" "+message; }
客户端切换配置分支时只须要修改 spring.cloud.config.profile 的值便可