基于SpringBoot2.X 整合 RabbitMQ 简单使用

加依赖先

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>


<dependency>
    <groupId>org.springframework.amqp</groupId>
    <artifactId>spring-rabbit-test</artifactId>
    <scope>test</scope>
</dependency>

application.yml


在启动类上加注解


 

开始使用

/**
 * 自动配置
 *  1、RabbitAutoConfiguration
 *  2、有自动配置了连接工厂ConnectionFactory;
 *  3、RabbitProperties 封装了 RabbitMQ的配置
 *  4、 RabbitTemplate :给RabbitMQ发送和接受消息;
 *  5、 AmqpAdmin : RabbitMQ系统管理功能组件;
 *  	AmqpAdmin:创建和删除 Queue,Exchange,Binding
 *  6、@EnableRabbit +  @RabbitListener 监听消息队列的内容
 *
 */

 

使用AmqpAdmin

     @Autowired
    AmqpAdmin amqpAdmin;        


    /**
     * 创建交换器,消息队列,进行绑定绑定
     */
    @Test
    public void createExchange(){
        amqpAdmin.declareExchange(new FanoutExchange("myExchange.fanout"));
		amqpAdmin.declareExchange(new DirectExchange("amqpadmin.exchange"));
		System.out.println("创建交换器完成");
        this.amqpAdmin.declareQueue(new Queue("myQueue.test"));
		amqpAdmin.declareQueue(new Queue("amqpadmin.queue",true));
        System.out.println("创建消息队列完成");

		//创建绑定规则
        //Binding()方法有五个参数,最后一个可以传null
        this.amqpAdmin.declareBinding(new Binding("myQueue.test",
                Binding.DestinationType.QUEUE,"myExchange.fanout","myampq.haha",null));
        this.amqpAdmin.declareBinding(new Binding("myQueue.test", Binding.DestinationType.QUEUE,
                "exchange.fanout","",null));
		amqpAdmin.declareBinding(new Binding("amqpadmin.queue",
	    	Binding.DestinationType.QUEUE,"amqpadmin.exchange","amqp.haha",null));
    }

使用 RabbitTemplate

    @Autowired
    RabbitTemplate rabbitTemplate;

    /**
     * 1、单播(点对点)
     */
    @Test
    public  void contextLoads() {
        /*Message需要自己构造一个;定义消息体内容和消息头
        rabbitTemplate.send(exchage,routeKey,message);*/

        //object默认当成消息体,只需要传入要发送的对象,自动序列化发送给rabbitmq;
        //rabbitTemplate.convertAndSend(exchage,routeKey,object);
        //此时对象被默认序列化以后发送出去
        this.rabbitTemplate.convertAndSend("myExchange.fanout","myampq.haha",
                new Book("水浒传","施耐庵"));
    }

上面的测试中,对象被默认序列化以后发送出去



如何将数据自动的转为json发送出去

默认是按字节数组的方式发送的

自定义一个消息转换器

在RabbitAutoConfiguration 中,有下图:

说明 我们自己定义的ManagerConvert  会自动生效的

接受消息

    @Autowired
    RabbitTemplate rabbitTemplate;
 
    /**
     * 接受数据
     */
    @Test
    public void receive(){
        Object o = rabbitTemplate.receiveAndConvert("atguigu.news");
        System.out.println(o.getClass());
        System.out.println(o);
        Book book = (Book)this.rabbitTemplate.receiveAndConvert("myQueue.test");
        System.out.println(book.getClass());
        System.out.println(book.toString());
    }

广播方式

    @Autowired
    RabbitTemplate rabbitTemplate;    


     /**
     * 广播
     */
    @Test
    public void sendMsg(){
        rabbitTemplate.convertAndSend("exchange.fanout","",
                new Book("红楼梦","曹雪芹"));
    }

使用注解监听

@Service
public class BookService {

    @RabbitListener(queues = "atguigu.news")
    public void receive(Book book){
        System.out.println("收到消息:"+book);
    }

    /*接受消息头*/
    @RabbitListener(queues = "atguigu")
    public void receive02(Message message){
        System.out.println(message.getBody());
        System.out.println(message.getMessageProperties());
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_42633481/article/details/107730023