失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 微服务架构开发实战:分布式消息总线 实现配置信息的自动更新

微服务架构开发实战:分布式消息总线 实现配置信息的自动更新

时间:2020-02-09 13:23:39

相关推荐

微服务架构开发实战:分布式消息总线 实现配置信息的自动更新

实现配置信息的自动更新

在上一篇文章中节演示了集成Spring Cloud Bus 的过程。在示例中,当微服务实例启动的时候,可以去加载最新的配置信息。当时这种做法有一定的局限性,即只有在应用启动的过程中才能获取到配置。

本节将演示如何基于Spring Cloud Bus来实现配置信息的自动更新。

刷新配置信息

Spring Cloud Bus提供了多种方式来更新微服务实例的配置信息。总结如下。

1.使用/refresh方法

使用/refresh方法,可以更新单个微服务实例配置。

例如,微服务实例

micro-weather-config-client-bus部署在8080端口,则发送POST请求到http://localhost:8080/refresh,可以触发该微服务实例,去获取最新的配置信息。

2.使用/bus/refresh方法

同样地,发送POST请求到

http:/localhost:8080/bus/refresh,可以触发该微服务实例,去获取最新的配置信息。

同时,使用/bus/refresh方法,可以更新多个微服务实例的配置信息。例如,在8081和8082上都部署了微服务实例,当使用/bus/refresh方法在任意一个微服务实例上触发时,另外一个微服务实例也能自动更新。这就是Spring Cloud Bus所带来的好处,让更新信息在多个微服务实例之间进行广播,从而能够通知到所有的微服务实例。

一般当微服务的配置需要更新时,并不会在每个微服务实例上去触发更新信息,而是去触发配置服务器上的/bus/refresh方法,从而将更新事件发送给所有的微服务实例。

例如,按照下面的方式分别来部署注册中心、配置服务器。

java -jar micro-weather-eureka-server-1.0.0.jar --server.port=8761java -jar micro-weather-config-server-bus-1.0.0.jar --server.port=8888java -jarmicro-weather-config-client-bus-1.0.0.jar --server.port=8081java -jar micro-weather-config-client-bus-1.0.0.jar --server.port=8082

当配置信息变更时,发送POST请求到

http:/localhost:8888/bus/refresh,即可更新所有的微服务实例的配置信息。

3.局部刷新

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

例如,bus/refresh?destination=

micro-weather-config-client-bus:8080,这样消息总线上的微服务实例就会根据destination参数的值来判断是否需要刷新。其中,micro-weather-config-client-bus:8080指的是各个微服务的ApplicationContext ID。

destination参数也可以用来定位特定的微服务。例如,/bus/refresh?destination=

micro-weath-er-config-client-bus:**,这样就可以触发micro-weather-config-client-bus微服务所有实例的配置刷新。

实现配置信息的自动更新

虽然使用触发/bus/refresh请求到配置服务器,可以避免手动刷新微服务实例配置的烦琐过程,但该触发过程仍然是手动的。是否可以自动来刷新配置呢?比如,当配置的Git仓库中变更了,可否能够及时通知到配置服务器呢?当然是肯定的,借助Git仓库的Webhook功能就能实现这个目的。

现在虽然可以不用重启服务就能更新配置,但还是需要手动操作,这样是不可取的。所以,这里就要用到Git的Webhook来达到自动更新配置。

图16-7展示了配置信息的自动更新的整个过程:

将配置修改信息推送到Git仓库;当Git仓库接收到配置信息之后,会通过Webhook发送/bus/refresh到 Bus;Bus发送变更事件给所有的微服务实例;微服务实例从配置中心获取到最新的配置。

当然,这里的配置中心和Bus有可能是同一个应用,就像本节所演示的案例。

使用 GitHub 的Webhook

GitHub提供了 Webhook的功能。

如图16-8所示,在GitHub的Payload URL填写相应的配置中心触发刷新的地址即可。URL必须是真实可用的,不能写localhost,因为无法从外网访问到。

使用ngrok进行本地测试

既然GitHub无法从外网来访问本地的服务,那如何在本地开发环境中进行测试呢?毕竟用户不能在本地搭建GitHub。此时,就需要ngrok来帮忙。

ngrok是一个反向代理,通过在公共的端点和本地运行的Web服务器之间建立一个安全的通道。

ngrok可捕获和分析所有通道上的流量,便于后期分析和重放。简单来说,就是通过ngrok建立一个隧道,让用户在外网也可以访问自己本地的计算机,这就是所谓的反向代理。

1.下载安装ngrok

ngrok官方提供了免费下载,下载地址为

/download。

下载包解压之后,即可指定端口号进行使用。

$ ./ngrok http 80

2.使用ngrok

使用ngrok来映射8888端口。

./ngrok http 8888

启动后,能看到如下信息。

ngrok by @inconshreveable(Ctrl+C to quit)Session Status onlineversion2.2.8RegionUnited states(us)web Interface http://127.0.0.1:4040Forwardinghttp://3589c7al.ngrok.io ->localhost:8888Forwarding https://3589c7al.ngrok.io->localhost:8888Connectionsttl opnrtlrt5p50p90 0 10.00 0.000.000.00

其中,Forwarding就是反射的过程。这里随机生成的3589c7al.ngrok.io域名,就是映射到本地127.0.0.1:4567地址。当然,每台机器上生成的域名都是不同的。

此时,就可以将

http://3589c7al.ngrok.io/bus/refresh复制到GitHub的Payload URL上。

3.测试

用户修改Git配置信息,并推送到Git仓库中。

auther=version=1.0.1

登录GitHub,可以看到如图16-9所示的Webhook 执行记录。

同时观察micro-weather-config-client-bus的控制台打印信息。-01-07 16:46:01.968 INFO 8496---[YaoODjeD-zBaA-1] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@6d4af162:startupdate [Sun Jan 07 16:46:01 CST ];root of context hierarchy-01-07 16:46:02.039 INFO 8496---[YaoODjeD-zBaA-1] trationDelegate$BeanPostProcessorChecker : Bean 'configurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$d12ac107] is not eligible for getting processed by all BeanPostProcessors(for example: not eligible for auto-proxying)-01-0716:46:02.508 INFO 8496---[YaoODjeD-zBaA-1] C.c.c.ConfigServicePropertySourceLocator :Fetching config from server at: http://localhost:8888/-01-07 16:46:06.506 INFO 8496---[YaoODjeD-zBaA-1] C.c.c.ConfigServicePropertySourceLocator : Located environment: name=micro-weather-config-client-bus,profiles=[dev],label=null,version=0ef59a4369cef35c9985115e1492929b517afla, state=null-01-07 16:46:06.506 INFO 8496---[YaoODjeD-zBaA-1] b.c.PropertySourceBootstrapConfiguration : Located property source:CompositePropertySource [name=' configService', propertySources=[MapPropertySource {name=' configClient'},MapPropertySource{name='/waylau/spring-cloud-microservices-development/config-repo/micro-weather-config-client-bus-dev.properties'}]]-01-0716:46:06.508 INFO 8496---[Yao0DjeD-ZBaA-1] o.s.boot.SpringApplication:No active profile set, falling back todefault profiles:default-01-0716:46:06.510 INFO 8496---[YaoODjeD-zBaA-1] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@75c0870d: startupdate [Sun Jan 07 16:46:06 CST ];parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@6d4af162-01-0716:46:06.557 INFO 8496---[Yao0DjeD-zBaA-1] o.s.boot.:started application in 4.882 secondsSpringApplication(JVM running for 17581.216)-01-07 16:46:06.558 INFO 8496---[Yao0DjeD-zBaA-1] s.c.a.AnnotationConfigApplicationContext :Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@75c0870d: startupdate [Sun Jan 07 16:46:06CST ];parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@6d4af162-01-07 16:46:06.558 INFO 8496--- [Yao0DjeD-zBaA-1] s.c.a.AnnotationConfigApplicationContext :Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@6d4af162:startupdate [Sun Jan 07 16:46:01 CST ];root of context hierarchy-01-07 16:46:06.893 INFO 8496---[YaoODjeD-zBaA-1] 0.s.cloud.bus.event.RefreshListener:Received remote refresh request. Keysrefreshed [config.client.version,version]

控制台记录了整个应用更新配置的过程。

浏览器访问

http:/localhost:8081/config,显示如下信息,则说明micro-weather-config-client-bus应用拿到了在配置中心中的配置。

-01-07 16:46:01.968 INFO 8496---[YaoODjeD-zBaA-1] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@6d4af162:startupdate [Sun Jan 07 16:46:01 CST ];root of context hierarchy-01-07 16:46:02.039 INFO 8496---[YaoODjeD-zBaA-1] trationDelegate$BeanPostProcessorChecker : Bean 'configurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$d12ac107] is not eligible for getting processed by all BeanPostProcessors(for example: not eligible for auto-proxying)-01-0716:46:02.508 INFO 8496---[YaoODjeD-zBaA-1] C.c.c.ConfigServicePropertySourceLocator :Fetching config from server at: http://localhost:8888/-01-07 16:46:06.506 INFO 8496---[YaoODjeD-zBaA-1] C.c.c.ConfigServicePropertySourceLocator : Located environment: name=micro-weather-config-client-bus,profiles=[dev],label=null,version=0ef59a4369cef35c9985115e1492929b517afla, state=null-01-07 16:46:06.506 INFO 8496---[YaoODjeD-zBaA-1] b.c.PropertySourceBootstrapConfiguration : Located property source:CompositePropertySource [name=' configService', propertySources=[MapPropertySource {name=' configClient'},MapPropertySource{name='/waylau/spring-cloud-microservices-development/config-repo/micro-weather-config-client-bus-dev.properties'}]]-01-0716:46:06.508 INFO 8496---[Yao0DjeD-ZBaA-1] o.s.boot.SpringApplication:No active profile set, falling back todefault profiles:default-01-0716:46:06.510 INFO 8496---[YaoODjeD-zBaA-1] s.

以上就是实现配置信息自动更新的完整过程。

本篇文章内容给大家讲解的是实现配置信息的自动更新

下篇文章给大家讲解的是;觉得文章不错的朋友可以转发此文关注小编;感谢大家的支持!

如果觉得《微服务架构开发实战:分布式消息总线 实现配置信息的自动更新》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。