使用SpringBoot对RabbitMQ进行整合,模拟生产者服务器(9003)向消费者服务器(8004)发送消息的过程,消息生产者通过接受Http请求向消息队列发送消息(Controller层、Service层),接收端则直接监听队列接收消息。这里的Demo分别设置两种不同的绑定类型,观察消费者端接收消息的情况。
关于RabbitMQ的搭建及搭建中常见的问题参考连接:RabbitMQ搭建及问题
头交换机(headers exchange)使用多个消息属性来代替路由键建立路由规则。通过判断消息头的值能否与指定的绑定相匹配来确立路由规则。头交换机可以视为直连交换机的另一种表现形式。头交换机能够像直连交换机一样工作,不同之处在于头交换机的路由规则是建立在头属性值之上,而不是路由键。路由键必须是一个字符串,而头属性值则没有这个约束,它们甚至可以是整数或者哈希值(字典)等
在pom.xml文件中添加依赖,主要是springboot中web和amqp的starter
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.0.RELEASEversion>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-amqpartifactId>
dependency>
dependencies>
server:
port: 9003
spring:
application:
name: header-sender
rabbitmq:
username: admin
password: admin
host: 192.168.108.128
port: 5672
server:
port: 8004
spring:
application:
name: header-receiver-1
rabbitmq:
username: admin
password: admin
host: 192.168.108.128
port: 5672
配置信息中创建两个队列分别为header.queue.1和header.queue.2,在绑定的时候调用方法whereAll和whereAny,whereAll用于匹配所有头信息,whereAny只需要匹配到一条即可。
@Component
public class SendConfig {
public static final String HEADER_QUEUE_1 = "header.queue.1";
public static final String HEADER_QUEUE_2 = "header.queue.2";
public static final String HEADER_EXCHANGE = "header.exchange";
@Bean
public Queue queue1() {
return new Queue(HEADER_QUEUE_1);
}
@Bean
public Queue queue2() {
return new Queue(HEADER_QUEUE_2);
}
@Bean
public HeadersExchange headersExchange() {
return new HeadersExchange(HEADER_EXCHANGE);
}
@Bean
public Binding binding1() {
HashMap<String, Object> header = new HashMap<>();
header.put("queue", "queue1");
header.put("bindType", "whereAll");
return BindingBuilder.bind(queue1()).to(headersExchange()).whereAll(header).match();
}
@Bean
public Binding binding2() {
HashMap<String, Object> header = new HashMap<>();
header.put("queue", "queue2");
header.put("bindType", "whereAny");
return BindingBuilder.bind(queue2()).to(headersExchange()).whereAny(header).match();
}
}
@RestController
@RequestMapping("/header")
public class SendController {
@Autowired
private HeaderService headerService;
@GetMapping("/send1/{msg}")
public void send1(@PathVariable String msg) {
headerService.send1(msg);
}
@GetMapping("/send2/{msg}")
public void send2(@PathVariable String msg) {
headerService.send2(msg);
}
}
与前面不同的是,这里通过使用MessageProperties对象封装头信息,通过Message对象传递消息
@Service
public class HeaderServiceImpl implements HeaderService {
@Autowired
private RabbitTemplate rabbitTemplate;
@Override
public void send1(String msg) {
MessageProperties messageProperties = new MessageProperties();
messageProperties.setHeader("queue", "queue1");
messageProperties.setHeader("bindType", "whereAll");
Message message = new Message(msg.getBytes(), messageProperties);
System.out.println("发送消息:"+msg);
rabbitTemplate.convertAndSend(SendConfig.HEADER_EXCHANGE, null, message);
}
@Override
public void send2(String msg) {
MessageProperties messageProperties = new MessageProperties();
messageProperties.setHeader("queue", "queue2");
messageProperties.setHeader("bindType", "whereAny");
Message message = new Message(msg.getBytes(), messageProperties);
System.out.println("发送消息:"+msg);
rabbitTemplate.convertAndSend(SendConfig.HEADER_EXCHANGE, null, message);
}
}
@Component
public class receive {
@RabbitListener(queues = "header.queue.1")
public void receive1(String msg) {
System.out.println("接收到 header.queue.1 发送的消息:" + msg);
}
@RabbitListener(queues = "header.queue.2")
public void receive2(String msg) {
System.out.println("接收到 header.queue.2 发送的消息:" + msg);
}
}
RabbitDemo代码
初识RabbitMQ——AMQP 0-9-1
RabbitMQ——RabbitMQ搭建及问题
SpringBoot整合RabbitMQ——Direct交换机
SpringBoot整合RabbitMQ——Fanout交换机
SpringBoot整合RabbitMQ——Topic交换机