带有 AWS Kinesis 流的 Spring 云总线 @refreshscope

问题描述

我到处都在阅读@RefreshScope 以了解与 RabbitMQ 和 Kafka 一起工作的云总线应用程序。但就我而言,我使用的是 AWS Parameter store。我希望我的所有客户端实例都能自动刷新,而无需在 AWS 控制台上重建服务器。

我从 Paramstore 创建了 AWS Eventbridge 来通知 Kinesis Stream,但我无法弄清楚它如何通知我的所有客户端节点,而不是仅对一个节点(实例)进行负载均衡器刷新。

感谢您提前回复

解决方法

我从未使用过 AWS Eventbridge / Kinesis,但是:

@RefreshScope 属于 Spring Cloud 而不是 AWS。

更准确地说,当 spring boot 云配置服务中的配置发生变化时,使用此范围定义的 bean 将被 spring 重新加载,而无需“动态”重新加载整个应用程序上下文。通常这意味着您不必重新启动应用程序。

现在,应该使用暴露 refresh 端点的执行器部署 Spring Boot 微服务。手动调用此端点将导致所有 @RefreshScope bean 重新加载。

这是 RefreshEndpointsource code


@Endpoint(id = "refresh")
public class RefreshEndpoint {

    private ContextRefresher contextRefresher;

    public RefreshEndpoint(ContextRefresher contextRefresher) {
        this.contextRefresher = contextRefresher;
    }

    @WriteOperation
    public Collection<String> refresh() {
        Set<String> keys = this.contextRefresher.refresh();
        return keys;
    }

}

如您所见,它仅调用 contextRefresher.refresh()ContextRefresher 是一个 bean,您可以将其注入到您的自定义代码中,该代码将侦听来自 AWS Parameter store 的更改(它应该以某种方式直接调用它或者发送一些您可以使用的消息或其他内容)。

如果您使用的是 spring-cloud-bus(免责声明,我从未使用过它)它也会公开 bus-refresh 端点(与我所描述的机制非常相似),请阅读 {{ 3}} 了解更多详情。

,

感谢团队分享信息。

这是我为使其工作所做的工作。将这两个库添加到我的项目中

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-stream-binder-kinesis</artifactId>
        <version>1.1.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-bus</artifactId>
    </dependency>

并将这两个条目添加到 bootstrap.properties

cloud.aws.region.static=us-east-1
cloud.aws.stack.auto = false

并使用此端点刷新(/bus-refresh)