1、Springcloud Config:集中管理配置文件
2、Config服务端配置与测试
2.1 新建远程仓库
用你自己的账号在Gitee上新建一个名为springcloud-config的新Repository。
由上一步获得刚新建的git地址:https://gitee.com/hawyer/springcloud-config.git
2.2 本地仓库
本地硬盘目录上新建git仓库并clone
2.3 配置服务端
1⃣️新建Module模块cloud-config-center-3344,它即为Cloud的配置中心模块cloudConfig Center。
2⃣️Pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
3⃣️yml
server:
port: 3344
spring:
application:
name: cloud-config-center #注册进Eureka服务器的微服务名
cloud:
config:
server:
git:
uri: https://gitee.com/hawyer/springcloud-config.git #GitHub上面的git仓库名字
####搜索目录
search-paths:
- springcloud-config
####读取分支
label: master
#服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
4⃣️主启动:添加注解@EnableConfigServer
5⃣️修改映射:修改/etc/hosts文件,增加映射127.0.0.1 config-3344.com
2.4 测试
测试通过Config微服务是否可以从GitHub上获取配置内容。
1⃣️启动微服务3344
2⃣️http://config-3344.com:3344/master/config-dev.yml
3⃣️配置读取规则之一:/{label}/{application}-{profile}.yml
http://config-3344.com:3344/master/config-dev.yml
成功实现了用SpringCloud Config通过GitHub获取配置信息
3、Config客户端配置与测试
3.1 客户端配置
1⃣️新建cloud-config-client-3355
2⃣️Pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
3⃣️yml:bootstrap.yml
server:
port: 3355
spring:
application:
name: config-client
cloud:
#Config客户端配置
config:
label: master #分支名称
name: config #配置文件名称
profile: dev #读取后缀名称 上述3个综合:master分支上config-dev.yml的配置文件被读取http://config-3344.com:3344/master/config-dev.yml
uri: http://localhost:3344 #配置中心地址k
#rabbitmq相关配置 15672是Web管理界面的端口;5672是MQ访问的端口
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
#服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
# 暴露监控端点
management:
endpoints:
web:
exposure:
include: "*"
4⃣️主启动
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3355 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientMain3355.class, args);
}
}
5⃣️业务类
@RestController
@RefreshScope
public class ConfigClientController {
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo(){
return configInfo;
}
}
3.2 测试
1⃣️启动Config配置中心3344微服务并自测
2⃣️启动3355作为Client准备访问:http://localhost:3355/configInfo
成功实现了客户端3355访问SpringCloud Config3344通过Gitee获取配置信息。
4、动态刷新
4.1 服务端动态刷新
Linux运维修改Gitee上的配置文件内容做调整,刷新3344,发现ConfigServer配置中心立刻响应。
4.2 客户端动态刷新
1⃣️POM引入actuator监控
2⃣️修改YML,暴露监控端口
# 暴露监控端点
management:
endpoints:
web:
exposure:
include: "*"
3⃣️业务类Controller,添加@RefreshScope注解
4⃣️运维人员发送Post请求刷新3355:curl -X POST "http://localhost:3355/actuator/refresh"