前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Cloud Bus监听服务配置的变化并自动通知其他服务(二)

Spring Cloud Bus监听服务配置的变化并自动通知其他服务(二)

原创
作者头像
堕落飞鸟
发布2023-04-16 07:47:55
5480
发布2023-04-16 07:47:55
举报
文章被收录于专栏:飞鸟的专栏

Spring Cloud Bus提供了一种自动通知服务配置变化的机制。当配置中心中的配置发生更改时,Spring Cloud Bus会自动通知应用程序,告诉它们需要重新加载最新的配置信息。这种机制可以大大简化应用程序的配置管理,并提高系统的可用性。

在本节中,我们将演示如何使用Spring Cloud Bus来监听配置的变化并自动通知其他服务。我们将使用前面提到的Eureka注册中心和Spring Cloud Config Server。

首先,我们需要在配置中心和应用程序中集成Spring Cloud Bus。这与前面所述的过程类似,我们需要添加Spring Cloud Bus和消息代理的依赖项,并配置它们。请参考前面的示例代码。

接下来,我们需要修改配置中心的配置文件,使其在配置更改时发送消息到Spring Cloud Bus。我们可以在配置文件中添加以下属性:

代码语言:javascript
复制
spring.cloud.config.server.git.uri=https://github.com/example/config-repo
spring.cloud.bus.enabled=true
spring.cloud.bus.env.enabled=true
spring.cloud.bus.trace.enabled=true
spring.cloud.bus.refresh.enabled=true

这些属性将启用Spring Cloud Bus,并告诉配置中心在配置更改时向消息代理发送消息。

然后,我们需要在应用程序中添加一个监听器,以便在收到配置更改通知时重新加载配置。可以使用@RefreshScope注释和@Value注释来动态加载配置。以下是示例代码:

代码语言:javascript
复制
@RestController
@RefreshScope
public class ConfigController {

    @Value("${message}")
    private String message;

    @GetMapping("/message")
    public String getMessage() {
        return message;
    }

    @RefreshScope
    @ConfigurationProperties(prefix = "test")
    @Bean
    public TestProperties testProperties() {
        return new TestProperties();
    }

    @Data
    public static class TestProperties {
        private String name;
        private int age;
    }

}

在这个例子中,我们使用@RefreshScope注释告诉Spring Cloud Bus这个类是需要动态刷新的,并使用@Value注释注入配置中心中的message属性的值。我们还使用@ConfigurationProperties注释和@Bean注释来动态加载test属性。

最后,我们需要在应用程序中添加一个Spring Cloud Bus监听器,以便在配置更改时重新加载配置。可以使用@RefreshScope注释和@Value注释来动态加载配置。以下是示例代码:

代码语言:javascript
复制
@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

@Component
@RefreshScope
public class MyConfig {
    @Value("${my.property}")
    private String myProperty;

    public String getMyProperty() {
        return myProperty;
    }
}

@RestController
@RefreshScope
public class MyController {
    @Autowired
    private MyConfig myConfig;

    @GetMapping("/myProperty")
    public String getMyProperty() {
        return myConfig.getMyProperty();
    }
}

@SpringBootApplication
@EnableDiscoveryClient
public class ConfigClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }
}

@Component
public class MyEventHandler {
    private static final Logger LOG = LoggerFactory.getLogger(MyEventHandler.class);

    @EventListener
    public void handleConfigChanged(ConfigChangedEvent event) {
        LOG.info("Received config changed event for: {}", event.getNamespace());
    }
}

在这个示例中,我们创建了一个配置服务器和一个配置客户端,客户端会监听配置变化的事件。我们在配置服务器中定义了一个名为my.property的配置属性,并将其值设置为Hello World。客户端会注入一个名为MyConfig的Bean,并在控制器中使用该Bean来暴露一个/myProperty端点,该端点返回my.property的值。

现在,我们可以通过在配置服务器上更改my.property的值来测试配置变化的事件。我们可以使用如下命令更新配置服务器的配置:

代码语言:javascript
复制
curl -X POST http://localhost:8888/actuator/bus-refresh

这将触发一个配置变化的事件,MyEventHandler将监听此事件并记录日志。我们可以在控制台上看到以下日志输出:

代码语言:javascript
复制
Received config changed event for: application

这表明客户端已经成功地监听到了配置变化的事件,并做出了相应的处理。

在这个示例中,我们没有使用消息代理来传递事件,而是使用了Spring Cloud Bus自带的轻量级实现。当我们在配置服务器上调用/actuator/bus-refresh端点时,Spring Cloud Bus将在所有连接到消息代理的客户端之间广播/actuator/refresh端点。在客户端上,Spring Cloud Bus将调用/actuator/refresh端点来刷新配置。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
微服务引擎 TSE
微服务引擎(Tencent Cloud Service Engine)提供开箱即用的云上全场景微服务解决方案。支持开源增强的云原生注册配置中心(Zookeeper、Nacos 和 Apollo),北极星网格(腾讯自研并开源的 PolarisMesh)、云原生 API 网关(Kong)以及微服务应用托管的弹性微服务平台。微服务引擎完全兼容开源版本的使用方式,在功能、可用性和可运维性等多个方面进行增强。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档