maven依赖:
4.0.0 org.springframework.boot spring-boot-starter-parent2.5.6 com.tech tech-rabbitmq1.0 tech-rabbitmq Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-weborg.projectlombok lomboktrue org.springframework.boot spring-boot-starter-testtest org.springframework.boot spring-boot-starter-amqporg.springframework.boot spring-boot-starter-testtest org.springframework.boot spring-boot-maven-pluginorg.projectlombok lombok
配置:
server:
port: 8080
spring:
rabbitmq:
host: 192.168.50.134
port: 5672
virtual-host: /dev
username: tech
password: tech
配置声明交换机和队列:
package com.tech.rabbitmq.spring;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitMQConfig {
public static final String EXCHANGE_NAME="exchange_order";
public static final String QUEUE="order_queue";
@Bean
public Exchange orderExchange(){
return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
}
@Bean
public Queue orderQueue(){
return QueueBuilder.durable(QUEUE).build();
}
@Bean
public Binding orderBinding(Queue queue,Exchange exchange){
return BindingBuilder.bind(queue).to(exchange).with("order.#").noargs();
}
}
消息发送者:
package com.tech.rabbitmq.spring;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MsgSendController {
@Autowired
private RabbitTemplate rabbitTemplate;
@GetMapping("send")
String send(){
for (int i = 0; i < 5; i++) {
rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME,"order.new","新订单");
}
return "ok";
}
}
消息监听者:
package com.tech.rabbitmq.spring;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "order_queue")
public class OrderMQListener {
@RabbitHandler
public void messageHandler(String body, Message message){
long deliveryTag = message.getMessageProperties().getDeliveryTag();
System.out.println("msgTag="+deliveryTag);
System.out.println("message="+message.toString());
System.out.println("body="+body);
System.out.println("*****************************");
}
}
启动后访问 /send接口发送消息成功后,可以看到控制台输出日志:
sgTag=1
message=(Body:'新订单' MessageProperties [headers={}, contentType=text/plain, contentEncoding=UTF-8, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=false, receivedExchange=exchange_order, receivedRoutingKey=order.new, deliveryTag=1, consumerTag=amq.ctag-f12rU2Z7lwPNGp7fa_tRjw, consumerQueue=order_queue])
body=新订单
*****************************
msgTag=2
message=(Body:'新订单' MessageProperties [headers={}, contentType=text/plain, contentEncoding=UTF-8, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=false, receivedExchange=exchange_order, receivedRoutingKey=order.new, deliveryTag=2, consumerTag=amq.ctag-f12rU2Z7lwPNGp7fa_tRjw, consumerQueue=order_queue])
body=新订单
*****************************
msgTag=3
message=(Body:'新订单' MessageProperties [headers={}, contentType=text/plain, contentEncoding=UTF-8, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=false, receivedExchange=exchange_order, receivedRoutingKey=order.new, deliveryTag=3, consumerTag=amq.ctag-f12rU2Z7lwPNGp7fa_tRjw, consumerQueue=order_queue])
body=新订单
*****************************
msgTag=4
message=(Body:'新订单' MessageProperties [headers={}, contentType=text/plain, contentEncoding=UTF-8, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=false, receivedExchange=exchange_order, receivedRoutingKey=order.new, deliveryTag=4, consumerTag=amq.ctag-f12rU2Z7lwPNGp7fa_tRjw, consumerQueue=order_queue])
body=新订单
*****************************
msgTag=5
message=(Body:'新订单' MessageProperties [headers={}, contentType=text/plain, contentEncoding=UTF-8, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=false, receivedExchange=exchange_order, receivedRoutingKey=order.new, deliveryTag=5, consumerTag=amq.ctag-f12rU2Z7lwPNGp7fa_tRjw, consumerQueue=order_queue])
body=新订单
*****************************



