RabbitMQ-SpringBoot

目录

一、环境搭建 

1.1 依赖

1.2 配置文件

二、队列模式 

2.1 简介

2.2生产者

2.3 消费者

三、工作队列

3.1 简介

3.2 生产者

3.3 消费者

3.4 结果

 四、发布订阅

4.1 简介

4.2 生产者

4.3 消费者

4.4 结果

五、路由

5.1 简介

5.2 生产者

5.3 消费者

5.4 结果

六、动态路由

6.1 简介

6.2 生产者

6.3 消费者

6.4 结果

七、MQ的应用场景

7.1 异步处理

7.2 应用解耦

7.3 流量削峰


一、环境搭建 

1.1 依赖

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

1.2 配置文件

spring:
  rabbitmq:
    host: 192.168.248.128
    port: 5672
    username: guest
    password: guest

二、队列模式 

2.1 简介

2.2生产者

@RunWith(SpringRunner.class)
@SpringBootTest
public class RabbitmqSpringbootApplicationTests {
    @Autowired
    private RabbitTemplate template;

    @Test
    public void contextLoads() {
        //队列名: hello   消息: hello word
        template.convertAndSend("hello","hello word");
    }

}

2.3 消费者

@Component   //默认: 持久化 非独占  不自动删除
@RabbitListener(queuesToDeclare = @Queue(value = "hello"))
public class HelloConsumer {
    @RabbitHandler
    public void receive(String msg){
        System.out.println(msg);
    }
}

三、工作队列

3.1 简介

3.2 生产者

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;

@RunWith(SpringRunner.class)
@SpringBootTest
public class RabbitmqSpringbootApplicationTests {
    @Autowired
    private RabbitTemplate template;

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

}

3.3 消费者

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

@Component   //公平消费
public class HelloConsumer {
    @RabbitListener(queuesToDeclare = @Queue("work"))
    public void receive1(String msg){
        System.out.println("receive1: "+msg);
    }

    @RabbitListener(queuesToDeclare = @Queue("work"))
    public void receive2(String msg){
        System.out.println("receive2: "+msg);
    }
}

3.4 结果

 

消息被两个消费者公平的消费完 

 四、发布订阅

4.1 简介

4.2 生产者

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;

@RunWith(SpringRunner.class)
@SpringBootTest
public class RabbitmqSpringbootApplicationTests {
    @Autowired
    private RabbitTemplate template;

    @Test //扇出 广播
    public void contextLoads() {
        for (int i = 0; i < 5; i++) {
            template.convertAndSend("logs","","Fanout模型"+i);
        }
    }

}

4.3 消费者

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 HelloConsumer {

    @RabbitListener(
            bindings = {
                    @QueueBinding(
                            value = @Queue,  //临时队列
                            exchange = @Exchange(value = "logs",type = "fanout")
                    )
            }
    )
    public void receive1(String msg){
        System.out.println("receive1: "+msg);
    }

    @RabbitListener(
            bindings = {
                    @QueueBinding(
                            value = @Queue,  //临时队列
                            exchange = @Exchange(value = "logs",type = "fanout")
                    )
            }
    )
    public void receive2(String msg){
        System.out.println("receive2: "+msg);
    }
}

4.4 结果

消费者1和2消费了同样的内容 

五、路由

5.1 简介

5.2 生产者

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;

@RunWith(SpringRunner.class)
@SpringBootTest
public class RabbitmqSpringbootApplicationTests {
    @Autowired
    private RabbitTemplate template;

    @Test //路由
    public void contextLoads() {
        template.convertAndSend("directs","info","路由info");
        template.convertAndSend("directs","error","路由error");
    }

}

5.3 消费者

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 HelloConsumer {

    @RabbitListener(
            bindings = {@QueueBinding(
                    value = @Queue,//临时队列
                    exchange = @Exchange(value = "directs",type = "direct"),
                    key = {"info","error"}
            )}
    )
    public void receive1(String msg){
        System.out.println("receive1: "+msg);
    }

    @RabbitListener(
            bindings = {@QueueBinding(
                    value = @Queue,//临时队列
                    exchange = @Exchange(value = "directs",type = "direct"),
                    key = {"info"}
            )}
    )
    public void receive2(String msg){
        System.out.println("receive2: "+msg);
    }
}

5.4 结果

 发info时都可以收到,发error时只有消费者1才可以收到

六、动态路由

6.1 简介

 *表示一个单词
#表示一个或者多个单词

6.2 生产者

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;

@RunWith(SpringRunner.class)
@SpringBootTest
public class RabbitmqSpringbootApplicationTests {
    @Autowired
    private RabbitTemplate template;

    @Test //路由
    public void contextLoads() {
        // user. 后面有一个单词
        template.convertAndSend("topic","user.save","路由user.save");
        // user. 后面有多个单词
        template.convertAndSend("topic","user.save.ok","路由user.save.ok");
    }

}

6.3 消费者

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 HelloConsumer {

    @RabbitListener(
            bindings = {@QueueBinding(
                    value = @Queue,//临时队列
                    exchange = @Exchange(value = "topic",type = "topic"),
                    key = {"user.*"}
            )}
    )
    public void receive1(String msg){
        System.out.println("receive1: "+msg);
    }

    @RabbitListener(
            bindings = {@QueueBinding(
                    value = @Queue,//临时队列
                    exchange = @Exchange(value = "topic",type = "topic"),
                    key = {"user.#"}
            )}
    )
    public void receive2(String msg){
        System.out.println("receive2: "+msg);
    }
}

6.4 结果

消费者2可以匹配多个单词,所以都可以消费

七、MQ的应用场景

7.1 异步处理

7.2 应用解耦

 

7.3 流量削峰

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值