SpringCloudConfig
可以通过这个组件实现对每个服务内的配置文件进行统一管理。

- 搭建ConfigServer
步骤一:引入依赖
|
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId> </dependency>
|
步骤二:编写配置文件
为了将分布式配置中心管理的这些配置文件存放在git上,所以需要现在github或者gitlab上创建仓库。
|
spring:
application:
name: hello-spring-cloud-config
cloud:
config:
label: master
server:
git:
uri: https://github.com/181cm/nz1905.git
search-paths: respo server:
port: 8888
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
|
步骤三:打上注解 在启动类上
|
@EnableEurekaClient @EnableConfigServer @SpringBootApplication public class QfSpringCloudNetflixEurekaConfigServerApplication
|
创建好以后,可以通过访问该配置服务中心来测试。
- 实现配置中心的客户端
步骤一:引入依赖
|
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId> </dependency>
|
步骤二:编写配置文件
注意,配置文件只需要指明配置中心服务的地址以及要获取的配置文件的名称
|
spring:
cloud:
config:
uri: http://localhost:8888 # 要连接的配置中心服务的地址
name: eureka-feign # 要获取的配置文件的名称
label: master # master分支
|

- 基于不同的运行环境下的配置文件的切换
开发阶段: dev
测试阶段: test
生产阶段: prod
只需要在git仓库上存放以不同阶段命名后缀的文件,比如: eureka-feign-dev.yml或者eureka-feign-prod.yml。这是就可以通过在Config客户端的配置中指明相应的配置文件即可。
|
spring:
cloud:
config:
uri: http://localhost:8888
name: eureka-feign
label: master
profile: prod
|
但是,如果通过手动的修改客户端的application.yml文件,来指明使用不同的环境配置文件,那么依然是在手动调整,因此可以通过改变启动配置来达到这个效果。
一个是在idea中改变配置

另一个是在启动jar时指明环境配置文件
java -jar hello-spring-cloud-web-admin-feign-1.0.0-SNAPSHOT.jar --spring.profiles.active=prod