Springboot整合之--RabbitMQ

Springboot整合系列

SpringBoot整合系列
Springboot整合之–mybatis Springboot整合之–Mybatis
Springboot整合之–RabbitMQ Springboot整合之–RabbitMQ
Springboot整合之–Redis Springboot整合之–Redis

开始整理SpringBoot整合系列的博客。今天开始RabbitMQ整合笔记。RabbitMQ基础知识学习可以参看整理:消息队列之—RabbitMQ

搭建环境:

  • 使用Spring Initializr 快速构建。引入rabbitmq的依赖。
    Springboot整合之--RabbitMQ_第1张图片
  • 配置文件
  # springboot 配置rabbitmq
  rabbitmq:
    host: localhost
    port: 5672
    username:rabbitmqtest1user
    password:rabbitmqtest1user
    virtual-host: /rabbitmqtest1

代码:

  • 生产者代码:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringbootRabbitmqApplication.class)
public class RabbitMQTest {

    @Autowired 
    //springboot整合rabbitmq使用RabbitTemplate操作.
    private RabbitTemplate rabbitTemplate; 
    

    // topics 动态路由模型
    @Test
    public void topicsProvider() {
        rabbitTemplate.convertSendAndReceive("topicsRouting", "user.save.123"," topics 动态路由模型消息 ");

    }
    // routing 路由模型
    @Test
    public void routingProvider() {
        rabbitTemplate.convertSendAndReceive("directRouting", "error"," routing 路由 模型消息 ");

    }

    // fanout 广播模型
    @Test
    public void fanoutProvider() {
        rabbitTemplate.convertSendAndReceive("logsExchange", ""," fanout 模型消息 ");

    }

    // work queue
    @Test
    public void workProvider() {
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertSendAndReceive("workqueue", i + " <--WorkQueue 模型消息");
        }
    }

    //hello world
    @Test
    public void test1() {
        //此时不会创建队列的
        rabbitTemplate.convertAndSend("helloworld", "hello world模式1使用rabbitTemplate发送的消息");
    }
}

RabbitMQ模式1:helloworld 直联模式

  • 消费者代码:
@Component
@RabbitListener(queuesToDeclare = @Queue(value = "helloworld",durable = "true",autoDelete = "false"))
public class HelloConsumer {

    @RabbitHandler
    public void receiveMessage(String message) {
        System.out.println("message--> "+message);
    }
}

RabbitMQ模式2:workqueue 工作队列模式

  • 消费者代码
@Component
public class WorkConsumer {

    //消费者1
    @RabbitListener(queuesToDeclare = @Queue("workqueue"))
    public void workConsumer(String message) {
        System.out.println("消费者1--> " + message);
    }

    //消费者2
    @RabbitListener(queuesToDeclare = @Queue("workqueue"))
    public void workConsumer2(String message) {
        System.out.println("消费者2--> " + message);
    }
}

RabbitMQ模式3: fanout 扇出、广播模式

  • 消费者代码
@Component
public class FanoutConsumer {

    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue,//创建临时队列
            exchange = @Exchange(value = "logsExchange", type = "fanout")
    )
    })
    public void receiverMessage(String message) {
        System.out.println("message1 ->" + message);
    }

    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue,//创建临时队列
            exchange = @Exchange(value = "logsExchange", type = "fanout")
    )
    })
    public void receiverMessage2(String message) {
        System.out.println("message2 ->" + message);
    }
}

RabbitMQ模式4: routing 路由模式

  • 消费者代码:
@Component
public class RoutDirectConsumer {

    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,//临时队列
                    exchange = @Exchange(value = "directRouting",type = "direct"),//定义交换机的名称和类型
                    key = {"info","error","debug"}//指定监听的路由key

            )
    })
    public void routingReceiver(String message){
        System.out.println("消费者1路由模型--> " +message);

    }
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,//临时队列
                    exchange = @Exchange(value = "directRouting",type = "direct"),//定义交换机的名称和类型
                    key = {"error"}//指定监听的路由key

            )
    })
    public void routingReceiver2(String message){
        System.out.println("消费者2路由模型--> " +message);

    }
}

整合模式5: topics 动态路由模式

  • 消费者代码:
@Component
public class TopicConsumer {

    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,
                    exchange = @Exchange(name = "topicsRouting",type = "topic"),
                    key = {"user.*","user.#"}
            )
    })
    public void getMessage(String message) {
        System.out.println("基于动态路由的消费者1--> " + message);
    }
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,
                    exchange = @Exchange(name = "topicsRouting",type = "topic"),
                    key = {"user.*"}
            )
    })
    public void getMessage2(String message) {
        System.out.println("基于动态路由的消费者2--> " + message);
    }
}

MQ应用场景

  • 异步处理
  • 应用解耦
  • 流量削峰

你可能感兴趣的:(SpringBoot整合系列,消息队列,springboot,rabbitmq)