微服务基本组成
Config

SpringCloud Config为微服务架构中的微服务提供集中化的外部支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置

服务端创建
pom
1 2 3 4
| <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
|
yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| server: port: 3344 spring: application: name: cloud-config-center cloud: config: server: git: uri: git@github.com:JTXYH/springcloud-config.git search-paths: - springcloud-config label: master
eureka: client: service-url: defaultZone: http://localhost:7001/eureka
|
启动类
1 2 3 4 5 6 7
| @SpringBootApplication @EnableConfigServer public class ConfigCenterMain3344 { public static void main(String[] args) { SpringApplication.run(ConfigCenterMain3344 .class,args); } }
|
客户端创建
pom
1 2 3 4 5 6 7 8 9
| <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>
|
bootstrap.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| server: port: 3355
spring: application: name: config-client cloud: config: label: master name: config profile: dev uri: http://localhost:3344
eureka: client: service-url: defaultZone: http://eureka7001.com:7001/eureka
management: endpoints: web: exposure: include: "*"
|
业务类
1 2 3 4 5 6 7 8 9 10 11 12
| @RefreshScope @RestController public class ConfigClientController {
@Value("${config.info}") private String configInfo;
@GetMapping("/configInfo") public String getConfigInfo(){ return configInfo; } }
|
启动类
1 2 3 4 5 6 7
| @EnableEurekaClient @SpringBootApplication public class ConfigClientMain3355 { public static void main(String[] args) { SpringApplication.run( ConfigClientMain3355.class,args); } }
|
每次远程更新配置文件之后使用这个post请求刷新
1
| curl -X POST "http://localhost:3355/actuator/refresh"
|
读取配置规则
/{label}/{application}-{profile}.yml(最推荐使用这种方式)

/{application}-{profile}.yml

/{application}-{profile}[/{label}]

boostrap.yml

Bus
Spring Cloud Bus配合Spring Cloud Config使用可以实现配置的动态刷新



设计思想
利用消息总线触发一个客户端/bus/refresh而刷新所有客户端的配置

利用消息总线触发一个服务端ConfigServer的/bus/refresh端点而刷新所有客户端(推荐使用)

服务端添加消息总线服务
pom
1 2 3 4 5
| <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency>
|
yml
1 2 3 4 5 6 7 8 9 10 11 12
| rabbitmq: host: localhost port: 5672 username: guest password: guest
management: endpoints: web: exposure: include: 'bus-refresh'
|
客户端添加总线服务
pom
1 2 3 4
| <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency>
|
yml
1 2 3 4 5 6
| spring: rabbitmq: host: localhost port: 5672 username: guest password: guest
|
使用
使用全局通知,一次通知,全局生效

1
| curl -X POST "http://localhost:3344/actuator/bus-refresh"
|
使用定点通知,只通知部分服务
公式:http://localhost:配置中心的端口号/actuator/bus-refresh/{destination}

1 2
| # 只通知3355 curl -X POST "http://localhost:3344/actuator/bus-refresh/config-client:3355"
|
总结

相关文章
SpringCloud
服务注册与发现
服务降级
服务调用
服务网关
Stream消息驱动
SpringCloud-OpenFeign问题
SpringCloud-GateWay工具类
DockerCompose常用软件配置
SpringQuartz动态定时任务