第一步:在项目pom.xml文件中,添加pring-boot-starter-amqp 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
<version>1.5.9.RELEASE</version>
</dependency>
第二步:在项目application.properties文件中,添加rabbitmq相关配置
##########RabbitMq配置##########
#是访问port不是15672,15672是api和管理界面的port
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=test
spring.rabbitmq.password=test
#如果要进行消息回调,则这里必须要设置为true
spring.rabbitmq.publisherconfirms=true
spring.rabbitmq.virtual-host=/
#最小消息监听线程数
spring.rabbitmq.listener.concurrency=2
#最大消息监听线程数
spring.rabbitmq.listener.max-concurrency=2
第三步:添加rabbitmq配置类和生产者类(生产消息)、消费者类(消费消息)
rabbitmq配置类
package com.fhsn.weixin.config.rabbitmq;
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;
@Configuration
public class RabbitMqConfig {
//声明队列
@Bean
public Queue queue1() {
return new Queue("hello.queue1", true); // true表示持久化该队列
}
@Bean
public Queue queue2() {
return new Queue("hello.queue2", true);
}
//声明交互器
@Bean
TopicExchange topicExchange() {
return new TopicExchange("topicExchange");
}
//绑定
@Bean
public Binding binding1() {
return BindingBuilder.bind(queue1()).to(topicExchange()).with("key.1");
}
@Bean
public Binding binding2() {
return BindingBuilder.bind(queue2()).to(topicExchange()).with("key.#");
}
}
生产者类(生产消息)
package com.fhsn.weixin.config.rabbitmq;
import java.util.UUID;
import javax.annotation.PostConstruct;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate.ReturnCallback;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Sender implements RabbitTemplate.ConfirmCallback, ReturnCallback {
@Autowired
private RabbitTemplate rabbitTemplate;
@PostConstruct
public void init() {
rabbitTemplate.setConfirmCallback(this);
rabbitTemplate.setReturnCallback(this);
}
@Override
public void confirm(CorrelationData correlationData, boolean ack, String cause) {
if (ack) {
System.out.println("消息发送成功:" + correlationData);
} else {
System.out.println("消息发送失败:" + cause);
}
}
@Override
public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
System.out.println(message.getMessageProperties().getCorrelationIdString() + " 发送失败");
}
//发送消息,不需要实现任何接口,供外部调用。
public void send(String msg){
CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString());
System.out.println("开始发送消息 : " + msg.toLowerCase());
String response = rabbitTemplate.convertSendAndReceive("topicExchange", "key.1", msg, correlationId).toString();
System.out.println("结束发送消息 : " + msg.toLowerCase());
System.out.println("消费者响应 : " + response + " 消息处理完成");
}
}
消费者类(消费消息)
package com.fhsn.weixin.config.rabbitmq;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class Receiver {
@RabbitListener(queues = "hello.queue1")
public String processMessage1(String msg) {
System.out.println(Thread.currentThread().getName() + " 接收到来自hello.queue1队列的消息:" + msg);
return msg.toUpperCase();
}
@RabbitListener(queues = "hello.queue2")
public void processMessage2(String msg) {
System.out.println(Thread.currentThread().getName() + " 接收到来自hello.queue2队列的消息:" + msg);
}
}
第四步:
在项目pom.xml文件中添加 spring-boot-starter-test依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>1.5.9.RELEASE</version>
<scope>test</scope>
</dependency>
第五步:添加测试类
package com.fhsn.weixn.controller.rabbitmq;
import java.util.Date;
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.SpringJUnit4ClassRunner;
import com.fhsn.WeixinWebApplication;
import com.fhsn.weixin.config.rabbitmq.Sender;
@RunWith(value=SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = WeixinWebApplication.class)
public class RabbitMqTests {
@Autowired
private Sender sender;
@Test
public void sendTest() throws Exception {
while(true){
String msg = new Date().toString();
sender.send(msg);
Thread.sleep(1000);
}
}
}
第六步 : 右击选中 RabbitMqTests->Run As->Junit Test 运行测试类,查看console控制台输出结果如下,即表示springboot成功集成rabbitmq
开始发送消息 : wed may 16 14:43:40 cst 2018
SimpleAsyncTaskExecutor-1 接收到来自hello.queue1队列的消息:Wed May 16 14:43:40 CST 2018
SimpleAsyncTaskExecutor-1 接收到来自hello.queue2队列的消息:Wed May 16 14:43:40 CST 2018
结束发送消息 : wed may 16 14:43:40 cst 2018
消费者响应 : WED MAY 16 14:43:40 CST 2018 消息处理完成
开始发送消息 : wed may 16 14:43:42 cst 2018
SimpleAsyncTaskExecutor-2 接收到来自hello.queue1队列的消息:Wed May 16 14:43:42 CST 2018
SimpleAsyncTaskExecutor-2 接收到来自hello.queue2队列的消息:Wed May 16 14:43:42 CST 2018
结束发送消息 : wed may 16 14:43:42 cst 2018
消费者响应 : WED MAY 16 14:43:42 CST 2018 消息处理完成
开始发送消息 : wed may 16 14:43:43 cst 2018
SimpleAsyncTaskExecutor-1 接收到来自hello.queue1队列的消息:Wed May 16 14:43:43 CST 2018
SimpleAsyncTaskExecutor-1 接收到来自hello.queue2队列的消息:Wed May 16 14:43:43 CST 2018
结束发送消息 : wed may 16 14:43:43 cst 2018
消费者响应 : WED MAY 16 14:43:43 CST 2018 消息处理完成
开始发送消息 : wed may 16 14:43:44 cst 2018
SimpleAsyncTaskExecutor-2 接收到来自hello.queue2队列的消息:Wed May 16 14:43:44 CST 2018
SimpleAsyncTaskExecutor-2 接收到来自hello.queue1队列的消息:Wed May 16 14:43:44 CST 2018
结束发送消息 : wed may 16 14:43:44 cst 2018
消费者响应 : WED MAY 16 14:43:44 CST 2018 消息处理完成
参考文章来源:
https://www.jianshu.com/p/e1258c004314