2017-12-05 4 views
1

私はウサギRPCを使用するスプリングブートアプリケーションを持っています。 問題は、アプリケーションが起動すると、Rabbitサーバーがダウンしても、アプリケーションはサーバーに再接続しないということです。また、ウサギサーバーがダウンしている間にアプリが起動すると、失敗し、ウサギが再び上がった後にウサギに再接続しようとしません。ここ は、ウサギのサーバーに障害が発生し、再起動した場合にどのように私は、再接続しようとする春ブーツを伝えることができた構成のコードサーバがダウンした後、Rabbit MQに再接続するには?

@Configuration 
public class RabbitConfiguration { 

@Value("${spring.rabbitmq.host}") 
private String rabbitmqHost; 

@Value("${spring.rabbitmq.queue}") 
private String rabbitmqQueue; 

@Value("${spring.rabbitmq.username}") 
private String rabbitmqUsername; 

@Value("${spring.rabbitmq.password}") 
private String rabbitmqPassword; 

private final CommentsServiceRpc commentsServiceRpc; 


public RabbitConfiguration(final CommentsServiceRpc commentsServiceRpc) { 
    this.commentsServiceRpc = commentsServiceRpc; 
} 

@Bean 
public Connection rabbitmqConnection() throws IOException, TimeoutException { 
    ConnectionFactory factory = new ConnectionFactory(); 
    factory.setHost(rabbitmqHost); 
    factory.setUsername(rabbitmqUsername); 
    factory.setPassword(rabbitmqPassword); 
    return factory.newConnection(); 
} 

@Bean 
public Channel rpcChanel() throws IOException, TimeoutException { 
    Connection connection = rabbitmqConnection(); 
    Channel channel = connection.createChannel(); 
    channel.queueDeclare(rabbitmqQueue, false, false, false, null); 
    return channel; 
} 

@Bean("commentsJsonRpcServer") 
@Lazy(false) 
public JsonRpcServer commentsJsonRpcServer() throws IOException, TimeoutException { 
    JsonRpcServer jsonRpcServer = new JsonRpcServer(rpcChanel(), rabbitmqQueue, CommentsServiceRpc.class, 
      commentsServiceRpc); 
    new Thread(() -> { 
     try { 
      jsonRpcServer.mainloop(); 
     } catch (Exception e) { 
      System.out.println(e); 
     } 
    }).start(); 
    return jsonRpcServer; 
} 
} 

のですか?

答えて

0

私は春ブーツについての最初の事を知っているが、this answer looks promisingはありません - 私は同様の問題を持っていた


、あなただけの接続ファクトリの設定にプロパティを配置する必要があります。

here工場出荷時にはfactory.setAutomaticRecoveryEnabled(true);factory.setNetworkRecoveryInterval(10000);と設定されています。ウサギのサーバーがダウンしているか接続が失われた場合、ウサギのクライアントは再接続を試みます。

あなたは接続ファクトリの春の構成を使用しているので、あなたの接続ファクトリは、次の

<bean id="connectionFactory" 
     class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory"> 
    <constructor-arg value="somehost"/> 
    <property name="username" value="guest"/> 
    <property name="password" value="guest"/> 
    <property name="automaticRecoveryEnabled" value="true"/> 
    <property name="networkRecoveryInterval" value="100000"/> 
</bean> 

接続ファクトリ参照のようansweingためhere

+0

おかげだろうが、問題は、接続の回復よりもあると思われます(私は自分のコードを編集しましたが、再接続してもまだ動作しません)。 rpcServer(およびクライアント)のBeanは、接続が再度確立された後は使用できないように見えます。 –

関連する問題