前提:已经配置并启动了Eureka注册中心,假设地址为http://localhost:8205/eureka/(根据实际情况修改地址)
环境:码云(Git),maven,springboot
server(配置中心服务端)
spring:
application:
name: base-config
#配置中心
cloud:
config:
server:
git:
uri: https://gitee.com/besttop_erp_btspc/config.git
search-paths: '{application}'
username: 123@qq.com
password: passwd
#注册中心
eureka:
client:
service-url:
defaultZone: http://localhost:8205/eureka/
解释
1、spring.application.name: 配置中心的服务名
2、spring.application.cloud.config.server.git.uri: Git仓库地址
3、spring.application.cloud.config.server.git.search-paths:表示配置文件的根路径
比如:服务service存放在git仓库的config/service/config-dev.yml,此时这里的值就可以为config/service。而图中的'{application}'表示动态取服务客户端的服务名作为路径名。这样的话就可以针对不同的客户端服务分配不同的文件夹。
4、spring.application.cloud.config.server.git.username:Git账户名
5、spring.application.cloud.config.server.git.password:Git账户的密码
备注:
1、注意search-paths后的值有单引号
2、还可以通过非对称密钥的方式进行鉴权,具体可自己探索。
服务端添加配置中心的pom依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
client(配置中心客户端)
特别注意:配置中心客户端配置的注册中心应该与服务端配置的注册中心是同一个
spring:
application:
name: btspc-metadata
#配置中心客户端配置
cloud:
config:
name: ${spring.application.name}
profile: local
label: develop
fail-fast: true
discovery:
enabled: true
service-id: base-config
#注册中心
eureka:
client:
service-url:
defaultZone: http://localhost:8205/eureka/
解释
1、fail-fast为true时表示先检查配置中心的配置是否可用
2、discovery.enable表示是否开启根据服务名发现配置中心
3、discovery.service-id表示配置中心的服务名称
客户端添加pom依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
服务btspc-metadata服务在git中的存放路径如下
备注:
1、文件夹位于仓库的根目录下
2、文件名的组成方式 (详见客户端配置) name-profile