对于传统的单体应用而言,常使用配置文件来管理全部配置,好比SpringBoot的application.yml文件,可是在微服务架构中所有手动修改的话很麻烦并且不易维护。
微服务的配置管理通常有如下需求:
1.集中配置管理,一个微服务架构中可能有成百上千个微服务,因此集中配置管理是很重要的。
2.不一样环境不一样配置,好比数据源配置在不一样环境(开发,生产,测试)中是不一样的。
3.运行期间可动态调整。
4.配置修改后可自动更新。
好在Spring Cloud Config已经所有实现了上面几点。html
Spring Cloud Config为分布式系统外部化配置提供了服务器端和客户端的支持,它包括Config Client 和 Config Server两个部分。原理是全部的配置信息都存储在Config Server,全部的微服务都指向Config Server,
各个微服务启动时都会请求Config Server来获取配置信息,而后缓存到本地以提升性能。java
1.在Git仓库https://github.com/2YSP/spring-cloud-config-repo(可使用本身的仓库)新建几个配置文件,例如:
内容分别为:
profile=dev-1.0
profile=production-1.0
profile=test-1.0
profile=default-1.0
2.新建一个SpringBoot项目microservice-config-server,并添加如下依赖git
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
3.在启动类添加 @EnableConfigServer注解
4.编写application.yml文件github
server:
port: 8080
spring:
application:
name: microservice-config-server
cloud:
config:
server:
git:
# 配置Git仓库的地址
uri: https://github.com/2YSP/spring-cloud-config-repo.git
# 配置Git仓库的用户名
username: 2YSP
# 配置Git仓库的密码
password: XX
这样就完成了,可使用端点来获取配置文件,端点与配置文件的映射规则以下:
/{application}/{profile}[/{lable}]
/{application}-{profile}.yml
/{lable}/{application}-{profile}.yml
/{application}-{profile}.properties
/{lable}/{application}-{profile}.properties
{application}表示微服务的名称,{profile}表明环境,{lable}表示Git仓库的分支,默认是master。
本例若是要访问microservice-foo-dev.properties,则能够访问这些URL:
http://localhost:8080/microservice-foo/dev
http://localhost:8080/microservice-foo-dev.properties
http://localhost:8080/microservice-foo-dev.ymlweb
1.建立一个SpringBoot工程,ArtifactId为microservice-config-client,并添加如下依赖spring
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<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>
2.编写配置文件application.ymlbootstrap
server:
port: 8081
3.建立配置文件bootstrap.yml,并添加如下内容。缓存
spring:
application:
# 对应Config Server所获取的配置文件的{application}
name: microservice-foo
cloud:
config:
uri: http://localhost:8080/
#对应config server所获取配置文件的{profile}
profile: dev
# 指定Git仓库的分支,对应config server所获取配置文件的{label}
label: master
须要注意的是,以上属性应配置在bootstrap.yml而不是application.yml文件中,不然部分配置就不能正常工做。
4.编写Controller服务器
@RestController
public class ConfigClientController {
@Value("${profile}")
private String profile;
@GetMapping("/profile")
public String hello(){
return this.profile;
}
}
这里经过注解 @Value("${profile}") 来绑定Git仓库的profile属性。
5.测试
先启动microservice-config-server,再启动microservice-config-client,访问http://localhost:8081/profile便可得到如下结果。
dev-1.0
说明可以正常的获取Git仓库的配置信息。架构
1.复制项目microservice-config-client更改成microservice-config-client-refresh
2.为项目添加spring-boot-starter-actuator依赖,若是有了就不添加了。
3.在Controller类上添加@RefreshScope注解
@RestController
@RefreshScope
public class ConfigClientController {
@Value("${profile}")
private String profile;
@GetMapping("/profile")
public String hello(){
return this.profile;
}
}
4.修改Git仓库中microservice-foo-dev.properties文件的内容,而后先发送POST请求到http://localhost:8081/refresh,再访问http://localhost:8081/refresh便可获取最新的配置。
1.首先安装RabbitMQ,安装步骤这里不介绍个人博客里有。
2.为项目添加如下依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
3.在bootstrap.yml中添加如下内容
spring:
application:
# 对应Config Server所获取的配置文件的{application}
name: microservice-foo
cloud:
config:
uri: http://localhost:8080/
#对应config server所获取配置文件的{profile}
profile: dev
# 指定Git仓库的分支,对应config server所获取配置文件的{label}
label: master
rabbitmq:
host: localhost
port: 5672 #默认端口 5672
username: guest
password: guest