RabbitMQ——springboot整合RabbitMQ

Posted 转身刹那的潇洒

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RabbitMQ——springboot整合RabbitMQ相关的知识,希望对你有一定的参考价值。

springboot整合RabbitMQ

环境搭建

一、创建一个Springboot项目

image

二、导入相关依赖

image

或者不勾选Spring for RabbitMQ,自己导入依赖

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

三、编写配置文件

# 应用名称
spring.application.name=rabbitmq_springboot
# 应用服务 WEB 访问端口
server.port=8080

spring.rabbitmq.port=5672
spring.rabbitmq.host=192.168.137.5
spring.rabbitmq.username=admin
spring.rabbitmq.password=123
#虚拟主机
spring.rabbitmq.virtual-host=/

helloworld模型

一、编写消费者

package com.study.hello;

import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
// 生产端没有指定交换机只有routingKey和Object。
//消费方产生hello队列,放在默认的交换机(AMQP default)上。
//而默认的交换机有一个特点,只要你的routerKey的名字与这个
//交换机的队列有相同的名字,他就会自动路由上。 
//生产端routingKey 叫hello ,消费端生产hello队列。
//他们就路由上了
@RabbitListener(queuesToDeclare = @Queue(value = "hello"))  //表示RabbitMQ消费者,声明一个队列
public class HelloConsumer {

    @RabbitHandler //当消费者从队列取出消息时的回调方法
    public void receive(String message){
        System.out.println("message = " + message);
    }
}

二、编写生产者测试类

package com.study;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest(classes = RabbitmqSpringbootApplication.class)
@RunWith(SpringRunner.class)
public class TestRabbitMQ {
    //注入rabbitTemplate
    @Autowired
    private RabbitTemplate rabbitTemplate;
    //hello world
    @Test
    public void testHelloWorld(){
        //转换和发送    1.routingKey 2.消息
        rabbitTemplate.convertAndSend("hello","hello world");
    }
}

三、运行生产者测试类

image

work工作模型

一、消费者

package com.study.work;

import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class WorkConsumer {

    //第一个消费者
    @RabbitListener(queuesToDeclare = @Queue("work")) //@RabbitListener在方法上代表它监听这个方法作为队列消费回调
    public void receive1(String message){
        System.out.println("message1 = " + message);
    }
    //第二个消费者
    @RabbitListener(queuesToDeclare = @Queue("work")) //@RabbitListener在方法上代表它监听这个方法作为队列消费回调
    public void receive2(String message){
        System.out.println("message2 = " + message);
    }
}

二、生产者

@Test
public void testWork(){
    for (int i = 0; i < 10; i++) {
        rabbitTemplate.convertAndSend("work","work模型");
    }
}

三、运行测试

image

fanout广播模型

一、消费者

package com.study.fanout;

import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class FanoutConsumer {
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue, //创建临时队列
                    exchange =@Exchange(value = "logs",type = "fanout") //绑定的交换机
            )
    })
    public void receive1(String message){
        System.out.println("message1 = " + message);
    }

    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue, //创建临时队列
                    exchange =@Exchange(value = "logs",type = "fanout") //绑定的交换机
            )
    })
    public void receive2(String message){
        System.out.println("message2 = " + message);
    }
}

二、生产者

@Test
public void testFanout(){
    rabbitTemplate.convertAndSend("logs","","Fanout模型发送的消息");
}

三、运行测试

image

route路由模型

一、消费者

package com.study.route;

import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class RouteConsumer {
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,//临时队列
                    exchange = @Exchange(value = "directs",type = "direct"),//指定交换机名称和类型
                    key = {"info","error","warn"}
            )
    })
    public void receive1(String message){
        System.out.println("message1 = " + message);
    }

    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,//临时队列
                    exchange = @Exchange(value = "directs",type = "direct"),//指定交换机名称和类型
                    key = {"error"}
            )
    })
    public void receive2(String message){
        System.out.println("message2 = " + message);
    }
}

二、生产者

@Test
public void testRoute(){
    rabbitTemplate.convertAndSend("directs","error","发送info的key的路由信息");
}

三、运行测试

image

Topic动态路由模型

一、消费者

package com.study.topic;

import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class TopicConsumer {
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,//临时队列
                    exchange = @Exchange(type = "topic",name = "topics"),
                    key = {"user.save","user.*"}
            )
    })
    public void receive1(String message){
        System.out.println("message1 = " + message);
    }
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,//临时队列
                    exchange = @Exchange(type = "topic",name = "topics"),
                    key = {"order.#","produce.#","user.*"}
            )
    })
    public void receive2(String message){
        System.out.println("message2 = " + message);
    }
}

二、生产者

@Test
public void testTopic(){
    rabbitTemplate.convertAndSend("topics","user.save","user.save 路由消息");
}

三、运行测试

image

以上是关于RabbitMQ——springboot整合RabbitMQ的主要内容,如果未能解决你的问题,请参考以下文章

RabbitMQ整合SpringBoot

RabbitMQ——springboot整合RabbitMQ

SpringBoot整合多个RabbitMQ

Spring和SpringBoot整合RabbitMQ

Spring和SpringBoot整合RabbitMQ

springboot rabbitmq整合