config-server的使用
1.1 依赖
配置中心服务增加依赖
<!-- 配置中心服务端依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>客户端增加依赖
<!-- 配置中心客户端依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>1.2 配置
服务端的配置
#指定注册中心地址
eureka:
client:
serviceUrl:
defaultZone: http://localhost:10000/eureka/
#服务的名称
spring:
application:
name: config-server
cloud:
config:
server:
git:
#git克隆地址
uri: https://gitee.com/twolflcy/springcloud-config.git
#git账号
username: git账号
#git密码
password: git密码
#超时时间
timeout: 5
#默认分支
default-label: master客户端的配置,必须要有的配置服务名称和注册中心以及配置中心的配置信息
#指定注册中心地址
eureka:
client:
serviceUrl:
defaultZone: http://localhost:10000/eureka/
#服务的名称
spring:
application:
name: product-service
cloud:
config:
discovery:
#配置中心服务名称
service-id: config-server
#是否启动注册中心
enabled: true
#获取文件的分支
label: master
#获取${name}-${profile} 即product-service-local.yml
profile: local1.3 启用注解
开启配置中心的注解,在启动类增加@EnableConfigServer
二、默认使用git存储配置中心
根据实际情况创建git仓库,可以是gitee、gitlab、github。配置在application.yml的配置
2.1 访问git配置文件
例如:http://127.0.0.1:9001/master/eureka-server.yml和http://127.0.0.1:9001/eureka-server.yml
master是属于lable分支名称。注意git上的配置文件必须要有"-" ,而且路径的匹配是根据前缀匹配的,如访问http://127.0.0.1:9001/eureka-server-aaaa.yml也能访问到yml。但是必须有"-"才会读取到,读取不到这种情况的路径http://127.0.0.1:9001/eureka-serveraaaa.yml。建议用lable分支来区分不同环境的配置文件,而不是通过profile,因为环境多的情况下文件变多。会混乱
name:服务器名称 profile :环境名称,开发、测试、生产 lable :仓库分支、默认master分支
访问方式:
/{name}-{profiles}.properties
/{name}-{profiles}.yml
/{name}-{profiles}.json
/{label}/{name}-{profiles}.yml
2.2 客户端使用配置中心配置
客户端引入依赖以后,需要把application.yml的配置文件改成bootstrap.yml,否则会加载的时候获取不到。这是由于配置文件的加载顺序问题
