服务端的依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
客户端依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
在config启动类上需要加上注解
@EnableEurekaClient
@EnableConfigServer
配置文件application.yml
spring:
application:
name: gblx-config
profiles:
active: git
cloud:
config:
server:
git:
uri: *****
username: ****
password: *
# 指定搜索路径,如果有多个路径则使用,分隔
searchPaths: /config
# 对于使用git,svn做为后端配置,从远程库获取配置文件,需要存储到本地文件
# basedir: /tmp/config
basedir: /config/basedir
# 配置中心通过git从远程git库,有时本地的拷贝被污染,这时配置中心无法从远程库更新本地配置,设置force-pull=true,则强制从远程库中更新本地库
force-pull: true
# 配置仓库的分支
label: master
客户端启动类
@EnableEurekaClient
特别声明 用bootstrap.yml
spring:
application:
name: ****
cloud:
config:
uri: http://localhost:8334
profile: test
name: mysql
如果您更喜欢用DiscoveryClient来定位配置服务器,可以通过设置spring.cloud.config.discovery.enabled=true(默认为“false”)来实现。最终的结果是,客户端应用程序都需要一个 bootstrap.yml(或一个环境变量)与适当的发现配置。例如,使用Spring Cloud Netflix,您需要定义Eureka服务器地址,例如eureka.client.serviceUrl.defaultZone。使用此选项的代价是在启动时进行额外的网络往返查找服务注册。好处是配置服务器可以改变它的坐标,前提是发现服务是一个固定的点。默认的服务ID是“configserver”,但是你可以采用spring.cloud.config.discovery.serviceId在服务器上用服务器修改服务(例如通过设置服务器spring.application.name)。