一、添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
二、添加注解@EnableConfigServer
@SpringBootApplication
@EnableConfigServer
public class MainApplicationCenter3344 {
public static void main(String[] args) {
SpringApplication.run(MainApplicationCenter3344.class, args);
}
}
三、添加配置
server:
port: 3344
spring:
application:
name: cloud-config-center #注册进Eureka服务器的微服务名
cloud:
config:
server:
git:
uri: https://github.com/djftjfs/springcloud-config.git #GitHub上面的git仓库名字
####搜索目录
search-paths:
- springcloud-config
username: xxx
password: xxx
####读取分支
label: master
#服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
四、测试http://config-3344.com:3344/master/config-dev.yml
五、配置读取规则
label : 分支名 ; application : 服务名 ; profile : 环境(dev/test/prod)
/{label}/{application}-{profile}.yml
eg:http://config-3344.com:3344/master/config-dev.yml
/{application}-{profile}.yml
eg:http://config-3344.com:3344/config-dev.yml
/{application}/{profile}/{label}
eg:http://config-3344.com:3344/config/dev/master
四、客户端配置
1、添加依赖
<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>
2、创建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
#服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
五、动态刷新(手动)
1、在客户端模块添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2、在bootstrap.yml 文件添加如下内容
# 暴露监控端点
management:
endpoints:
web:
exposure:
include: "*"
3、在业务类上添加@RefreshScope
@RestController
@RefreshScope
public class ConfigClientController
{
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo()
{
return configInfo;
}
}
4、手动刷新(必须post)
执行 curl -X POST "http://localhost:3355/actuator/refresh"
六、动态刷新(自动)
1、在之前基础上在服务端和客户端上添加依赖
<!--添加消息总线RabbitMQ支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
2、服务端添加配置
#rabbitmq相关配置
rabbitmq:
host: 192.168.xxx.165
port: 5672
username: admin
password: 123
##rabbitmq相关配置,暴露bus刷新配置的端点
management:
endpoints: #暴露bus刷新配置的端点
web:
exposure:
include: 'bus-refresh'
3、客户端只需添加RabbitMQIP相关配置即可
4、当在git端修改完配置之后,只需在服务端执行curl -X POST "http://localhost:3344/actuator/bus-refresh" 即可刷新全部客户端
七、精确刷新
只需在上面通知上添加服务注册名及端口号即可
curl -X POST "http://localhost:3344/actuator/bus-refresh/config-client:3355"