RabbitMQ 入门
* 基于 spring boot 请阅读下方参考文档
启动一个 rabbitmq
version: "3.2" services: some-rabbit: image: rabbitmq:3-management hostname: "my-host" environment: RABBITMQ_ERLANG_COOKIE: "SWQOKODSQALRPCLNMEQG" RABBITMQ_DEFAULT_USER: "rabbitmq" RABBITMQ_DEFAULT_PASS: "rabbitmq" RABBITMQ_DEFAULT_VHOST: "/" ports: - "15672:15672" - "5672:5672"
启动一个 spring boot 项目,配置
spring: rabbitmq: username: rabbitmq password: rabbitmq
Rabbitmq 中的 exchange 的三种主要类型,请参考 https://zhuanlan.zhihu.com/p/37198933
下面就来了解一下 Exchange 的三种主要类型:Direct、Fanout
和 Topic
。
Direct 策略(点对点)
Broker (同一个 exhange 与 bindingKey 同时绑定多个 queue)
@Bean public Declarables directBindings() { Queue directQueue1 = new Queue("directQueue1", false); Queue directQueue2 = new Queue("directQueue2", false); DirectExchange directExchange = new DirectExchange("direct.exchange"); return new Declarables(directQueue1,directQueue2, directExchange, BindingBuilder.bind(directQueue1).to(directExchange).with("info"), BindingBuilder.bind(directQueue1).to(directExchange).with("error"), BindingBuilder.bind(directQueue2).to(directExchange).with("error"));
producer
rabbitTemplate.convertAndSend("direct.exchange", "info", "Hello info!"); rabbitTemplate.convertAndSend("direct.exchange", "error", "Hello error!");
consumer
@RabbitListener(queues = "directQueue1") public void directQueue1(String in) { System.out.println("Message read from directQueue1 : " + in); } @RabbitListener(queues = "directQueue2") public void directQueue2(String in) { System.out.println("Message read from directQueue2 : " + in); }
Default Exchange 是一种特殊的 Direct Exchange,它的名称为空,故只关心队列名称即可)
queue
@Bean public Queue myQueue() { return new Queue("myQueue", false); }
producer
@Test void producer1() { rabbitTemplate.convertAndSend("myQueue", "Hello, world!"); }
cosumer
@RabbitListener(queues = "myQueue") public void listen(String in) { System.out.println("Message read from myQueue : " + in); }
Fanout 策略(发布订阅)
Broker
@Bean public Declarables fanoutBindings() { Queue fanoutQueue1 = new Queue("fanout.queue1", false); Queue fanoutQueue2 = new Queue("fanout.queue2", false); FanoutExchange fanoutExchange = new FanoutExchange("fanout.exchange"); return new Declarables( fanoutQueue1, fanoutQueue2, fanoutExchange, BindingBuilder.bind(fanoutQueue1).to(fanoutExchange), BindingBuilder.bind(fanoutQueue2).to(fanoutExchange)); }
producer
@Test void producer2() { String message = " payload is broadcast"; rabbitTemplate.convertAndSend("fanout.exchange", "", "fanout" + message); }
consumer(两个 )
@RabbitListener(queues = {"fanout.queue1"}) public void receiveMessageFromFanout1(String message) { System.out.println("Received fanout 1 message: " + message); } @RabbitListener(queues = {"fanout.queue2"}) public void receiveMessageFromFanout2(String message) { System.out.println("Received fanout 2 message: " + message); }
Topic 策略(发布订阅)
Broker
@Bean public Declarables topicBindings() { Queue topicQueue1 = new Queue("topicQueue1Name", false); Queue topicQueue2 = new Queue("topicQueue2Name", false); TopicExchange topicExchange = new TopicExchange("topicExchangeName"); return new Declarables( topicQueue1, topicQueue2, topicExchange, BindingBuilder .bind(topicQueue1) .to(topicExchange).with("*.important.*"), BindingBuilder .bind(topicQueue2) .to(topicExchange).with("#.error")); }
生产者
@Test void producer2() { String message = " payload is broadcast"; rabbitTemplate.convertAndSend("topicExchangeName", "hello.important.warn", "topic important warn" + message); rabbitTemplate.convertAndSend("topicExchangeName", "my.important.error", "topic important error" + message); }
消费者
@RabbitListener(queues = {"topicQueue1Name"}) public void receiveMessageFromTopic1(String message) { System.out.println("Received topic 1 (" + "BINDING_PATTERN_IMPORTANT" + ") message: " + message); } @RabbitListener(queues = {"topicQueue2Name"}) public void receiveMessageFromTopic2(String message) { System.out.println("Received topic 2 (" + "BINDING_PATTERN_ERROR" + ") message: " + message); }
233
参考文档
入门指南 https://spring.io/guides/gs/messaging-rabbitmq/
Spring boot 配置 https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-amqp
详细参考文档 https://docs.spring.io/spring-amqp/reference/html/#_spring_integration_reference
另一个参考文档 https://docs.spring.io/spring-amqp/docs/2.2.7.RELEASE/reference/html/
exchange 介绍 https://zhuanlan.zhihu.com/p/37198933