centos安装RabbitMQ和spring cloud使用RebbitMQ发送和接收消息

本文档介绍了在CentOS7环境下如何安装RabbitMQ,包括启用management插件,设置用户权限以及启动服务。接着展示了Spring Cloud应用中如何使用RabbitMQ进行消息收发,包括添加依赖、配置队列Bean,以及解决消费者监听异常问题的方法。提供了详细的命令行操作和代码示例。

centos安装rabbitmq:

spring cloud使用rabbitMQ收发消息简单示例

  • 引入依赖:gradle

    compile(‘org.springframework.cloud:spring-cloud-starter-bus-amqp’)

  • 注入队列bean

    import org.springframework.amqp.core.Queue;
    import org.springframework.context.annotation.Bean;
    
    @Bean
    public Queue helloQueue() {
        return new Queue("hello");
    }
    
  • 发送消息示例:

    import org.springframework.amqp.core.AmqpTemplate;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    @Component
    public class Sender {
    
      @Autowired 
        private AmqpTemplate rabbitTemplate;
        
        public void send() {
            String context = "hello" +new Date();
            System.out.println("sender:"+context);
            this.rabbitTemplate.convertAndSend("hello",context);
        }
    
    }
    
  • 接收消息示例:

    import org.springframework.amqp.rabbit.annotation.RabbitHandler;
    import org.springframework.amqp.rabbit.annotation.RabbitListener;
    import org.springframework.stereotype.Component;
    
    
    @Component
    @RabbitListener(queues="hello",containerFactory="rabbitListenerContainerFactory")
    public class Receiver {
    
        @RabbitHandler
        public void process(String hello) {
            System.out.println("receiver:"+hello);
        }
    }
    

    注意:其中containerFactory="rabbitListenerContainerFactory"是为了解决消费消息时一直报异常:org.springframework.amqp.AmqpException: No method found for class [B ,并且有时无限循环该异常。

    参考文档:https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-amqp

    原因:文档原话:By default, if retries are disabled and the listener throws an exception, the delivery is retried indefinitely. You can modify this behavior in two ways: Set the defaultRequeueRejected property to false so that zero re-deliveries are attempted or throw an AmqpRejectAndDontRequeueException to signal the message should be rejected. The latter is the mechanism used when retries are enabled and the maximum number of delivery attempts is reached.
    大致意思就是,重试(spring.rabbitmq.template.retry.enabled=false)在默认情况下是关闭的,如果没有打开重试,并且消费listener检测到了异常,就相当与当前消息并没有消费,就会一直尝试重试。而导致异常的根本原因就是@RabbitListener管道中消息内容转换出问题。所以解决办法就是手动给message添加Convrter,注册Bean:

    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
      RabbitTemplate template = new RabbitTemplate(connectionFactory);
      template.setMessageConverter(new Jackson2JsonMessageConverter());
      return template;
    }
    
    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
      SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
      factory.setConnectionFactory(connectionFactory);
      factory.setMessageConverter(new Jackson2JsonMessageConverter());
      return factory;
    }
    
  • REST api:

    import java.util.HashMap;
    import java.util.Map;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.mysoft.rabbit.Sender;
    
    @RestController
    public class RabbitController {
    
      @Autowired
      private Sender sender;
      
      @GetMapping(value = "/send")
      public Map<String,Object> sendMessage(){
        Map<String,Object> result = new HashMap<>();
        sender.send();
        result.put("result", true);
        return result;
      }
    }
    
  • 完整代码示例:github

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值