企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持知识库和私有化部署方案 广告
## 一、前提 之前的章节,我们先后为大家介绍了netflix公司为Spring Cloud社区贡献的一系列技术方案: * eureka实现服务注册中心 * spring cloud config 为我们提供了配置集中管理的能力 * spring cloud bus为我们提供了配置属性在全量实例或者部分实例刷新的能力(我们还没实验,本节就来实验) 所以在正确的完成了eureka client相关配置,config client相关配置,我们的微服务想具备spring cloud bus提供的配置刷新能力,需要做如下的配置 ## 二、为微服务集成Bus提供的配置刷新能力 比如:aservice-rbac和aservice-sms,集成Bus提供的配置刷新能力 ~~~ <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> ~~~ 加上RabbitMQ消息队列的配置(加到git仓库对应微服务的配置文件中) ~~~ spring: rabbitmq: host: 192.168.161.3 port: 5672 username: guest password: guest ~~~ ## 三、测试方法 参考上一章《config客户端配置刷新》章节的测试方法。只不过之前是访问单节点`http://localhost:8401/actuator/refresh`进行单节点配置刷新,现在访问Bus的`http://localhost:8771/actuator/bus-refresh`进行所有实例的配置刷新。 * 先启动多个aservice-rbac实例 * 然后修改user.init.password密码 * 之后请求Bus的`http://localhost:8771/actuator/bus-refresh`进行配置刷新 * 最后在修改密码服务内下断点,看看配置刷新后的默认用户初始化密码是不是已经热更新。(不需要重启) ## 四、局部刷新方式 之前我们讲的`/actuator/bus-refresh`是**刷新所有**在eureka上注册,并且连接了config server微服务实例的配置。假如我们希望局部刷新实例需要使用:`/actuator/bus-refresh/{destination}`,这样消息总线上的微服务实例就会根据destination参数的值来判断某个微服务实例是否需要刷新。 * `/actuator/bus-refresh/aservice-rbac:8401`表示启动端口为8401的aservice-rbac微服务实例的配置将被刷新。 * `/actuator/bus-refresh/aservice-rbac:**`,这样就可以触发aservice-rbac微服务所有实例的配置刷新。 这种刷新方式虽然提供了一定的灵活性,但远远达不到灰度发布的要求,参考本文档的《apollo灰度发布》。所以Spring Cloud Config整体上还是有一定的局限性。 > 默认情况下,ApplicationContext[ID是spring.application.name](http://xn--idspring-0i5q.application.name/):server.port,详见`org.springframework.boot.context.ContextIdApplicationContextInitializer.getApplicationId(ConfigurableEnvironment)`方法。 ## 附录-截至目前为止aservice-rbac的配置文件 以下是本地项目中bootstrap.yml的配置内容,只有config 和eureka的配置信息即可。 ~~~ spring: application: name: aservice-rbac cloud: config: label: master profile: dev username: zimug password: pwd123456 discovery: enabled: true # 开启 service-id: zimug-server-config # 指定配置中心服务端的server-id eureka: client: service-url: defaultZone: http://zimug:centerpwd@peer1:8761/eureka/eureka/,http://zimug:centerpwd@peer2:8761/eureka/eureka/,http://zimug:centerpwd@peer3:8761/eureka/eureka/ instance: preferIpAddress: true ~~~ 以下是远程git仓库中aservice-rbac-dev.yml的所有配置。 ~~~ server: port: 8401 spring: application: name: aservice-rbac jackson: date-format: yyyy-MM-dd HH:mm:ss time-zone: GMT+8 datasource: url: jdbc:mysql://123.56.169.21:3306/linnadb?useUnicode=true&characterEncoding=utf-8&useSSL=false username: linna password: d%r2Tz08 driver-class-name: com.mysql.cj.jdbc.Driver cloud: inetutils: preferredNetworks: - 192.168 ignored-interfaces: - .*VirtualBox.* rabbitmq: host: 192.168.161.3 port: 5672 username: guest password: guest # debug: true mybatis: configuration: map-underscore-to-camel-case: true log-impl: org.apache.ibatis.logging.stdout.StdOutImpl eureka: client: service-url: defaultZone: http://zimug:centerpwd@peer1:8761/eureka/eureka/,http://zimug:centerpwd@peer2:8761/eureka/eureka/,http://zimug:centerpwd@peer3:8761/eureka/eureka/ ribbon: eager-load: enabled: true clients: aservice-sms feign: okhttp: enabled: true logging: level: com.zimug.dongbb.cloud.aservice.rbac.feign.SmsService: debug #aservice-rbac: # ribbon: # NFLoadBalancerRuleClassName: com.zimug.dongbb.cloud.aservice.rbac.config.MyLoadBanlanceRule management: endpoint: shutdown: enabled: false endpoints: web: exposure: include: refresh,health user: init: password: Abcd1234 ~~~