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

实现配置信息的自动更新

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

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

ed257f9e446ce0b736590304a83f9bb1.png

刷新配置信息

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微服务所有实例的配置刷新。

50d57b7ce2516bcb5082d5d567a86339.png

实现配置信息的自动更新

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

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

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

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

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

2405036a774b5a47f1cca926a2312552.png

使用 GitHub 的Webhook

GitHub提供了 Webhook的功能。

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

405983adf1a5b54a57357ace73f726b3.png

使用ngrok进行本地测试

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

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

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

1.下载安装ngrok

ngrok官方提供了免费下载,下载地址为https://ngrok.com/download。

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

$ ./ngrok http 80

2.使用ngrok

使用ngrok来映射8888端口。

./ngrok http 8888

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

ngrok by @inconshreveable(Ctrl+C to quit)Session Status   onlineversion             2.2.8Region          United states(us)web Interface      http://127.0.0.1:4040Forwarding           http://3589c7al.ngrok.io ->localhost:8888Forwarding       https://3589c7al.ngrok.io->localhost:8888Connections           ttl    opn          rtl           rt5            p50p90                         0      1            0.00     0.00           0.000.00

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

此时,就可以将http://3589c7al.ngrok.io/bus/refresh复制到GitHub的Payload URL上。

3.测试

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

auther=waylau.comversion=1.0.1

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

5e89435a8b8f6738d5756d0670c4fa38.png
同时观察micro-weather-config-client-bus的控制台打印信息。2018-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 2018];root of context hierarchy2018-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)2018-01-0716:46:02.508 INFO 8496---[YaoODjeD-zBaA-1] C.c.c.ConfigServicePropertySourceLocator :Fetching config from server at: http://localhost:8888/2018-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=null2018-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='https://github.com/waylau/spring-cloud-microservices-development/config-repo/micro-weather-config-client-bus-dev.properties'}]]2018-01-0716:46:06.508 INFO 8496---[Yao0DjeD-ZBaA-1] o.s.boot.SpringApplication:No active profile set, falling back todefault profiles:default2018-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 2018];parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@6d4af1622018-01-0716:46:06.557 INFO 8496---[Yao0DjeD-zBaA-1] o.s.boot.:started application in 4.882 secondsSpringApplication(JVM running for 17581.216)2018-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 2018];parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@6d4af1622018-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 2018];root of context hierarchy2018-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应用拿到了在配置中心中的配置。

2018-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 2018];root of context hierarchy2018-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)2018-01-0716:46:02.508 INFO 8496---[YaoODjeD-zBaA-1] C.c.c.ConfigServicePropertySourceLocator :Fetching config from server at: http://localhost:8888/2018-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=null2018-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='https://github.com/waylau/spring-cloud-microservices-development/config-repo/micro-weather-config-client-bus-dev.properties'}]]2018-01-0716:46:06.508 INFO 8496---[Yao0DjeD-ZBaA-1] o.s.boot.SpringApplication:No active profile set, falling back todefault profiles:default2018-01-0716:46:06.510 INFO 8496---[YaoODjeD-zBaA-1] s.

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

c5cad3699a894a4143676936a2d1b979.png

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

  1. 下篇文章给大家讲解的是;
  2. 觉得文章不错的朋友可以转发此文关注小编;
  3. 感谢大家的支持!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值