First of all: code version is based on Spring2.1.12 + Rabbit achieve integration
rabbit:pom
<!-- rabbitmq -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
rabbit: YML 配置
server:
port: 8600
spring:
application:
name: rabbitmq-provider
rabbitmq:
host: 192.168.189.131
port: 5673
username: admin
password: admin
#确认消息已发送到交换机(Exchange)
publisher-confirms: true
#确认消息已发送到队列(Queue)
publisher-returns: true
1: Direct Exchange Direct Switch
Definition: Direct Connect type of switch with a queue bindings, you need to specify an explicit binding key.
Routing rules: discovery messages directly connected to the type of switch, and only when routing key binding key exact match, bound queue to receive messages.
Dump failed to re-upload canceled
DirectRabbitConfig
@Configuration
public class DirectRabbitConfig {
//队列 起名:TestDirectQueue
@Bean
public Queue TestDirectQueue() {
return new Queue("TestDirectQueue",true);
}
//Direct交换机 起名:TestDirectExchange
@Bean
DirectExchange TestDirectExchange() {
return new DirectExchange("TestDirectExchange");
}
//绑定 将队列和交换机绑定, 并设置用于匹配键:TestDirectRouting
@Bean
Binding bindingDirect() {
return BindingBuilder.bind(TestDirectQueue()).to(TestDirectExchange()).with("TestDirectRouting");
}
}
Dump failed to re-upload canceled
DirectRabbitController
@RestController
public class DirectRabbitController {
@Autowired
private RabbitTemplate rabbitTemplate;
@GetMapping("/sendDirectMessage/{message}")
public void sendDirectMessage(@PathVariable("message") String message){
String messageId = String.valueOf(UUID.randomUUID());
String messageData = message;
String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
Map<String,Object> map=new HashMap<>();
map.put("messageId",messageId);
map.put("messageData",messageData);
map.put("createTime",createTime);
rabbitTemplate.convertAndSend("TestDirectExchange", "TestDirectRouting", map);
}
@RabbitListener(queues = "TestDirectQueue")
public void consumerDirectMessage(Map map){
System.out.println("DirectReceiver消费者收到消息 : " + map.toString());
}
}
2, Topic Exchange theme switch
Definition: subject type switch with a queue binding routing key can be specified by the pattern matching.
There are two wildcards, * represents a matching word. On behalf of # Match zero or more words. Separated by. Between words.
Routing rule: When sending a message to the topic type of switches, routing key accord binding key patterns, bound queue to receive messages.
Dump failed to re-upload canceled
TopicRabbitConfig
@Configuration
public class TopicRabbitConfig {
public final static String man = "topic.man";
public final static String woman = "topic.woman";
@Bean
public Queue firstQueue() {
return new Queue(TopicRabbitConfig.man);
}
@Bean
public Queue secondQueue() {
return new Queue(TopicRabbitConfig.woman);
}
@Bean
public TopicExchange exchange() {
return new TopicExchange("topicExchange");
}
//将firstQueue和topicExchange绑定,而且绑定的键值为topic.man
//这样只要是消息携带的路由键是topic.man,才会分发到该队列
@Bean
Binding bindingExchangeMessage() {
return BindingBuilder.bind(firstQueue()).to(exchange()).with(man);
}
//将secondQueue和topicExchange绑定,而且绑定的键值为用上通配路由键规则topic.#
// 这样只要是消息携带的路由键是以topic.开头,都会分发到该队列
@Bean
Binding bindingExchangeMessage2() {
return BindingBuilder.bind(secondQueue()).to(exchange()).with("topic.#");
}
}
TopicRabbitController
@RestController
public class TopicRabbitController {
@Autowired
private RabbitTemplate rabbitTemplate;
@GetMapping("/sendTopicFirst")
public String sendTopicMessage1() {
String messageId = String.valueOf(UUID.randomUUID());
String messageData = "message: M A N ";
String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
Map<String, Object> manMap = new HashMap<>();
manMap.put("messageId", messageId);
manMap.put("messageData", messageData);
manMap.put("createTime", createTime);
rabbitTemplate.convertAndSend("topicExchange", "topic.man", manMap);
return "ok";
}
@GetMapping("/sendTopicSecond")
public String sendTopicSecond() {
String messageId = String.valueOf(UUID.randomUUID());
String messageData = "message: woman is all ";
String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
Map<String, Object> womanMap = new HashMap<>();
womanMap.put("messageId", messageId);
womanMap.put("messageData", messageData);
womanMap.put("createTime", createTime);
rabbitTemplate.convertAndSend("topicExchange", "topic.woman", womanMap);
return "ok";
}
@RabbitListener(queues = "topic.man")
public void topicManReceiver(Map map){
System.out.println("topicManReceiver消费者收到消息 : " + map.toString());
}
@RabbitListener(queues = "topic.woman")
public void topicWomenReceiver(Map map){
System.out.println("topicWomenReceiver消费者收到消息 : " + map.toString());
}
}
3, Fanout Exchange broadcasting switch
Definition: The broadcast type switch and a queue bound, do not need to specify the binding key.
Routing Rule: When a broadcast message is sent to the types of switches, do not need to specify the routing key, all of it is bound can receive message queue.
Dump failed to re-upload canceled
FanoutRabbitConfig
@Configuration
public class FanoutRabbitConfig {
/**
* 创建三个队列 :fanout.A fanout.B fanout.C
* 将三个队列都绑定在交换机 fanoutExchange 上
* 因为是扇型交换机, 路由键无需配置,配置也不起作用
*/
@Bean
public Queue queueA() {
return new Queue("fanout.A");
}
@Bean
public Queue queueB() {
return new Queue("fanout.B");
}
@Bean
public Queue queueC() {
return new Queue("fanout.C");
}
@Bean
FanoutExchange fanoutExchange() {
return new FanoutExchange("fanoutExchange");
}
@Bean
Binding bindingExchangeA() {
return BindingBuilder.bind(queueA()).to(fanoutExchange());
}
@Bean
Binding bindingExchangeB() {
return BindingBuilder.bind(queueB()).to(fanoutExchange());
}
@Bean
Binding bindingExchangeC() {
return BindingBuilder.bind(queueC()).to(fanoutExchange());
}
}
FanoutRabbitController
@RestController
public class FanoutRabbitController {
@Autowired
private RabbitTemplate rabbitTemplate;
@GetMapping("/sendFanoutMessage")
public String sendFanoutMessage() {
String messageId = String.valueOf(UUID.randomUUID());
String messageData = "message: testFanoutMessage ";
String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
Map<String, Object> map = new HashMap<>();
map.put("messageId", messageId);
map.put("messageData", messageData);
map.put("createTime", createTime);
rabbitTemplate.convertAndSend("fanoutExchange", null, map);
return "ok";
}
@RabbitListener(queues = "fanout.A")
public void FanoutReceiverA(Map testMessage){
System.out.println("FanoutReceiverA消费者收到消息 : " +testMessage.toString());
}
@RabbitListener(queues = "fanout.B")
public void FanoutReceiverB(Map testMessage){
System.out.println("FanoutReceiverB消费者收到消息 : " +testMessage.toString());
}
@RabbitListener(queues = "fanout.C")
public void FanoutReceiverC(Map testMessage){
System.out.println("FanoutReceiverC消费者收到消息 : " +testMessage.toString());
}
}