我们知道,微服务每个都是一个单体服务,我们的功能单元会拆分的很细,我们会有很多的微服务,这也就意味着我们将有n多的配置,如果不能将配置进行统一的管理将会是一件很可怕的事情,接下来我们就来搭建一个配置中心
idea搭建非常的快建议使用。
和注册中心一样配置中心也是分为server端和client端,server端负责从仓库拉取配置,client从server获取配置
上图
依赖
compile 'org.springframework.cloud:spring-cloud-config-server:2.1.3.RELEASE'
compile 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client:2.1.2.RELEASE'
我们需要引入config server以及eureka client
主类加注解
@SpringBootApplication
@EnableDiscoveryClient
public class GetawayApplication {
public static void main(String[] args) {
SpringApplication.run(GetawayApplication.class, args);
}
}
这里使用@EnableDiscoveryClient,使用@EnableEurekaClient有可能获取不到配置
配置
spring:
application:
name: admin-config
cloud:
config:
server:
git:
#git仓库地址
uri: xxx
#用户名
username: xxxx
#密码
password: 'xxx'
#配置文件搜索路径
search-paths: /**
eureka:
instance:
non-secure-port: ${server.port}
prefer-ip-address: true
instance-id: ${spring.application.name}:${server.port}
lease-renewal-interval-in-seconds: 10
client:
register-with-eureka: true
fetch-registry: true
serviceUrl:
defaultZone: http://regist.byb.com:38001/eureka/
server:
port: 38002
然后启动服务即可在注册中心看见配置中心微服务
客服端使用
- 同样也是加依赖
implementation('org.springframework.cloud:spring-cloud-starter-config')
注意这里我同样没有加版本,我的依赖版本统一由spring cloud管理。
- 然后删除原来的application.propertity或application.yaml的配置文件,或者将其移到static目录或者其他目录,别放在项目能读取到的目录就行,为什么呢?因为开发的时候,有时候难免还是需要本地配置测试,会比较方便。
- 然后添加bootstrap.yaml或者propertity配置文件,此配置文件的优先级高于application的配置文件
- bootstrap.yaml配置
spring:
application:
name: ts-log
cloud:
config:
discovery:
enabled: true
# 配置中心微服务名称
service-id: admin-config
# 仓库的分支
label: master
# 开发环境
profile: dev
eureka:
client:
service-url:
defaultZone: http://regist.byb.com:38001/eureka/
server:
port: 8080
- 然后在仓库加入你的微服务配置以微服务名称+开发环境命名,为了方便每个微服务都可以建一个目录,方便对不同环境进行管理
- 我的配置是存在git代码仓库里面的,非常方便管理,当然你也可以存在本地的某个文件夹或者其他地方,只需要修改config的配置即可。
然后启动项目,在启动图标下面即可看见服务是否拿到配置
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ //
// 佛祖保佑 永不宕机 永无BUG //
[INFO ] 2019-12-09 14:26:31,435 org.springframework.cloud.config.client.ConfigServicePropertySourceLocator org.springframework.cloud.config.client.ConfigServicePropertySourceLocator.getRemoteEnvironment(ConfigServicePropertySourceLocator.java:207) - Fetching config from server at : http://172.17.0.3:38002/
[INFO ] 2019-12-09 14:26:32,287 org.springframework.cloud.config.client.ConfigServicePropertySourceLocator org.springframework.cloud.config.client.ConfigServicePropertySourceLocator.log(ConfigServicePropertySourceLocator.java:151) - Located environment: name=ts-log, profiles=[dev], label=master, version=84b2687eddebd947f726a69683e7c35744053cad, state=null
[INFO ] 2019-12-09 14:26:32,291 org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration.initialize(PropertySourceBootstrapConfiguration.java:101) - Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='configClient'}, MapPropertySource {name='https://codehub.devcloud.huaweicloud.com/byb2000001/doc_configForTs.git/ts-log/ts-log-dev.yml'}, MapPropertySource {name='https://codehub.devcloud.huaweicloud.com/byb2000001/doc_configForTs.git/application-dev.yaml'}, MapPropertySource {name='https://codehub.devcloud.huaweicloud.com/byb2000001/doc_configForTs.git/application.yml'}]}
默认的启动图标是spring boot(我这里是使用了自己的图标),图标第一行我们可以看见配置中心的地址:http://172.17.0.3:38002/,如果这里显示的是localhost:8888那么证明你没有添加依赖n,默认从本地的8888端口获取配置。最后一行后面可以看见从配置中心获取到的所有配置。
- 微服务配置文件加载顺序
扫描的顺序,相同的配置,优先级低的覆盖优先级高的,相同配置文件yml格式会覆盖properties格式
- bootstrap.yml/bootstrap.properties
- application.yml/application.properties
- 同级的有application-dev.yml,application-test.yml,application-prod.yml等,使用需要在application中添加spring.profiles.active=xxx,xxx为dev,test…
- 同一项目下application配置文件放的位置可能不同,扫描循序如下
- classpath:/(classpath根目录)
- classpath:/config/(classpath下的config目录)
- file:./(当前目录)
- file:./config/(当前目录下的config文件夹)
- xxx.yml/xxx.properties,xxx为spring.application.name中定义的名字(spring cloud config)
- 同级文件在本地bootstrap的spring.profiles.active中配置,同级的配置文件有
- xxx-dev,开发环境
- xxx-test,测试环境
- xxx-exp,体验环境
- xxx-prod,生成环境
所以我们可以将公共的配置放到仓库的以application开头的配置文件中,所有的微服务都会加载application开头的配置,加载顺序上面已经写的很清楚了。比如你的开发环境是dev,那么会依次加载bootstrap.yaml,application.yaml.application-dev.yaml,xxx.yaml,xxx-dev.yaml
至此配置中心搭建,以及使用介绍完毕,如文章有问题,或有其他疑问欢迎交流。