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>
@Bean
public MessageConverter messageConverter(){
return new Jackson2JsonMessageConverter();
}
server:
port: 8080
spring:
rabbitmq:
host: 192.168.193.105
port: 5672
username: wuyu
password: 123456
virtual-host: "/wuyu" # 自己创建的虚拟主机
package com.shi.consumer.listener;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.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 = msg.getBody();
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 = msg.getBody();
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 = msg.getBody();
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 = msg.getBody();
String res = new String(body);
System.out.println("direct.queue2:" + res);
}
@RabbitListener(bindings = {
@QueueBinding(value = @Queue("topic.queue1"),
exchange = @Exchange(name = "wuyu.topic", type = ExchangeTypes.TOPIC),
key = {"china.#"}
)
})
public void topicQueue1(Message msg) {
byte[] body = msg.getBody();
String res = new String(body);
System.out.println("topic.queue1:" + res);
}
@RabbitListener(bindings = {
@QueueBinding(value = @Queue("topic.queue2"),
exchange = @Exchange(name = "wuyu.topic", type = ExchangeTypes.TOPIC),
key = {"#.news"}
)
})
public void topicQueue2(Message msg) {
byte[] body = msg.getBody();
String res = new String(body);
System.out.println("topic.queue2:" + res);
}
}
@SpringBootTest
public class PublisherAppTest {
@Resource
private RabbitTemplate rabbitTemplate;
/**
* fanout模式,不需要 routingKey。
* 它会把消息转发到每一个和 fanout交换机绑定的队列
*/
@Test
public void testSendFanoutExchange(){
String exchange="wuyu.fanout";
Map<String,String> map=new HashMap<>();
map.put("name","小舞");
rabbitTemplate.convertAndSend(exchange,"",map);
}
/**
* direct模式,需要 routingKey
* 它会把消息转发到 指定 routingKey的队列
*/
@Test
public void testDirectExchange(){
String exchange="wuyu.direct";
String routingKey="red";
Map<String,String> map=new HashMap<>();
map.put("name","小舞");
rabbitTemplate.convertAndSend(exchange,routingKey,map);
}
/**
* topic模式,需要 routingKey (在direct的基础上 routingKey可以使用通配符)
* ( # 匹配 0或多个单词 )
* ( * 匹配一个单词 )
* 匹配到 routingKey 的监听队列会接收发布的消息
*/
@Test
public void testTopicExchange(){
String exchange="wuyu.topic";
String routingKey="china.video";
Map<String,String> map=new HashMap<>();
map.put("name","小舞");
rabbitTemplate.convertAndSend(exchange,routingKey,map);
}
}