Spring Boot Sample 027之spring-boot-mq-rabbitmq
一、环境
- Idea 2020.1
- JDK 1.8
- maven
二、目的
spring boot 通过整合rabbitMQ
gitHub地址: https://github.com/ouyushan/ouyushan-spring-boot-samples
三、步骤
3.1、点击File -> New Project -> Spring Initializer,点击next
3.2、在对应地方修改自己的项目信息
3.3、选择Web依赖,选中Spring Web、Spring Boot DevTools、Spring for RabbitMQ。可以选择Spring Boot版本,本次默认为2.3.0,点击Next
3.4、项目结构
四、添加文件
pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.ouyushan</groupId>
<artifactId>ouyushan-spring-boot-samples</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>org.ouyushan</groupId>
<artifactId>spring-boot-mq-rabbitmq</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-mq-rabbitmq</name>
<description>Rabbitmq project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties文件
# RABBIT (RabbitProperties)
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
# 开启发送确认
spring.rabbitmq.publisher-confirm-type=simple
# 开启发送失败退回
spring.rabbitmq.publisher-returns=true
# 开启ACK
spring.rabbitmq.listener.direct.acknowledge-mode=manual
spring.rabbitmq.listener.simple.acknowledge-mode=manual
RabbitMqConfig.java
package org.ouyushan.springboot.mq.rabbitmq.config;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Description:
* @Author: ouyushan
* @Email: ouyushan@hotmail.com
* @Date: 2020/6/8 10:18
*/
@Configuration
public class RabbitMqConfig {
@Bean
public Queue kinsonQueue() {
return new Queue("ouyushan.queue");
}
}
mq消费监听器
RabbitMQConsumerListener.java
package org.ouyushan.springboot.mq.rabbitmq.listener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* @Description:
* @Author: ouyushan
* @Email: ouyushan@hotmail.com
* @Date: 2020/6/4 16:46
*/
@Component
public class RabbitMQConsumerListener {
private static Logger logger = LoggerFactory.getLogger(RabbitMQConsumerListener.class);
/* 接收消息 */
@RabbitListener(queues = "ouyushan.queue")
public void receiveQueue(String text) {
logger.info("接受到:" + text);
}
}
通过接口调用模拟mq生产者
RabbitMQProducerController.java
package org.ouyushan.springboot.mq.rabbitmq.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Description:
* @Author: ouyushan
* @Email: ouyushan@hotmail.com
* @Date: 2020/6/4 16:47
*/
@RestController
public class RabbitMQProducerController {
private static Logger logger = LoggerFactory.getLogger(RabbitMQProducerController.class);
@Autowired
private AmqpTemplate amqpTemplate;
/* 发送消息 */
@RequestMapping(value = "/sendMsg")
public void send(String msg) {
this.amqpTemplate.convertAndSend("ouyushan.queue", msg);
logger.info("msg:" + msg);
}
}
五、测试
SpringBootMqRabbitmqApplicationTests.java
package org.ouyushan.springboot.mq.rabbitmq;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest
@AutoConfigureMockMvc
class SpringBootMqRabbitmqApplicationTests {
@Test
public void testMQProducer(@Autowired MockMvc mockMvc) throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/sendMsg?msg=sendMSG")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(print());
}
}
执行测试方法后,控制台打印:
2020-06-08 10:40:16.266 INFO 8004 --- [ main] o.o.s.m.r.c.RabbitMQProducerController : msg:sendMSG
2020-06-08 10:40:16.312 INFO 8004 --- [ntContainer#0-1] o.o.s.m.r.l.RabbitMQConsumerListener : 接受到:sendMSG
创建项目后运行SpringBootMqRabbitmqApplication.java类, 然后访问 - get请求 - localhost:8080/sendMsg?msg=sendMSG 登录后台管理 账号/密码 guest/ http://localhost:15672/