使用Spring Cloud + Spring ampq + Spring Boot + RabbitMQ

时间:2016-09-14 14:32:53

标签: spring-boot rabbitmq spring-cloud spring-amqp

我正在使用Spring Boot和RabbitMQ开发基于Spring云的应用程序。我正在尝试使用FanoutExchange在RabbitMQ中创建一个交换。 我的要求是向多个队列发送相同的消息,因此我使用的是FanoutExchange 但是将异常视为:

Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; 
protocol method: #method<channel.close>(reply-code=406, reply-text=PRECONDITION_FAILED
 - inequivalent arg 'type' for exchange 'rkk_exchange1' in vhost  
'': received 'fanout' but current is 'topic', 
class-id=40, method-id=10)

下面是代码(配置文件):

@Configuration
@Profile("cloud")
public class MultiListenerRabbitConfig extends AbstractCloudConfig{

private static final String QUEUE_NAME1="rk.rkk_queue1";
private static final String QUEUE_NAME2="rk.rkk_queue2";
private static final String EXCHANGE_NAME="rkk_exchange1";
private static final int PORT = 1;


@Bean
public CachingConnectionFactory cachingConnectionFactory(){

    CloudFactory cloudFactory = new CloudFactory();
    Cloud cloud = cloudFactory.getCloud();
    List<ServiceInfo> listServices = cloud.getServiceInfos();
    AmqpServiceInfo serviceInfo = (AmqpServiceInfo) cloud.getServiceInfo(cloud.getServiceInfos().get(0).getId());


    CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
    cachingConnectionFactory.setUsername(serviceInfo.getUserName());
    cachingConnectionFactory.setPassword(serviceInfo.getPassword());
    cachingConnectionFactory.setVirtualHost(serviceInfo.getVirtualHost());
    cachingConnectionFactory.setHost(serviceInfo.getHost());
    cachingConnectionFactory.setPort(PORT);
    cachingConnectionFactory.setRequestedHeartBeat(30);
    cachingConnectionFactory.setConnectionTimeout(30000);
    return cachingConnectionFactory;
}

@Bean
public RabbitTemplate rabbitTemplate(){
    System.out.println("-----------------**************------------------ Create Rabbit Template");
    RabbitTemplate template = new RabbitTemplate(cachingConnectionFactory());

    return template;
}


@Bean
public RabbitAdmin rabbitAdmin(){
    //Set up queue, exchanges and bindings
    RabbitAdmin admin = new RabbitAdmin(cachingConnectionFactory());
    Queue queue1 = new Queue(QUEUE_NAME1);
    admin.declareQueue(queue1);
    //For a Fanout Exchange, any Queue bound to that Exchange will receive any message sent to that Exchange
    FanoutExchange exchange = new FanoutExchange(EXCHANGE_NAME);
    admin.declareExchange(exchange);
    Binding binding = BindingBuilder.bind(queue1).to(exchange);
    admin.declareBinding(binding);


    return admin;
}
}

放置堆栈跟踪:

  

ERR引起:java.io.IOException   2016-09-14T19:42:02.68 + 0530 [App / 0] ERR at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:106)   2016-09-14T19:42:02.68 + 0530 [App / 0] ERR at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:102)   2016-09-14T19:42:02.68 + 0530 [App / 0] ERR at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:124)   2016-09-14T19:42:02.68 + 0530 [App / 0] ERR at com.rabbitmq.client.impl.ChannelN.exchangeDeclare(ChannelN.java:693)   2016-09-14T19:42:02.68 + 0530 [App / 0] ERR at com.rabbitmq.client.impl.ChannelN.exchangeDeclare(ChannelN.java:662)   2016-09-14T19:42:02.68 + 0530 [App / 0] ERR at com.rabbitmq.client.impl.ChannelN.exchangeDeclare(ChannelN.java:61)   2016-09-14T19:42:02.68 + 0530 [App / 0] sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)的ERR   2016-09-14T19:42:02.68 + 0530 [App / 0] sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)的ERR   2016-09-14T19:42:02.68 + 0530 [App / 0] sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)的ERR   2016-09-14T19:42:02.68 + 0530 [App / 0] java.lang.reflect.Method.invoke的ERR(Method.java:498)   2016-09-14T19:42:02.68 + 0530 [App / 0] ERR at org.springframework.amqp.rabbit.connection.CachingConnectionFactory $ CachedChannelInvocationHandler.invoke(CachingConnectionFactory.java:625)   2016-09-14T19:42:02.68 + 0530 [App / 0] com.sun.proxy的ERR。$ Proxy50.exchangeDeclare(未知来源)   2016-09-14T19:42:02.68 + 0530 [App / 0] ERR at org.springframework.amqp.rabbit.core.RabbitAdmin.declareExchanges(RabbitAdmin.java:422)   2016-09-14T19:42:02.68 + 0530 [App / 0] ERR at org.springframework.amqp.rabbit.core.RabbitAdmin.access $ 000(RabbitAdmin.java:54)   2016-09-14T19:42:02.68 + 0530 [App / 0] ERR at org.springframework.amqp.rabbit.core.RabbitAdmin $ 1.doInRabbit(RabbitAdmin.java:111)   2016-09-14T19:42:02.68 + 0530 [App / 0] ERR at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:1045)   2016-09-14T19:42:02.68 + 0530 [App / 0] ERR ...... 38更多   2016-09-14T19:42:02.68 + 0530 [App / 0] ERR引起:com.rabbitmq.client.ShutdownSignalException:通道错误;协议方法:#method(回复代码= 406,回复文本= PRECONDITION_FAILED - 不等效的arg&#39;类型&#39;用于交换&#39; rkk_exchange1&#39;在vhost&#39;&#39;:收到&#39;粉丝&#39;但是当前的主题是&#39;,class-id = 40,method-id = 10)   2016-09-14T19:42:02.68 + 0530 [App / 0] ERR at com.rabbitmq.utility.ValueOrException.getValue(ValueOrException.java:67)   2016-09-14T19:42:02.68 + 0530 [App / 0] ERR at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:33)   2016-09-14T19:42:02.68 + 0530 [App / 0] ERR at com.rabbitmq.client.impl.AMQChannel $ BlockingRpcContinuation.getReply(AMQChannel.java:343)   2016-09-14T19:42:02.68 + 0530 [App / 0] ERR at com.rabbitmq.client.impl.AMQChannel.privateRpc(AMQChannel.java:216)   2016-09-14T19:42:02.68 + 0530 [App / 0] ERR at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:118)   2016-09-14T19:42:02.68 + 0530 [App / 0] ERR ... 51更多   2016-09-14T19:42:02.68 + 0530 [App / 0] ERR引起:com.rabbitmq.client.ShutdownSignalException:通道错误;协议方法:#method(回复代码= 406,回复文本= PRECONDITION_FAILED - 不等效的arg&#39;类型&#39;用于交换&#39; rkk_exchange1&#39;在vhost&#39;&#39;:收到&#39;粉丝&#39;但是当前的主题是&#39;,class-id = 40,method-id = 10)   2016-09-14T19:42:02.68 + 0530 [App / 0] ERR at com.rabbitmq.client.impl.ChannelN.asyncShutdown(ChannelN.java:478)   2016-09-14T19:42:02.68 + 0530 [App / 0] ERR at com.rabbitmq.client.impl.ChannelN.processAsync(ChannelN.java:315)   2016-09-14T19:42:02.68 + 0530 [App / 0] ERR at com.rabbitmq.client.impl.AMQChannel.handleCompleteInboundCommand(AMQChannel.java:144)   2016-09-14T19:42:02.68 + 0530 [App / 0] ERR at com.rabbitmq.client.impl.AMQChannel.handleFrame(AMQChannel.java:91)   2016-09-14T19:42:02.68 + 0530 [App / 0] ERR at com.rabbitmq.client.impl.AMQConnection $ MainLoop.run(AMQConnection.java:552)   2016-09-14T19:42:02.68 + 0530 [App / 0] ERR ......还有1个   2016-09-14T19:42:02.73 + 0530 [DEA / 19] ERR实例(索引0)未能开始接受连接

注意:此应用正在使用TopicExchange,但我的要求是使用FanoutExchange

1 个答案:

答案 0 :(得分:3)

您无法更改交换类型(它目前是主题交换);你必须删除它并重新添加它。

此外,您应该在@Bean定义中不做工作(声明) - 应用程序上下文尚未构建,可能会导致问题。相反,定义bean然后稍后获取对它的引用来进行声明。

@Bean
public RabbitAdmin rabbitAdmin(){
    return new RabbitAdmin(cachingConnectionFactory());
}

完成上下文后,您需要执行声明。