一、SpringCloud Config简介
Spring Cloud Config为分布式系统中的配置管理提供服务器和客户端支持。它包含两部分:
- Config Server:是一个可横向扩展、集中式的配置服务器,它用于集中管理应用程序各个环境下的配置,默认使用Git存储配置文件内容,也可以使用SVN存储,或者是本地文件存储。
- Config Client:是Config Server的客户端,用于操作存储在Config Server中的配置内容。微服务在启动时Config Client会请求Config Server以获取配置文件的内容,请求到后再启动容器并应用配置。
二、统一配置管理实现
1、创建git仓库(gitee)
创建一个文件夹
作为Git仓库,并添加一个配置文件,可以直接复制user-service工程中的配置文件
使用命令推送到git仓库
git init
git add .
git commit -m "初始化配置文件"
git remote add origin 仓库地址
git push -u origin master
2、搭建ConfigServer工程
(1)、引入依赖
<!--eureka依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--config-server依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
(2)、创建启动类并添加注解
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
(3)、添加配置文件(application.yml文件中)
server:
port: 12580
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://gitee.com/Rrx-w/cloud-config.git # git仓库地址
# username: # 如果有用户名和密码需要加上
# password:
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka # eureka地址
(4)、测试
访问:http://localhost:12580/userservice-dev.yml
访问规范
/{application}/{profile}[/{label}]
/{application}-{profile}.yml # 没写lable 默认就是master分支
/{label}/{application}-{profile}.yml
3、配置ConfigClient
引入依赖(在客户端)
在user-service服务中引入config依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
添加配置文件
SpringBoot中提供了一个名为bootstrap.yml的文件,加载顺序比application.yml优先,重命名application.yml文件为application.yml.bak使其失效
创建bootstrap.yml文件
eureka:
client:
service-url:
# defaultZone: http://localhost:10086/eureka,http://localhost:10087/eureka
defaultZone: http://localhost:10086/eureka
spring:
application:
name: user-service
cloud:
config:
name: userservice # 配置文件的应用名称
label: master # 配置文件的分支名称
profile: dev # 环境名称
discovery:
enabled: true # 开启从注册中心获取配置中心地址
service-id: config-server # 配置中心的服务名称
定义属性读取类
@Data
@Component
@ConfigurationProperties(prefix = "test.redis")
public class RedisProperties {
private String host;
private int port;
}
测试:定义一个controller类
@RestController
@RequestMapping("hello")
public class HelloController {
@Autowired
private RedisProperties properties;
@GetMapping("prop")
public RedisProperties getProperties(){
return properties;
}
}
集成测试
修改git仓库中,master分支的userservice-dev.yml文件
test:
redis:
host: localhost
port: 6380
访问:http://localhost:8081/hello/prop
4、开启手动刷新功能
user-service微服务中引入依赖
<!-- springboot提供的监控依赖,可以查看当前springboot的一些信息
/health /info /up /refresh:刷新配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
user_service的 bootstrap.yml 文件中添加配置,暴露接口
management:
endpoints:
web:
exposure:
include: "*" # 暴露refresh接口
测试
-
重启user-service测试
-
修改git中的配置文件
-
先访问http://localhost:8081/actuator/refresh,在刷新页面, 端口号变为7777