简介
Spring Cloud Config可以对微服务配置进行统一的外部管理,并且默认采用Git来管理配置信息。相对于传统的每个微服务对应一份自个儿的配置文件来说,通过Spring Cloud Config统一管理所有微服务配置具有如下优点:1.集中管理微服务配置,当微服务数量众多的时候,使用这种方式会更为方便;2.通过Git管理微服务配置,方便追踪配置修改记录;3.可以在应用运行期间修改配置,微服务能够自动更新配置。
搭建Config-Server
在搭建Config-Server之前,我们需要创建好一个存储配置文件的Git仓库。
四个配置文件中分别都包含了一个message属性。
创建好配置文件仓库后,我们新建一个Spring Boot项目,然后引入如下依赖:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
在入口类中加入@EnableConfigServer注解,开启Spring Cloud Config服务端功能。
接着配置application.yml:
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://github.com/haliaddel/spring-cloud-config.git
username: xxx
password: xxx
server:
port: 8092
配置具体含义如下:
- spring.cloud.config.server.git.uri:配置了Git仓库的地址,这里用的是码云,当然你也可以使用别的Git代码托管平台;
- spring.cloud.config.server.git.username:仓库用户名,即Git托管平台的用户名;
- spring.cloud.config.server.git.password:仓库密码,即Git托管平台的密码。
启动应用,我们便可以使用下面这些格式来访问配置信息了:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
搭建Config-Client
新建SpringBoot项目,pom文件引入依赖
dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</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-starter-bus-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
编写一个Controller,用于测试获取Config-Server中配置文件的message属性值:
@RestController
public class TestController {
@Value("${message}")
private String message;
@GetMapping("message")
public String getMessage() {
return this.message;
}
}
要想让上面的代码顺利的获取到message属性值,我们还需要在配置文件中进行一些额外配置:
spring:
application:
name: qiang
cloud:
config:
profile: dev
label: master
#整合Spring Security时,需要提供身份信息
username: qiang
password: 123456
#整合服务发现
discovery:
enabled: true
service-id: config-server
#在Greenwich版本中,需要如下配置暴露/refresh端口,否则无效
management:
endpoint:
refresh:
enabled: true
endpoints:
web:
exposure:
include: refresh, health
eureka:
client:
service-url:
defaultZone: http://localhost:8085/eureka, http://localhost:8087/eureka
值得注意的是,这里配置文件并不是叫application.yml,而是必须命名为bootstrap.yml。
上面配置含义如下:
spring.application.name: 对应配置文件规则中的{application};
spring.cloud.config.profile: 对应配置文件规则中的{profile}部分;
spring.cloud.config.label: 对应配置文件规则中的{label}部分;
spring.cloud.config.uri: 对应Config-Server的地址。未引入eureka时,直接指定config服务的地址
Config-Server额外配置
-
占位符的使用
在Config-Server中,除了固定配置一个Git仓库地址外,我们也可以使用占位符来灵活的指定Git仓库地址。
将上面Config-Server的Git仓库地址改为:spring: cloud: config: server: git: uri: https://gitee.com/mrbird/{application}
这里使用占位符{application}来代替上面的spring-cloud-config,通过这种配置,我们可以让不同的Config-Client去不同的Git仓库获取配置。比如,当Config-Client的项目名称为febs的时候,对应Git仓库地址为:https://gitee.com/mrbird/febs,当名称为test时,对应Git仓库地址为:https://gitee.com/mrbird/test。这样我们就可以为不同的项目配置不同的Git仓库。
-
子目录支持
除了使用占位符为每个项目创建单独的Git仓库来存储配置信息外,我们也可以只创建一个Git仓库来存储配置,只不过是将不同的项目配置放置到不同的目录下,只需要像下面这样配置即可:spring: cloud: config: server: git: uri: https://gitee.com/mrbird/spring-cloud-config/ username: xxxx password: xxxx search-paths: '{application}'
这里search-paths的占位符必须加上单引号’’,否则没办法正确读取配置
-
clone-on-start
默认情况下Config-Server在启动的时候并不会马上就去Git参考clone配置文件,只有当Config-Clinet从Config-Server获取相关配置信息的时候,其才会去进行clone操作。我们可以将clone-on-start属性设置为true,其Config-Server在启动的时候就进行clone操作:spring cloud: config: server: git: clone-on-start: true
这样做的好处在于,当Git连接信息有误时,可以马上发现。
-
整合Spring Security
Config-Server中包含了Git连接信息,为了使其更加安全,我们可以通过Spring Security来做用户名密码认证。
在Config-Server中加入Spring Security依赖.
然后在Config-Server的配置文件application.yml中加入用户名和密码:security: user: name: qiang password: 123456
与此同时,我们也需要在Config-Client中配置Config-Server的用户名和密码,否则在获取配置的时候将报401错误:
spring: cloud: config: username: qiang password: 123456
-
加密
参考链接
Config-Client额外配置
-
刷新配置
在Config-Server和Config-Client都启动后,如果这时候Git仓库存储的配置信息改变了,在不重启Config-Client的情况下,配置信息是不会跟着更新的。那么如何在Git仓库存储的配置得到改变的时候也刷新Config-Client中获取到的配置值呢?很简单,我们只需要对Config-Client进行简单的改造:在Config-Client中添加spring-boot-starter-actuator依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
该依赖包含了/refresh端点,可以用来刷新配置。
然后在获取配置的Controller上加入@RefreshScope注解:
@RestController @RefreshScope public class TestController { @Value("${message}") private String message; @GetMapping("message") public String getMessage() { return this.message; } }
值得注意的是,我们需要在Config-Client的配置文件中加入如下配置来关闭认证,否则我们无权访问/refresh端点。
SpringCloud暴露了一个接口 /refresh 来给我们去刷新配置,但是SpringCloud 2.0.0以后,有了改变.-
我们需要在bootstrap.yml里面加上需要暴露出来的地址
management: endpoints: web: exposure: include: refresh,health
-
现在的地址也不是/refresh了,而是/actuator/refresh
-
集群配置
如果Config-Server只是单机部署的话,当其遇到问题宕机后,所有从Config-Server获取配置信息的微服务都无法正常工作了,这是灾难性的。所以我们很有必要对Config-Server进行集群处理。
集群的方式有很多,这里介绍通过将Config-Server注册到Eureka的方式来搭建集群。
我们使用在Spring Cloud Eureka服务治理一节中的Eureka-Service来构建Eureka-Server服务注册中心,使用以下命令启动Eureka-Server集群:
java -jar Eureka-Service-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
java -jar Eureka-Service-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2
Eureka集群启动成功。 接下来开始对Config-Server进行改造,将其注册到Eureka服务注册中心。
引入eureka依赖,在入口开启服务发现@EnableDiscoveryClient,在配置中指定eureka的注册地址。改造好后,启动Config-Server。
接着开始改造Config-Client,在其pom中同样引入Eureka依赖,然后在bootstrap.yml中指定获取Config-Server服务的地址:
spring:
application:
name: qiang
cloud:
config:
profile: dev
label: master
username: qiang
password: 123456
discovery:
enabled: true
service-id: config-server
其中eureka.client.service-url.defaultZone指定了Eureka服务注册中心的地址;spring.cloud.config.discovery.enabled指定为true开启获取服务的功能,spring.cloud.config.discovery.service-id表明需要获取服务的名称为config-server。
最后在Config-Client的启动类中引入@EnableDiscoveryClient注解,启动应用。