RabbitMq的5种模式及实例解读
作者:某某灬
这篇文章主要介绍了RabbitMq的5种模式及实例解读,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
先建一个spring boot项目,然后加入RabbitMQ的依赖就和相关配置。
在pom.xml中加入:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency>
在application.yml中加入配置:
spring: #rabbitmq配置 rabbitmq: host: 127.0.0.1 port: 5672 username: guest password: guest publisher-confirms: true # 开启消息确认,这可不用管 virtual-host: /
1、点对点,简单的生产者消费者
先创建一个配置类:
@Configuration public class RabbitConfig { public final static String SIMPLE = "simple"; // 队列名称 @Bean public Queue simpleQueue() { // 返回一个bean return new Queue(SIMPLE, true, false, true); } }
创建消费者和生产者:
@Component public class SimpleRabbit { // 简单的点对点模式 @Autowired RabbitTemplate rabbitTemplate; // 生产者 public void send() throws Exception { for (int i = 0; i < 5; i++) { rabbitTemplate.convertAndSend(RabbitConfig.SIMPLE, "发送:" + i); Thread.sleep(1000); } } // 消费者监听SIMPLE队列 @RabbitListener(queues = RabbitConfig.SIMPLE) public void comsumer(String msg) { System.out.println("comsumer:" + msg); } }
然后在测试类测试一下:
@RunWith(SpringRunner.class) @SpringBootTest public class SimpleTest { @Autowired SimpleRabbit rabbit; @Test public void test() throws Exception { rabbit.send(); } }
结果为:
2、工作模式(work)
工作模式和上边的模式差不多,只是对于当前的队列多了一个消费者:
我们只用在 SimpleRabbit类中添加一个消费者就可以完成测试:
@Component public class SimpleRabbit { // 简单的点对点模式 @Autowired RabbitTemplate rabbitTemplate; // 生产者 public void send() throws Exception { for (int i = 0; i < 10; i++) { rabbitTemplate.convertAndSend(RabbitConfig.SIMPLE, "发送:" + i); Thread.sleep(1000); } } @RabbitListener(queues = RabbitConfig.SIMPLE) public void comsumer1(String o) { System.out.println("consumer1:" + o); } @RabbitListener(queues = RabbitConfig.SIMPLE) public void comsumer2(String o) { System.out.println("consumer2" + o); } }
直接运行test的测试方法:
从打印数据来开,是采用轮询的方式消费的。
3、发布/订阅模式Publish/Subscribe
生产者是把消息发到交换机,然后交换机把数据发送到绑定的队列上。
如果没有队列与之绑定,信息将会丢失,交换机不能存储信息。消费者监听队列,有就消费。
为了方便,这里的两个队列后边两个模式就一起用了。
@Configuration public class RabbitConfig { public final static String PUB_SUB_EXCHANGE = "pub_sub"; // 发布订阅的交换机 public final static String QUEUE_1 = "queue_1"; //队列1 public final static String QUEUE_2 = "queue_2"; //队列2 @Bean("queue_1") public Queue QUEUE_1_Queue() { return new Queue(QUEUE_1, true, false, true); } @Bean("queue_2") public Queue QUEUE_2_Queue() { return new Queue(QUEUE_2, true, false, true); } @Bean("pub_sub") // 交换机 public Exchange pub_sub_Exchange(){ return ExchangeBuilder.fanoutExchange(PUB_SUB_EXCHANGE).build(); } // 使交换机与队列绑定 @Bean public Binding binding_QUEUE_1(@Qualifier("queue_1") Queue queue, @Qualifier(PUB_SUB_EXCHANGE) Exchange exchange) { // with是路由key,这模式默认为空就好 return BindingBuilder.bind(queue).to(exchange).with("").noargs(); } @Bean public Binding binding_QUEUE_2(@Qualifier("queue_2") Queue queue, @Qualifier(PUB_SUB_EXCHANGE) Exchange exchange) { return BindingBuilder.bind(queue).to(exchange).with("").noargs(); } }
然后定义生产者和消费者:
@Component public class FanoutRabbit { @Autowired RabbitTemplate template; public void producer() { for (int i = 0; i < 5; i++) { // ""是路由key,应为没有所以传的空 template.convertAndSend(RabbitConfig.PUB_SUB_EXCHANGE, "", i); } } // 队列1的消费者 @RabbitListener(queues = RabbitConfig.QUEUE_1) public void queues_1(Integer msg) { System.out.println("队列1的消费者:" + msg); } // 队列1的消费者 @RabbitListener(queues = RabbitConfig.QUEUE_2) public void queues_2(Integer msg) { System.out.println("队列2的消费者:" + msg); } }
测试:
@Autowired FanoutRabbit fanoutRabbit; @Test public void test1() { fanoutRabbit.producer(); }
结果:
4、routing路由模式
交换机绑定的路由key和队列绑定的一样时,才发送。
@Configuration public class RabbitConfig { public final static String ROUTING_EXCHANGE = "routing1"; // 路由的交换机 public final static String ROUTING_1 = "routing_1"; //routing队列1 public final static String ROUTING_2 = "routing_2"; //routing队列2 @Bean("routing_1") public Queue OUTING_1_Queue() { return new Queue(ROUTING_1, true, false, true); } @Bean("routing_2") public Queue ROUTING_2_Queue() { return new Queue(ROUTING_2, true, false, true); } @Bean("routing1") public Exchange routingExchange(){ return ExchangeBuilder.directExchange(ROUTING_EXCHANGE).build(); } @Bean public Binding binding_Routing_QUEUE_1(@Qualifier(ROUTING_1) Queue queue, @Qualifier(ROUTING_EXCHANGE) Exchange exchange) { return BindingBuilder.bind(queue).to(exchange).with(ROUTING_1).noargs(); } @Bean public Binding binding_Routing_QUEUE_2(@Qualifier(ROUTING_2) Queue queue, @Qualifier(ROUTING_EXCHANGE) Exchange exchange) { return BindingBuilder.bind(queue).to(exchange).with(ROUTING_2).noargs(); } }
生产者和消费者:
@Component public class RoutingRabbit { @Autowired RabbitTemplate template; public void producer(){ for (int i = 0; i < 5; i++) { template.convertAndSend(RabbitConfig.ROUTING_EXCHANGE, RabbitConfig.ROUTING_1,i); } } // 队列1的消费者 @RabbitListener(queues = RabbitConfig.ROUTING_1) public void queues_1(Integer msg) { System.out.println("队列1的消费者:" + msg); } // 队列1的消费者 @RabbitListener(queues = RabbitConfig.ROUTING_2) public void queues_2(Integer msg) { System.out.println("队列2的消费者:" + msg); } }
测试:
5、topic主题模式
设置队列和交换机的路由key,当生产者发送消息时,知道路由key,比较有不有合适的key。有就发布到队列上去。
public final static String TOPIC_1 = "routing_1"; //topic队列1 public final static String TOPIC_2 = "routing_2"; //topic队列2 @Bean(TOPIC_1) public Queue TOPIC_1_Queue() { return new Queue(TOPIC_1, true, false, true); } @Bean(TOPIC_2) public Queue TOPIC_2_Queue() { return new Queue(TOPIC_2, true, false, true); } @Bean(TOPIC_EXCHANGE) public Exchange topicExchange(){ return ExchangeBuilder.topicExchange(TOPIC_EXCHANGE).build(); } @Bean public Binding binding_Topic_QUEUE_1(@Qualifier(TOPIC_1) Queue queue, @Qualifier(TOPIC_EXCHANGE) Exchange exchange) { return BindingBuilder.bind(queue).to(exchange).with("aa.bb.cc").noargs(); } @Bean public Binding binding_Topic_QUEUE_2(@Qualifier(TOPIC_2) Queue queue, @Qualifier(TOPIC_EXCHANGE) Exchange exchange) { return BindingBuilder.bind(queue).to(exchange).with("aa.#").noargs(); }
生产者和消费者:
@Component public class TopicRabbit { @Autowired RabbitTemplate template; public void producer() { for (int i = 0; i < 5; i++) { template.convertAndSend(RabbitConfig.TOPIC_EXCHANGE, "aa.bb", i); } } // 队列1的消费者 @RabbitListener(queues = RabbitConfig.TOPIC_1) public void queues_1(Integer msg) { System.out.println("队列1的消费者:" + msg); } // 队列1的消费者 @RabbitListener(queues = RabbitConfig.TOPIC_2) public void queues_2(Integer msg) { System.out.println("队列2的消费者:" + msg); } }
测试结果:
队列2的消费者:1
队列2的消费者:0
队列2的消费者:3
队列2的消费者:2
队列2的消费者:4
*号代表单个词语
#代表多个词语
其它的和routing没什么区别。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。