【问题标题】:Spring Cloud Stream Automatically reconnect to the Rabbit MQSpring Cloud Stream 自动重连 Rabbitmq
【发布时间】:2017-10-17 09:11:24
【问题描述】:

我有一个连接到 RabbitMQ 服务器的 Spring Cloud Stream 应用程序。我们在 Rabbit MQ 中使用的是 rabbitmq-auth-backend-uaa 插件,但这不是问题。

此插件会检查一个 oAuth2.0 JWT 令牌,该令牌在创建与 Rabbit MQ 的连接时作为用户名发送。

为此,我的 Spring 应用程序中有以下代码:

@Bean
@Primary
ConnectionFactory connectionFactory() throws Exception {

    //These lines get the token from the UAA automatically.
    MyTokenService tokenService = new MyTokenService();
    String token= tokenService.obtainAccessToken(); // HTTP POST request to the UAA

    AbstractConnectionFactory connectionFactory = new CachingConnectionFactory("localhost");
    connectionFactory.setUsername(token);

    return connectionFactory;
}

问题是令牌每小时都会过期,因此必须重新加载连接的用户名(再次调用 tokenService.obtainAccessToken())。

这是自动完成的吗?我如何确定每次都会重新加载连接?

【问题讨论】:

    标签: spring rabbitmq spring-amqp spring-cloud-stream spring-rabbit


    【解决方案1】:

    不,@Bean 定义在上下文初始化期间加载一次。

    您应该能够将ConnectionListener 添加到连接工厂,并将重新配置连接工厂的逻辑放在onClose() 方法中(也许是onShutDown(),从2.0 开始)。

    【讨论】:

      猜你喜欢
      • 2018-01-24
      • 1970-01-01
      • 2019-03-27
      • 2016-12-27
      • 2019-03-16
      • 2017-11-24
      • 2021-03-25
      • 2018-06-20
      • 2020-07-26
      相关资源
      最近更新 更多