一、作用
在没有使用统一配置中心之前,每个微服务都各自有自己的配置,这样就导致项目配置不方便维护、安全与权限的问题,以及修改了配置文件项目就需要重启。spring cloud config配置中心就可以解决这些问题。
二、原理
spring cloud config分为server端和client端。当项目启动的时候,config server会从远端git拉取配置然后会更新本地仓库中的配置文件,然后config client端通过在eureka中获取到config server端进而获取到配置文件。当config server和远端的git失去联系时,config server就会从本地仓库获取配置内容。
三、使用
1.config server
(1)添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
(2)启动类上添加注解
@EnableConfigServer
(3)修改配置文件
spring:
cloud:
config:
server:
git:
uri: #远端git地址
username: #用户名
password: #密码
basedir: #因为config server要从远端git