1、引入Maven依赖
<dependencies>
<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>
<scope>test</scope>
</dependency>
<!-- springboot rabbitmq(amqp) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>
2、Application.prperties配置
生产者客户端整合Springboot代码:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RabbitMQProducerApplication {
public static void main(String[] args) {
SpringApplication.run(RabbitMQProducerApplication.class, args);
}
}
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate.ConfirmCallback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;
import java.util.Map;
import java.util.UUID;
@Component
public class RabbitSender {
@Autowired
private RabbitTemplate rabbitTemplate;
/**
* 这里就是确认消息的回调监听接口,用于确认消息是否被broker所收到
*/
final ConfirmCallback confirmCallback = new ConfirmCallback() {
/**
* @param correlationData 作为一个唯一的标识
* @param ack broker 是否落盘成功
* @param cause 失败的一些异常信息
*/
@Override
public void confirm(CorrelationData correlationData, boolean ack, String cause) {
System.err.println("消息ACK结果:" + ack + ", correlationData: " + correlationData.getId());
}
};
/**
* 对外发送消息的方法
* @param message 具体的消息内容
* @param properties 额外的附加属性
* @throws Exception
*/
public void send(Object message, Map<String, Object> properties) throws Exception {
MessageHeaders mhs = new MessageHeaders(properties);
Message<?> msg = MessageBuilder.createMessage(message, mhs);
rabbitTemplate.setConfirmCallback(confirmCallback);
// 指定业务唯一的iD
CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
MessagePostProcessor mpp = message1 -> {
System.err.println("---> post to do: " + message1);
return message1;
};
rabbitTemplate.convertAndSend("exchange-1",
"springboot.rabbit",
msg, mpp, correlationData);
}
}
application.properties配置文件:
server.servlet.context-path=/
server.port=8001
spring.rabbitmq.addresses=192.168.110.130:5672
#spring.rabbitmq.addresses=192.168.11.71:5672,192.168.11.72:5672,192.168.11.71:5673
spring.rabbitmq.username=root
spring.rabbitmq.password=1TdhblkFcdhx2a
spring.rabbitmq.virtual-host=/
spring.rabbitmq.connection-timeout=15000
## 使用启用消息确认模式
# spring.rabbitmq.publisher-confirms=true
spring.rabbitmq.publisher-confirm-type=correlated
## 设置return消息模式,注意要和mandatory一起去配合使用
##spring.rabbitmq.publisher-returns=true
##spring.rabbitmq.template.mandatory=true
spring.application.name=rabbit-producer
server.servlet.encoding.charset=UTF-8
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
spring.jackson.default-property-inclusion=NON_NULL
生产者客户端测试类代码:
import com.lvxiaosha.rabbit.producer.component.RabbitSender;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.HashMap;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest
public class RabbitMQConsumerApplicationTests {
@Autowired
private RabbitSender reRabbitSender;
@Test
public void testSender() throws Exception {
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("attr1", "12345");
properties.put("attr2", "abcde");
reRabbitSender.send("hello rabbitmq!", properties);
Thread.sleep(10000);
}
}
消费者客户端整合Springboot代码:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RabbitMQConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(RabbitMQConsumerApplication.class, args);
}
}
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;
@Component
public class RabbitReceive {
/**
* 组合使用监听
* @RabbitListener @QueueBinding @Queue @Exchange
* @param message
* @param channel
* @throws Exception
*/
@RabbitListener(bindings = @QueueBinding(
value = @Queue(value = "queue-1", durable = "true"),
exchange = @Exchange(name = "${spring.rabbitmq.listener.template.exchange.name}",
durable = "${spring.rabbitmq.listener.template.exchange.durable}",
type = "${spring.rabbitmq.listener.template.exchange.type}",
ignoreDeclarationExceptions = "true"),
key = "${spring.rabbitmq.listener.template.exchange.key}"
)
)
@RabbitHandler
public void onMessage(Message message, Channel channel) throws Exception {
// 1. 收到消息以后进行业务端消费处理
System.err.println("-----------------------");
System.err.println("消费消息:" + message.getPayload());
// 2. 处理成功之后 获取deliveryTag 并进行手工的ACK操作。
// 因为我们配置文件里配置的是 手工签收 spring.rabbitmq.listener.simple.acknowledge-mode=manual
Long deliveryTag = (Long)message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);
channel.basicAck(deliveryTag, false);
}
}
application.properties配置文件:
server.servlet.context-path=/
server.port=8002
spring.rabbitmq.addresses=192.168.110.130:5672
#spring.rabbitmq.addresses=192.168.11.71:5672,192.168.11.72:5672,192.168.11.71:5673
spring.rabbitmq.username=root
spring.rabbitmq.password=1TdhblkFcdhx2a
spring.rabbitmq.virtual-host=/
spring.rabbitmq.connection-timeout=15000
## 表示消费者消费成功消息以后需要手工的进行签收(ack),默认为auto
spring.rabbitmq.listener.simple.acknowledge-mode=manual
spring.rabbitmq.listener.simple.concurrency=5
spring.rabbitmq.listener.simple.max-concurrency=10
spring.rabbitmq.listener.simple.prefetch=1
## 最好不要在代码里写死配置信息,尽量使用这种方式也就是配置文件的方式。
## 在代码里使用${}方式进行配置:${spring.rabbitmq.listener.template.exchange.name}
spring.rabbitmq.listener.template.exchange.name=exchange-1
spring.rabbitmq.listener.template.exchange.durable=true
spring.rabbitmq.listener.template.exchange.type=topic
spring.rabbitmq.listener.template.exchange.key=springboot.*
spring.application.name=rabbit-producer
server.servlet.encoding.charset=UTF-8
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
spring.jackson.default-property-inclusion=NON_NULL