Spring Cloud(九):配置中心(消息总线)【Finchley 版】

Spring Cloud(九):配置中心(消息总线)【Finchley 版】

发表于 2018-04-19 |  更新于 2018-05-07 |

我们在 Spring Cloud(七):配置中心(Git、Refresh) 中讲到,如果需要客户端获取到最新的配置信息需要执行refresh,我们可以利用 Webhook 的机制每次提交代码发送请求来刷新客户端,当客户端越来越多的时候,需要每个客户端都执行一遍,这种方案就不太适合了。使用 Spring Cloud Bus 可以完美解决这一问题。

Spring Cloud Bus

Spring Cloud Bus 通过轻量消息代理连接各个分布的节点。这会用在广播状态的变化(例如配置变化)或者其他的消息指令。Spring Bus 的一个核心思想是通过分布式的启动器对 Spring Boot 应用进行扩展,也可以用来建立一个多个应用之间的通信频道。目前唯一实现的方式是用 Amqp 消息代理作为通道,同样特性的设置(有些取决于通道的设置)在更多通道的文档中。

Spring Cloud Bus 被国内很多都翻译为消息总线,也挺形象的。大家可以将它理解为管理和传播所有分布式项目中的消息既可,其实本质是利用了 MQ 的广播机制在分布式的系统中传播消息,目前常用的有 Kafka 和 RabbitMQ。利用 Bus 的机制可以做很多的事情,其中配置中心客户端刷新就是典型的应用场景之一,我们用一张图来描述 Bus 在配置中心使用的机制。

实战

我们选择上一篇文章 Spring Cloud(八):配置中心(服务化与高可用) 版本的示例代码来改造, MQ 我们使用 RabbitMQ 来做示例。

因为用的是 Spring Boot 2.0.1 + Spring Cloud Finchley.RC1,更新较多,坑也比较多,这里就把代码再贴一遍了。

示例代码:GitHub

服务端

POM 配置

在 pom.xml 里添加,这 4 个是必须的

复制

12345678910111213141516
<dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-config-server</artifactId></dependency><dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-bus</artifactId></dependency><dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-stream-binder-rabbit</artifactId></dependency>

配置文件

application.yml 内容如下

复制

123456789101112131415161718192021222324
spring:  application:    name: config-server  cloud:    config:      server:        git:          uri: https://github.com/zhaoyibo/spring-cloud-study          search-paths: config-repo    bus:      enabled: true      trace:        enabled: trueserver:  port: 12000eureka:  client:    service-url:      defaultZone: http://localhost:7000/eureka/management:  endpoints:    web:      exposure:        include: bus-refresh

启动类

@EnableConfigServer注解

复制

12345678
@SpringBootApplication@EnableConfigServerpublic class ConfigServerApplication {

public static void main(String[] args) {        SpringApplication.run(ConfigServerApplication.class, args);    }}

客户端

POM 配置

在 pom.xml 里添加以下依赖,前 5 个是必须的,最后一个 webflux 你可以用 web 来代替

复制

123456789101112131415161718192021222324
<dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-starter-config</artifactId></dependency><dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-bus</artifactId></dependency><dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-stream-binder-rabbit</artifactId></dependency><dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-webflux</artifactId></dependency>

这里容易漏掉spring-boot-starter-actuator,如果缺了这个,当对服务端执行/actuator/bus-refresh的时候,客户端接收不到信息,Console 里只会显示以下信息(我就是在这儿被困了好久……)

复制

123
2018-04-19 18:50:05.711  INFO 39762 --- [vOu-GI8c8mJtQ-1] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: [localhost:5672]2018-04-19 18:50:05.739  INFO 39762 --- [vOu-GI8c8mJtQ-1] o.s.a.r.c.CachingConnectionFactory       : Created new connection: rabbitConnectionFactory.publisher#2bc15b3b:0/[email protected] [delegate=amqp://[email protected]:5672/, localPort= 60107]2018-04-19 18:50:05.749  INFO 39762 --- [vOu-GI8c8mJtQ-1] o.s.amqp.rabbit.core.RabbitAdmin         : Auto-declaring a non-durable, auto-delete, or exclusive Queue (springCloudBus.anonymous.bOoVqQuSQvOu-GI8c8mJtQ) durable:false, auto-delete:true, exclusive:true. It will be redeclared if the broker stops and is restarted while the connection factory is alive, but all messages will be lost.

配置文件

还是分为两个,分别如下
application.yml

复制

12345678910
spring:  application:    name: config-client  cloud:    bus:      trace:        enabled: true      enabled: trueserver:  port: 13000

bootstrap.yml

复制

12345678910111213
spring:  cloud:    config:      name: config-client      profile: dev      label: master      discovery:        enabled: true        service-id: config-servereureka:  client:    service-url:      defaultZone: http://localhost:7000/eureka/

Controller

复制

12345678910111213
@RestController@RefreshScopepublic class HelloController {

@Value("${info.profile:error}")    private String profile;

@GetMapping("/info")    public Mono<String> hello() {        return Mono.justOrEmpty(profile);    }

}

@RefreshScope必须加,否则客户端会受到服务端的更新消息,但是更新不了,因为不知道更新哪里的。

至于启动主类,用默认生成的不用改,就不贴了。

测试

分别启动 eureka、config-server 和两个 config-client

复制

123456
// 打包./mvnw clean package -Dmaven.test.skip=true

// 运行两个 clientjava -jar target/spring-cloud-config-client-0.0.1-SNAPSHOT.jar --server.port=13000java -jar target/spring-cloud-config-client-0.0.1-SNAPSHOT.jar --server.port=13001

启动后,RabbitMQ 中会自动创建一个 topic 类型的 Exchange 和两个以springCloudBus.anonymous.开头的匿名 Queue

我们访问 http://localhost:13000/info 和 http://localhost:13001/info 返回内容的都是dev
将 Git 中的配置信息由dev改为dev bus,并执行

复制

1
curl -X POST http://localhost:12000/actuator/bus-refresh/

再次访问 http://localhost:13000/info 和 http://localhost:13001/info 这时返回内容就是修改之后的dev bus了,说明成功了。

服务端在刷新接口产生的的日志:

复制

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
2018-04-19 18:37:08.034  INFO 38109 --- [io-12000-exec-9] s.c.a.AnnotationConfigApplicationContext : Refreshing org.spring[email protected]371b3397: startup date [Thu Apr 19 18:37:08 CST 2018]; root of context hierarchy2018-04-19 18:37:08.148  INFO 38109 --- [io-12000-exec-9] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 ‘javax.inject.Inject‘ annotation found and supported for autowiring2018-04-19 18:37:08.163  INFO 38109 --- [io-12000-exec-9] trationDelegate$BeanPostProcessorChecker : Bean ‘configurationPropertiesRebinderAutoConfiguration‘ of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$32744ef0] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)2018-04-19 18:37:08.294  INFO 38109 --- [io-12000-exec-9] o.s.boot.SpringApplication               : No active profile set, falling back to default profiles: default2018-04-19 18:37:08.301  INFO 38109 --- [io-12000-exec-9] s.c.a.AnnotationConfigApplicationContext : Refreshing org.spring[email protected]3e669fd6: startup date [Thu Apr 19 18:37:08 CST 2018]; parent: org.spring[email protected]371b33972018-04-19 18:37:08.306  INFO 38109 --- [io-12000-exec-9] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 ‘javax.inject.Inject‘ annotation found and supported for autowiring2018-04-19 18:37:08.330  INFO 38109 --- [io-12000-exec-9] o.s.boot.SpringApplication               : Started application in 0.391 seconds (JVM running for 511.143)2018-04-19 18:37:08.333  INFO 38109 --- [io-12000-exec-9] s.c.a.AnnotationConfigApplicationContext : Closing org.spring[email protected]3e669fd6: startup date [Thu Apr 19 18:37:08 CST 2018]; parent: org.spring[email protected]371b33972018-04-19 18:37:08.334  INFO 38109 --- [io-12000-exec-9] s.c.a.AnnotationConfigApplicationContext : Closing org.spring[email protected]371b3397: startup date [Thu Apr 19 18:37:08 CST 2018]; root of context hierarchy2018-04-19 18:37:08.985  INFO 38109 --- [io-12000-exec-9] com.netflix.discovery.DiscoveryClient    : Shutting down DiscoveryClient ...2018-04-19 18:37:09.007  INFO 38109 --- [io-12000-exec-9] o.s.c.n.eureka.InstanceInfoFactory       : Setting initial instance status as: STARTING2018-04-19 18:37:12.012  INFO 38109 --- [io-12000-exec-9] com.netflix.discovery.DiscoveryClient    : Unregistering ...2018-04-19 18:37:12.019  INFO 38109 --- [io-12000-exec-9] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_CONFIG-SERVER/172.16.106.93:config-server:12000 - deregister  status: 2002018-04-19 18:37:12.030  INFO 38109 --- [io-12000-exec-9] com.netflix.discovery.DiscoveryClient    : Completed shut down of DiscoveryClient2018-04-19 18:37:12.032  INFO 38109 --- [io-12000-exec-9] com.netflix.discovery.DiscoveryClient    : Initializing Eureka in region us-east-12018-04-19 18:37:12.037  INFO 38109 --- [io-12000-exec-9] c.n.d.provider.DiscoveryJerseyProvider   : Using JSON encoding codec LegacyJacksonJson2018-04-19 18:37:12.037  INFO 38109 --- [io-12000-exec-9] c.n.d.provider.DiscoveryJerseyProvider   : Using JSON decoding codec LegacyJacksonJson2018-04-19 18:37:12.037  INFO 38109 --- [io-12000-exec-9] c.n.d.provider.DiscoveryJerseyProvider   : Using XML encoding codec XStreamXml2018-04-19 18:37:12.037  INFO 38109 --- [io-12000-exec-9] c.n.d.provider.DiscoveryJerseyProvider   : Using XML decoding codec XStreamXml2018-04-19 18:37:12.145  INFO 38109 --- [io-12000-exec-9] c.n.d.s.r.aws.ConfigClusterResolver      : Resolving eureka endpoints via configuration2018-04-19 18:37:12.146  INFO 38109 --- [io-12000-exec-9] com.netflix.discovery.DiscoveryClient    : Disable delta property : false2018-04-19 18:37:12.146  INFO 38109 --- [io-12000-exec-9] com.netflix.discovery.DiscoveryClient    : Single vip registry refresh property : null2018-04-19 18:37:12.146  INFO 38109 --- [io-12000-exec-9] com.netflix.discovery.DiscoveryClient    : Force full registry fetch : false2018-04-19 18:37:12.146  INFO 38109 --- [io-12000-exec-9] com.netflix.discovery.DiscoveryClient    : Application is null : false2018-04-19 18:37:12.146  INFO 38109 --- [io-12000-exec-9] com.netflix.discovery.DiscoveryClient    : Registered Applications size is zero : true2018-04-19 18:37:12.146  INFO 38109 --- [io-12000-exec-9] com.netflix.discovery.DiscoveryClient    : Application version is -1: true2018-04-19 18:37:12.146  INFO 38109 --- [io-12000-exec-9] com.netflix.discovery.DiscoveryClient    : Getting all instance registry info from the eureka server2018-04-19 18:37:12.151  INFO 38109 --- [io-12000-exec-9] com.netflix.discovery.DiscoveryClient    : The response status is 2002018-04-19 18:37:12.152  INFO 38109 --- [io-12000-exec-9] com.netflix.discovery.DiscoveryClient    : Starting heartbeat executor: renew interval is: 302018-04-19 18:37:12.152  INFO 38109 --- [io-12000-exec-9] c.n.discovery.InstanceInfoReplicator     : InstanceInfoReplicator onDemand update allowed rate per min is 42018-04-19 18:37:12.153  INFO 38109 --- [io-12000-exec-9] com.netflix.discovery.DiscoveryClient    : Discovery Client initialized at timestamp 1524134232153 with initial instances count: 32018-04-19 18:37:12.154  INFO 38109 --- [io-12000-exec-9] o.s.c.n.e.s.EurekaServiceRegistry        : Unregistering application config-server with eureka with status DOWN2018-04-19 18:37:12.154  INFO 38109 --- [io-12000-exec-9] o.s.c.n.e.s.EurekaServiceRegistry        : Registering application config-server with eureka with status UP2018-04-19 18:37:12.154  WARN 38109 --- [io-12000-exec-9] com.netflix.discovery.DiscoveryClient    : Saw local status change event StatusChangeEvent [timestamp=1524134232154, current=UP, previous=DOWN]2018-04-19 18:37:12.155  INFO 38109 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_CONFIG-SERVER/172.16.106.93:config-server:12000: registering service...2018-04-19 18:37:12.155  INFO 38109 --- [io-12000-exec-9] o.s.cloud.bus.event.RefreshListener      : Received remote refresh request. Keys refreshed []2018-04-19 18:37:12.165  INFO 38109 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_CONFIG-SERVER/172.16.106.93:config-server:12000 - registration status: 2042018-04-19 18:37:14.044  INFO 38109 --- [o-12000-exec-10] .c.s.e.MultipleJGitEnvironmentRepository : Fetched for remote master and found 1 updates2018-04-19 18:37:14.105  INFO 38109 --- [o-12000-exec-10] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.An[email protected]: startup date [Thu Apr 19 18:37:14 CST 2018]; root of context hierarchy2018-04-19 18:37:14.108  INFO 38109 --- [o-12000-exec-10] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 ‘javax.inject.Inject‘ annotation found and supported for autowiring2018-04-19 18:37:14.112  INFO 38109 --- [o-12000-exec-10] o.s.c.c.s.e.NativeEnvironmentRepository  : Adding property source: file:/var/folders/rm/v0glpzzn7dxd1nj5nk00ssq00000gn/T/config-repo-520847716419946533/config-repo/config-client-dev.yml2018-04-19 18:37:14.112  INFO 38109 --- [o-12000-exec-10] s.c.a.AnnotationConfigApplicationContext : Closing org.spring[email protected]5e8ec379: startup date [Thu Apr 19 18:37:14 CST 2018]; root of context hierarchy2018-04-19 18:37:15.386  INFO 38109 --- [io-12000-exec-1] s.c.a.AnnotationConfigApplicationContext : Refreshing org.spring[email protected]62062974: startup date [Thu Apr 19 18:37:15 CST 2018]; root of context hierarchy2018-04-19 18:37:15.388  INFO 38109 --- [io-12000-exec-1] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 ‘javax.inject.Inject‘ annotation found and supported for autowiring2018-04-19 18:37:15.393  INFO 38109 --- [io-12000-exec-1] o.s.c.c.s.e.NativeEnvironmentRepository  : Adding property source: file:/var/folders/rm/v0glpzzn7dxd1nj5nk00ssq00000gn/T/config-repo-520847716419946533/config-repo/config-client-dev.yml2018-04-19 18:37:15.393  INFO 38109 --- [io-12000-exec-1] s.c.a.AnnotationConfigApplicationContext : Closing org.spring[email protected]62062974: startup date [Thu Apr 19 18:37:15 CST 2018]; root of context hierarchy

客户端在刷新接口产生的的日志:

复制

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
2018-04-19 18:35:45.899  INFO 38808 --- [x6SsSoOn6XStg-1] s.c.a.AnnotationConfigApplicationContext : Refreshing org.spring[email protected]43b9bce7: startup date [Thu Apr 19 18:35:45 CST 2018]; root of context hierarchy2018-04-19 18:35:46.292  INFO 38808 --- [x6SsSoOn6XStg-1] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 ‘javax.inject.Inject‘ annotation found and supported for autowiring2018-04-19 18:35:46.448  INFO 38808 --- [x6SsSoOn6XStg-1] trationDelegate$BeanPostProcessorChecker : Bean ‘configurationPropertiesRebinderAutoConfiguration‘ of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$494fdb28] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)2018-04-19 18:35:46.778  INFO 38808 --- [x6SsSoOn6XStg-1] o.s.c.n.eureka.InstanceInfoFactory       : Setting initial instance status as: STARTING2018-04-19 18:35:46.819  INFO 38808 --- [x6SsSoOn6XStg-1] com.netflix.discovery.DiscoveryClient    : Initializing Eureka in region us-east-12018-04-19 18:35:46.828  INFO 38808 --- [x6SsSoOn6XStg-1] c.n.d.provider.DiscoveryJerseyProvider   : Using JSON encoding codec LegacyJacksonJson2018-04-19 18:35:46.828  INFO 38808 --- [x6SsSoOn6XStg-1] c.n.d.provider.DiscoveryJerseyProvider   : Using JSON decoding codec LegacyJacksonJson2018-04-19 18:35:46.828  INFO 38808 --- [x6SsSoOn6XStg-1] c.n.d.provider.DiscoveryJerseyProvider   : Using XML encoding codec XStreamXml2018-04-19 18:35:46.829  INFO 38808 --- [x6SsSoOn6XStg-1] c.n.d.provider.DiscoveryJerseyProvider   : Using XML decoding codec XStreamXml2018-04-19 18:35:47.017  INFO 38808 --- [x6SsSoOn6XStg-1] c.n.d.s.r.aws.ConfigClusterResolver      : Resolving eureka endpoints via configuration2018-04-19 18:35:47.019  INFO 38808 --- [x6SsSoOn6XStg-1] com.netflix.discovery.DiscoveryClient    : Disable delta property : false2018-04-19 18:35:47.019  INFO 38808 --- [x6SsSoOn6XStg-1] com.netflix.discovery.DiscoveryClient    : Single vip registry refresh property : null2018-04-19 18:35:47.019  INFO 38808 --- [x6SsSoOn6XStg-1] com.netflix.discovery.DiscoveryClient    : Force full registry fetch : false2018-04-19 18:35:47.019  INFO 38808 --- [x6SsSoOn6XStg-1] com.netflix.discovery.DiscoveryClient    : Application is null : false2018-04-19 18:35:47.019  INFO 38808 --- [x6SsSoOn6XStg-1] com.netflix.discovery.DiscoveryClient    : Registered Applications size is zero : true2018-04-19 18:35:47.019  INFO 38808 --- [x6SsSoOn6XStg-1] com.netflix.discovery.DiscoveryClient    : Application version is -1: true2018-04-19 18:35:47.019  INFO 38808 --- [x6SsSoOn6XStg-1] com.netflix.discovery.DiscoveryClient    : Getting all instance registry info from the eureka server2018-04-19 18:35:47.031  INFO 38808 --- [x6SsSoOn6XStg-1] com.netflix.discovery.DiscoveryClient    : The response status is 2002018-04-19 18:35:47.034  INFO 38808 --- [x6SsSoOn6XStg-1] com.netflix.discovery.DiscoveryClient    : Not registering with Eureka server per configuration2018-04-19 18:35:47.035  INFO 38808 --- [x6SsSoOn6XStg-1] com.netflix.discovery.DiscoveryClient    : Discovery Client initialized at timestamp 1524134147035 with initial instances count: 32018-04-19 18:35:47.287  INFO 38808 --- [x6SsSoOn6XStg-1] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at: http://172.16.106.93:12000/2018-04-19 18:35:51.550  INFO 38808 --- [x6SsSoOn6XStg-1] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=config-client, profiles=[dev], label=master, version=77913e8aed0054fd05fe6889db20baf96e781632, state=null2018-04-19 18:35:51.550  INFO 38808 --- [x6SsSoOn6XStg-1] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name=‘configService‘, propertySources=[MapPropertySource {name=‘configClient‘}, MapPropertySource {name=‘https://github.com/zhaoyibo/spring-cloud-study/config-repo/config-client-dev.yml‘}]}2018-04-19 18:35:51.553  INFO 38808 --- [x6SsSoOn6XStg-1] o.s.boot.SpringApplication               : No active profile set, falling back to default profiles: default2018-04-19 18:35:51.557  INFO 38808 --- [x6SsSoOn6XStg-1] s.c.a.AnnotationConfigApplicationContext : Refreshing org.spring[email protected]5c32759e: startup date [Thu Apr 19 18:35:51 CST 2018]; parent: org.spring[email protected]43b9bce72018-04-19 18:35:51.569  INFO 38808 --- [x6SsSoOn6XStg-1] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 ‘javax.inject.Inject‘ annotation found and supported for autowiring2018-04-19 18:35:51.626  INFO 38808 --- [x6SsSoOn6XStg-1] o.s.boot.SpringApplication               : Started application in 6.011 seconds (JVM running for 312.876)2018-04-19 18:35:51.627  INFO 38808 --- [x6SsSoOn6XStg-1] s.c.a.AnnotationConfigApplicationContext : Closing org.spring[email protected]5c32759e: startup date [Thu Apr 19 18:35:51 CST 2018]; parent: org.spring[email protected]43b9bce72018-04-19 18:35:51.630  INFO 38808 --- [x6SsSoOn6XStg-1] s.c.a.AnnotationConfigApplicationContext : Closing org.spring[email protected]43b9bce7: startup date [Thu Apr 19 18:35:45 CST 2018]; root of context hierarchy2018-04-19 18:35:51.630  INFO 38808 --- [x6SsSoOn6XStg-1] com.netflix.discovery.DiscoveryClient    : Shutting down DiscoveryClient ...2018-04-19 18:35:51.641  INFO 38808 --- [x6SsSoOn6XStg-1] com.netflix.discovery.DiscoveryClient    : Completed shut down of DiscoveryClient2018-04-19 18:35:51.839  INFO 38808 --- [x6SsSoOn6XStg-1] com.netflix.discovery.DiscoveryClient    : Shutting down DiscoveryClient ...2018-04-19 18:35:51.840  INFO 38808 --- [x6SsSoOn6XStg-1] o.s.c.n.eureka.InstanceInfoFactory       : Setting initial instance status as: STARTING2018-04-19 18:35:54.848  INFO 38808 --- [x6SsSoOn6XStg-1] com.netflix.discovery.DiscoveryClient    : Unregistering ...2018-04-19 18:35:54.854  INFO 38808 --- [x6SsSoOn6XStg-1] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_CONFIG-CLIENT/172.16.106.93:config-client:13001 - deregister  status: 2002018-04-19 18:35:54.863  INFO 38808 --- [x6SsSoOn6XStg-1] com.netflix.discovery.DiscoveryClient    : Completed shut down of DiscoveryClient2018-04-19 18:35:54.865  INFO 38808 --- [x6SsSoOn6XStg-1] com.netflix.discovery.DiscoveryClient    : Initializing Eureka in region us-east-12018-04-19 18:35:54.867  INFO 38808 --- [x6SsSoOn6XStg-1] c.n.d.provider.DiscoveryJerseyProvider   : Using JSON encoding codec LegacyJacksonJson2018-04-19 18:35:54.867  INFO 38808 --- [x6SsSoOn6XStg-1] c.n.d.provider.DiscoveryJerseyProvider   : Using JSON decoding codec LegacyJacksonJson2018-04-19 18:35:54.867  INFO 38808 --- [x6SsSoOn6XStg-1] c.n.d.provider.DiscoveryJerseyProvider   : Using XML encoding codec XStreamXml2018-04-19 18:35:54.867  INFO 38808 --- [x6SsSoOn6XStg-1] c.n.d.provider.DiscoveryJerseyProvider   : Using XML decoding codec XStreamXml2018-04-19 18:35:54.937  INFO 38808 --- [x6SsSoOn6XStg-1] c.n.d.s.r.aws.ConfigClusterResolver      : Resolving eureka endpoints via configuration2018-04-19 18:35:54.938  INFO 38808 --- [x6SsSoOn6XStg-1] com.netflix.discovery.DiscoveryClient    : Disable delta property : false2018-04-19 18:35:54.938  INFO 38808 --- [x6SsSoOn6XStg-1] com.netflix.discovery.DiscoveryClient    : Single vip registry refresh property : null2018-04-19 18:35:54.938  INFO 38808 --- [x6SsSoOn6XStg-1] com.netflix.discovery.DiscoveryClient    : Force full registry fetch : false2018-04-19 18:35:54.938  INFO 38808 --- [x6SsSoOn6XStg-1] com.netflix.discovery.DiscoveryClient    : Application is null : false2018-04-19 18:35:54.938  INFO 38808 --- [x6SsSoOn6XStg-1] com.netflix.discovery.DiscoveryClient    : Registered Applications size is zero : true2018-04-19 18:35:54.939  INFO 38808 --- [x6SsSoOn6XStg-1] com.netflix.discovery.DiscoveryClient    : Application version is -1: true2018-04-19 18:35:54.939  INFO 38808 --- [x6SsSoOn6XStg-1] com.netflix.discovery.DiscoveryClient    : Getting all instance registry info from the eureka server2018-04-19 18:35:54.949  INFO 38808 --- [x6SsSoOn6XStg-1] com.netflix.discovery.DiscoveryClient    : The response status is 2002018-04-19 18:35:54.951  INFO 38808 --- [x6SsSoOn6XStg-1] com.netflix.discovery.DiscoveryClient    : Starting heartbeat executor: renew interval is: 302018-04-19 18:35:54.952  INFO 38808 --- [x6SsSoOn6XStg-1] c.n.discovery.InstanceInfoReplicator     : InstanceInfoReplicator onDemand update allowed rate per min is 42018-04-19 18:35:54.953  INFO 38808 --- [x6SsSoOn6XStg-1] com.netflix.discovery.DiscoveryClient    : Discovery Client initialized at timestamp 1524134154953 with initial instances count: 32018-04-19 18:35:54.954  INFO 38808 --- [x6SsSoOn6XStg-1] o.s.c.n.e.s.EurekaServiceRegistry        : Unregistering application config-client with eureka with status DOWN2018-04-19 18:35:54.975  INFO 38808 --- [x6SsSoOn6XStg-1] o.s.c.n.e.s.EurekaServiceRegistry        : Registering application config-client with eureka with status UP2018-04-19 18:35:54.977  WARN 38808 --- [x6SsSoOn6XStg-1] com.netflix.discovery.DiscoveryClient    : Saw local status change event StatusChangeEvent [timestamp=1524134154977, current=UP, previous=DOWN]2018-04-19 18:35:54.980  INFO 38808 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_CONFIG-CLIENT/172.16.106.93:config-client:13001: registering service...2018-04-19 18:35:54.986  INFO 38808 --- [x6SsSoOn6XStg-1] o.s.cloud.bus.event.RefreshListener      : Received remote refresh request. Keys refreshed [config.client.version, info.profile]2018-04-19 18:35:54.999  INFO 38808 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_CONFIG-CLIENT/172.16.106.93:config-client:13001 - registration status: 204

思考

这里我们再想一个问题:如果对客户端使用 /actuator/bus-refresh会发生什么呢?是只刷新了当前客户端还是刷新全部客户端,还是一个都没刷新呢?

不如来试一下吧:

首先我们需要把客户端上的bus-refresh端点给放出来

复制

12345
management:  endpoints:    web:      exposure:        include: bus-refresh

重启客户端,这时候访问 http://localhost:13000/info 和 http://localhost:13001/info 返回的都是dev bus

去 Git 里把配置信息由dev bus改为dev,然后执行

复制

1
curl -X POST http://localhost:13000/actuator/bus-refresh/

再次访问 http://localhost:13000/info 和 http://localhost:13001/info 发现这时返回的都是dev了,说明只要开启 Spring Cloud Bus 后,不管是对 config-server 还是 config-client 执行/actuator/bus-refresh都是可以更新配置的。

其它

因为版本问题,以下两部分还未在该版本进行验证。待有时间了试一下。

局部刷新

某些场景下(例如灰度发布),我们可能只想刷新部分微服务的配置,此时可通过/actuator/bus-refresh/{destination}端点的 destination 参数来定位要刷新的应用程序。

例如:/actuator/bus-refresh/customers:8000,这样消息总线上的微服务实例就会根据 destination 参数的值来判断是否需要要刷新。其中,customers:8000指的是各个微服务的 ApplicationContext ID。

destination 参数也可以用来定位特定的微服务。例如:/actuator/bus-refresh/customers:**,这样就可以触发 customers 微服务所有实例的配置刷新。

跟踪总线事件

一些场景下,我们可能希望知道 Spring Cloud Bus 事件传播的细节。此时,我们可以跟踪总线事件(RemoteApplicationEvent 的子类都是总线事件)。

跟踪总线事件非常简单,只需设置spring.cloud.bus.trace.enabled=true,这样在/actuator/bus-refresh端点被请求后,访问 /trace (现在好像是/actuator/httptrace)端点就可获得 类似如下的结果

复制

123456789101112131415161718192021222324252627282930
{  "timestamp": 1495851419032,  "info": {    "signal": "spring.cloud.bus.ack",    "type": "RefreshRemoteApplicationEvent",    "id": "c4d374b7-58ea-4928-a312-31984def293b",    "origin": "stores:8002",    "destination": "*:**"  }  },  {  "timestamp": 1495851419033,  "info": {    "signal": "spring.cloud.bus.sent",    "type": "RefreshRemoteApplicationEvent",    "id": "c4d374b7-58ea-4928-a312-31984def293b",    "origin": "spring-cloud-config-client:8001",    "destination": "*:**"  }  },  {  "timestamp": 1495851422175,  "info": {    "signal": "spring.cloud.bus.ack",    "type": "RefreshRemoteApplicationEvent",    "id": "c4d374b7-58ea-4928-a312-31984def293b",    "origin": "customers:8001",    "destination": "*:**"  }}

这个日志显示了customers:8001发出了 RefreshRemoteApplicationEvent 事件,广播给所有的服务,被customers:9000stores:8081接受到了。想要对接受到的消息自定义自己的处理方式的话,可以添加@EventListener注解的 AckRemoteApplicationEvent 和 SentApplicationEvent 类型到你自己的应用中。或者到 TraceRepository 类中,直接处理数据。

这样,我们就可清晰地知道事件的传播细节。

相关阅读

Spring Cloud(一):服务治理技术概览
Spring Cloud(二):服务注册与发现 Eureka
Spring Cloud(三):服务提供与调用 Eureka
Spring Cloud(四):服务容错保护 Hystrix
Spring Cloud(五):Hystrix 监控面板
Spring Cloud(六):Hystrix 监控数据聚合 Turbine
Spring Cloud(七):配置中心(Git 版与动态刷新)
Spring Cloud(八):配置中心(服务化与高可用)
Spring Cloud(九):配置中心(消息总线)
Spring Cloud(十):服务网关 Zuul(路由)
Spring Cloud(十一):服务网关 Zuul(过滤器)
Spring Cloud(十二):分布式链路跟踪(Sleuth 与 Zipkin)

示例代码:GitHub

参考

springcloud(九):配置中心和消息总线(配置中心终结版)
Spring Cloud - Push Notifications and Spring Cloud Bus
Refreshable Configuration using Spring Cloud Config Server, Spring Cloud Bus, RabbitMQ and Git
springboot 2.0.0.RELEASE +spring cloud Finchley.M7 +springcloud bus+kafka 实现配置动态刷新
Config Server——使用 Spring Cloud Bus 自动刷新配置

原文地址:https://www.cnblogs.com/chenweida/p/9025569.html

时间: 2024-08-05 02:25:00

Spring Cloud(九):配置中心(消息总线)【Finchley 版】的相关文章

Spring Cloud学习--配置中心(Config)

Spring Cloud学习--配置中心(Config) 一 Spring Cloud Config简介 二 编写 Config Server 三 编写Config Client 四 使用refresh端点手动刷新配置 五 Spring Config Server与Eurelka配合使用 六 Config Server的高可用 一. Spring Cloud Config简介 微服务要实现集中管理微服务配置.不同环境不同配置.运行期间也可动态调整.配置修改后可以自动更新的需求,Spring Cl

Spring Cloud Config 配置中心

1.构建config-server 创建一个pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://

用Zookeeper作为Spring cloud的配置中心(转)

本文转自https://blog.csdn.net/CSDN_Stephen/article/details/78856323 Spring Cloud 配置中心的主流实现方式 Spring cloud configSpring cloud zookeeper config以下是这两者的简介 Srping Cloud Config Spring cloud config就是和git(svn)集成来实现配置中心.Spring cloud config分服务端.客户端和git(svn)三部分,服务端

微服务SpringCloud之Spring Cloud Config配置中心SVN

在回来的路上看到一个个的都抱着花,吃了一路的狗粮,原本想着去旁边的工业园里跑跑步呢,想想还是算了,人家过七夕,俺们过巴西.上一博客学习了Spring Cloud Config使用git作为配置中心,本篇学习下使用svn作为配置中心. 一.Server 端 1.准备配置文件 这里在本地电脑安装了下svn server,并在https://cuiyw/svn/config-repo/config目录下提交了上一博客的3个配置文件. 2.创建Spring Cloud Config SVN  Serve

spring cloud 的配置中心config

为啥要配置中心? 微服务架构中,每个项目都有一个yml配置,管理起来麻烦.要使用spring cloud config来统一管理. 它支持配置服务放在配置服务的内存中(即本地),也支持放在远程码云(码云中国的github)仓库中. 在spring cloud config 组件中,分两个角色,一是config server(配置中心),二是config client(客户端配置). 1.1.1. 架构 操作 一:操作码云 1.在码云中建立仓库 下一步 最好选择私有,据说访问要快些 下一步,在新建

Spring Cloud Config 配置中心 自动加解密功能

使用  jasypt-spring-boot-starter  进行加解密功能. 整个流程说明: 配置一个 spring cloud config server ,将要使用的配置文件存放到github上,然后从这个配置源拿配置. 我们使用 jasypt 进行自动加解密,将需要加密的数据,通过jasypt进行加密,然后将该内容放入 github.如下图: 使用 ENC() 将加密后的原文包裹,这样spring cloud config client 客户端拿到这个串之后,会自动解密,拿到原文. 下

Spring Cloud 核心组件——注册中心

1. 什么是微服务的注册中心 注册中心:服务管理,核心是有个服务注册表,心跳机制动态维护. 为什么要用? 微服务应用和机器越来越多,调用方需要知道接口的网络地址,如果靠配置文件的方式去控制网络地址,对于动态新增机器,维护带来很大问题. 主流的注册中心:Zookeeper.Eureka.Consul.ETCD 等. 服务提供者 Provider:启动的时候向注册中心上报自己的网络信息. 服务消费者 Consumer:启动的时候向注册中心上报自己的网络信息,拉取 Provider 的相关网络信息.

基于Spring Cloud的微服务构建学习-3 Spring Cloud Eureka配置详解

配置详解 在Eureka的服务治理体系中,主要分为服务端与客户端.服务端为服务注册中心,而客户端为各个提供接口的微服务应用.当部署高可用注册中心时,每个服务端也已经成为了客户端,因此,在使用Spring Cloud Eureka的过程中,我们所做的配置内容几乎都是对Eureka客户端配置进行的操作,所以了解这部分的配置内容,对于用好Eureka非常有帮助. 而Eureka服务端更多类似于一个现成产品,大多数情况下,我们不需要修改它的配置信息. Eureka客户端配置分类 服务注册相关配置,包括服

Spring Cloud Eureka 配置

实例名配置       在Netflix Eureka的原生实现中,实例名采用主机名作为默认值,这样的设置使得在同一主机上无法启动多个相同的实例,所以在Spring Cloud Eureka的配置中,针对同一主机启动多个实例的情况,采用如下规则: eureka.instance.instanceId=${spring.application.name}:${random.int} 利用应用名加随机数的方式来区分不同的实例,从而实现同一主机上,不指定端口就能轻松启动多个实例的效果. 端点配置  

spring cloud 分布式配置(使用git进行远程配置)

我们使用分布式架构 搭建项目时 就比如说我们更改了数据库的密码 那如果有十几个微服务配置在不同的服务器上 我们是不是得一个一个服务器的去更改 那样就相当的麻烦 不光麻烦 还及其容易错 所以基本是不可能这样实现 这里有一个解决方式 可以把项目的配置放到gitlab上 从gitlab来读取 这样就方便了我们的配置 那么就要登陆到gitlab上创建账号 发布项目 等等 这些东西可以到 https://blog.csdn.net/Adelly/article/details/79099772 这个教程看