本地(更新需重启):
需要创建两个项目一个配置中心config-server,一个获取配置的服务config-client
CONFIGE-SERVER:
1、创建springBoot项目 加上 config server 依赖
2、启动类上加@EnableConfigServer注解,开启配置中心服务
3、yml配置
server:
port: 9090
spring:
cloud:
config:
server:
native:
search-locations: classpath:shared #默认查找classpath下shared文件夹中的配置文件,先创建shared文件夹
profiles:
active: native
4、shared文件夹下配置文件
文件名config-local-client-dev.yml:
server:
port: 9092
myname: zhangsan
此文件中的 myname属性可以用来测试配置是否被配置中心更改。写一个controller使用@value和@RestController,直接访问http:localhost:9092查看属性值
CONFIG-CLIENT:
1、将application.properties改为bootstrap.yml
2、yml配置
spring:
application:
name: config-local-client
cloud:
config:
uri: http://localhost:9090
fail-fast: true
profiles:
active: dev
config-server项目启动,再将config-client项目启动,启动时,config-server根据配置文件去 http://localhost:9090查找名字为自身的spring.application.name(config-local-client)
的后缀为dev的配置文件(即为config-local-client-dev.yml),在shared文件夹中找到,加载配置。
远端情况(git,更新需重启):
两个项目,配置中心为config-remote-server,以下简称server服务,获取配置的服务config-remote-client,以下简称client服务。
CONFIGE-REMOTE-SERVER:
与本地情况一样,只是将CONFIG-SERVER中spring.cloud.config配置为在本地查找的改为在git拉取。
1、创建springBoot项目 加上config server 依赖
2、启动类上加@EnableConfigServer注解,开启配置中心服务
3、yml配置
server:
port: 9090
spring:
application:
name: config-remote-server
cloud:
config:
server:
git:
uri: #git地址
search-paths: #文件路径
4、git文件
server:
port: 9092
myname: lisi
CONFIG-REMOTE-CLIENT:
1、创建springBoot项目
2、将application.properties改为bootstrap.yml
这里为区别本地Demo,仅仅将应用名修改,yml内容为:
spring:
application:
name: config-remote-client
cloud:
config:
uri: http://localhost:9090
fail-fast: true
profiles:
active: dev
先将server项目启动,再将client项目启动,启动时,config-remote-client根据配置文件去 http://localhost:9090查找名字为自身的spring.application.name(config-remote-client)
的后缀为dev的配置文件(即为config-remoto-client-dev.yml),server服务根据配置git的地址(uri)以及目录( search-paths)查找配置,加载配置。