【问题标题】:Spring cloud bus with AWS Kinesis stream @refreshscope带有 AWS Kinesis 流 @refreshscope 的 Spring 云总线
【发布时间】:2021-06-14 09:35:39
【问题描述】:

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

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

感谢您提前回复。

【问题讨论】:

    标签: spring-boot spring-cloud-stream amazon-kinesis aws-parameter-store


    【解决方案1】:

    感谢团队分享信息。

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

        <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)

    【讨论】:

      【解决方案2】:

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

      @RefreshScope 属于 Spring Cloud,不属于 AWS。

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

      现在,Spring Boot 微服务应该与暴露 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 Parameter store 的更改(它应该以某种方式直接调用它,或者可能发送一些你可以消费的消息或其他东西)。

      如果您使用的是 spring-cloud-bus(免责声明,我从未使用过它)它也会暴露 bus-refresh 端点(与我所描述的机制非常相似),请阅读 spring-cloud-bus documentation更多细节。

      【讨论】:

      • 我也从未尝试过,但 Spring Cloud Bus 本质上是基于 Spring Cloud Stream 的,并且由于 AWS Kinesis 有一个 Binder,您只需将其 deps 与 spring-cloud-bus 一起添加:github.com/spring-cloud/spring-cloud-stream-binder-aws-kinesis
      • 感谢您的快速回复。 @MarkBramnik 我正在尝试做同样的事情,但不确定 contextRefresher 将如何知道 AWS Paramstore 中是否发生了任何变化。
      • @ArtemBilan,我浏览了那个链接,但它从来没有说它会帮助 RefreshScope。
      • @SatyaG:这正是难题中缺失的部分。我从未与 Paramstore 合作过,但也许它可以发送一些事件/调用一些休息或任何改变。您必须在 Spring Boot 应用程序中创建一个侦听器,该侦听器将侦听此类事件并基本上调用上下文刷新器。当然假设它在 AWS 中是可行的:)
      猜你喜欢
      • 2018-05-11
      • 2021-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-29
      • 2016-01-26
      • 1970-01-01
      相关资源
      最近更新 更多