SpringBoot+RabbitMQ
一、topic模式下product and consumer 手动确认消息(MQ各名词已熟悉)
boot集成RabbitMQ,只讲topic模式,因为它几乎涵盖了其他所有模式。
二、配置文件
tips:测试手动确认和自动确认之前,记得改一下yml配置
spring:
rabbitmq:
host: xxxx
port: 5672
username: xxxx
password: xxxx
#确认消息已发送到交换机(Exchange) 版本较低用 publisher-confirms: true
publisher-confirm-type: correlated
#指定消息确认模式为手动确认
listener:
simple:
acknowledge-mode: manual
#acknowledge-mode: none
#发送返回监听回调
publisher-returns: true
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>2.2.9.RELEASE</version>
</dependency>
三、TopicRabbitConfig、MQ初始化Broker-Exchange-RoutingKey-Queue
tips:绑定通配符topic.A 、topic.# ( .# 代表匹配任意长度字符,.* 代表匹配一个)
package com.sdzn.flxt.commons.configs;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* rabbit innit
*/
@Configuration
public class TopicRabbitConfig {
/*
exchange - name
*/
private final static String exchange_name = "topicExchange";
/*
routingKey - name
*/
private final static String routingKey1 = "topic.A";
private final static String routingKey2 = "topic.#";
/*
Queue - name
*/
private static String QueueName_A = "QueueA";
private static String QueueName_B = "QueueB";
/*
Queue - matchRule
*/
@Bean
public Queue firstQueue() {
return new Queue(TopicRabbitConfig.QueueName_A);
}
@Bean
public Queue secondQueue() {
return new Queue(TopicRabbitConfig.QueueName_B);
}
/*
exchange - set
*/
@Bean
TopicExchange exchange() {
return new TopicExchange(exchange_name);
}
/*
bind - queue->exchange->routingKey
*/
@Bean
Binding bindingExchangeMessage() {
return BindingBuilder.bind(