Spring 带 AWS Kinesis 流的云总线 @refreshscope

Spring cloud bus with AWS Kinesis stream @refreshscope

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

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

感谢您提前回复。

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

@RefreshScope 属于 spring 云而不是 AWS。

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

现在,spring 引导微服务应与公开 refresh 端点的执行器一起部署。手动调用此端点将导致所有 @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 参数存储的更改(它应该以某种方式调用它直接或者发送一些你可以消费的消息或其他东西)。

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

感谢团队分享信息。

以下是我为让它发挥作用所做的工作。将这两个库添加到我的项目中

    <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 = 假

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