docker run -d
–hostname my-rabbit
–name some-rabbit
-e RABBITMQ_DEFAULT_USER=user
-e RABBITMQ_DEFAULT_PASS=password
-p 15672:15672 -p 5672:5672
rabbitmq:3-management
– hostname:在配置集群时使用,区分节点。(这里是单机,随便起个名字)
-p 15672 rabbitmq 提供的可视化平台的端口号,便于管理
-p 5672 rabbitmq 服务器的端口,用于消息的发布和订阅
RABBITMQ_DEFAULT_USER、RABBITMQ_DEFAULT_PASS:用来配置登录可视化管理平台的账号,密码
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency>
@Beanpublic MessageConverter messageConverter(){return new Jackson2JsonMessageConverter();}
server:port: 8080
spring:rabbitmq:host: 192.168.193.105 port: 5672username: wuyupassword: 123456virtual-host: "/wuyu" # 自己创建的虚拟主机
package sumer.listener;import org.ExchangeTypes;
import org.Message;
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 MessageQueueListener {@RabbitListener(bindings = {@QueueBinding(value = @Queue("fanout.queue1"), exchange = @Exchange(name = "wuyu.fanout", type = ExchangeTypes.FANOUT))})public void fanoutQueue1(Message msg) {byte[] body = Body();String res = new String(body);System.out.println("fanout.queue1:" + res);}@RabbitListener(bindings = {@QueueBinding(value = @Queue("fanout.queue2"), exchange = @Exchange(name = "wuyu.fanout", type = ExchangeTypes.FANOUT))})public void fanoutQueue2(Message msg) {byte[] body = Body();String res = new String(body);System.out.println("fanout.queue2:" + res);}@RabbitListener(bindings = {@QueueBinding(value = @Queue("direct.queue1"),exchange = @Exchange(name = "wuyu.direct", type = ExchangeTypes.DIRECT),key = {"red"})})public void directQueue1(Message msg) {byte[] body = Body();String res = new String(body);System.out.println("direct.queue1:" + res);}@RabbitListener(bindings = {@QueueBinding(value = @Queue("direct.queue2"),exchange = @Exchange(name = "wuyu.direct", type = ExchangeTypes.DIRECT),key = {"yellow"})})public void directQueue2(Message msg) {byte[] body = Body();String res = new String(body);System.out.println("direct.queue2:" + res);}@RabbitListener(bindings = {@QueueBinding(value = @Queue("topic.queue1"),exchange = @Exchange(name = pic", type = ExchangeTypes.TOPIC),key = {"china.#"})})public void topicQueue1(Message msg) {byte[] body = Body();String res = new String(body);System.out.println("topic.queue1:" + res);}@RabbitListener(bindings = {@QueueBinding(value = @Queue("topic.queue2"),exchange = @Exchange(name = pic", type = ExchangeTypes.TOPIC),key = {"#.news"})})public void topicQueue2(Message msg) {byte[] body = Body();String res = new String(body);System.out.println("topic.queue2:" + res);}
}
@SpringBootTest
public class PublisherAppTest {@Resourceprivate RabbitTemplate rabbitTemplate;/*** fanout模式,不需要 routingKey。* 它会把消息转发到每一个和 fanout交换机绑定的队列*/@Testpublic void testSendFanoutExchange(){String exchange="wuyu.fanout";Map<String,String> map=new HashMap<>();map.put("name","小舞");vertAndSend(exchange,"",map);}/*** direct模式,需要 routingKey* 它会把消息转发到 指定 routingKey的队列*/@Testpublic void testDirectExchange(){String exchange="wuyu.direct";String routingKey="red";Map<String,String> map=new HashMap<>();map.put("name","小舞");vertAndSend(exchange,routingKey,map);}/*** topic模式,需要 routingKey (在direct的基础上 routingKey可以使用通配符)* ( # 匹配 0或多个单词 )* ( * 匹配一个单词 )* 匹配到 routingKey 的监听队列会接收发布的消息*/@Testpublic void testTopicExchange(){String exchange=pic";String routingKey="china.video";Map<String,String> map=new HashMap<>();map.put("name","小舞");vertAndSend(exchange,routingKey,map);}
}
本文发布于:2024-01-27 23:52:51,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/17063707963399.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |