1. 环境
- 部署rabbitMQ 使用docker在阿里云上部署,可通过访问ip+15672访问管理页面(使用默认端口情况下)
-- 搜索镜像
docker pull rabbitmq
-- 拉取镜像(指定版本)
docker pull rabbitmq:3.8.0-beta.4-management
-- 启动容器
docker run -d --hostname my-rabbit -p 5672:5672 -p 15672:15672 rabbitmq:3.8.0-beta.4-management
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
<version>2.6.4</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
<dependency>
<groupId>com.github.javafaker</groupId>
<artifactId>javafaker</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
2. SimpleMQConfig 配置类
package com.yvxiao.rabbitmq_study.demo02.默认交换机;
import lombok.Getter;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* SimpleMQ配置类
* @author zouxiaoao
* @date 2022/3/23 16:00
*/
@Configuration
@Getter
public class SimpleMQConfig {
/**
* 队列名
*/
public static final String SIMPLE_QUEUE_NAME = "com.yvxiao.mq.simple";
/**
* 处理对象的MQ队列
*/
public static final String HANDLER_OBJECT_QUEUE_NAME = "com.yvxiao.mq.simple.object";
@Bean
public Queue simpleQueue() {
return new Queue(SIMPLE_QUEUE_NAME);
}
@Bean
public Queue handleObjectQueue() {
return new Queue(HANDLER_OBJECT_QUEUE_NAME);
}
}
3. SimpleProducer Simple生产者
package com.yvxiao.rabbitmq_study.demo02.默认交换机;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.yvxiao.rabbitmq_study.demo02.domain.Order;
import com.yvxiao.rabbitmq_study.demo02.domain.User;
import com.yvxiao.rabbitmq_study.demo02.user.api.domain.Teacher;
import lombok.extern.slf4j.Slf4j;
/**
* 生产者
* @author zouxiaoao
* @date 2022/3/23 16:02
*/
@Slf4j
@Component
public class SimpleProducer {
private RabbitTemplate rabbitTemplate;
@Autowired
public SimpleProducer(RabbitTemplate rabbitTemplate) {
this.rabbitTemplate = rabbitTemplate;
}
/**
* 发生消息到RabbitMQ,使用SpringBoot默认的交换机
*
* @param message
*/
public void sendMessage(String message) {
for (int i = 0; i < 20000; i++) {
log.info("Simple 生产消息: ===> {}",i);
rabbitTemplate.convertAndSend(SimpleMQConfig.SIMPLE_QUEUE_NAME, message+"===>"+i);
}
}
public void sendTeacher(Teacher teacher){
log.info("Teacher ===> {}",teacher);
rabbitTemplate.convertAndSend(SimpleMQConfig.SIMPLE_QUEUE_NAME,teacher);
}
}
4. SimpleConsumer 消费者
package com.yvxiao.rabbitmq_study.demo02.默认交换机;
import java.io.IOException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.Channel;
import com.yvxiao.rabbitmq_study.demo02.domain.Order;
import com.yvxiao.rabbitmq_study.demo02.user.api.domain.Teacher;
import com.yvxiao.rabbitmq_study.demo02.user.service.TeacherService;
import lombok.extern.slf4j.Slf4j;
/**
* 消费者
* @author zouxiaoao
* @date 2022/3/23 16:28
*/
@Component
@Slf4j
public class SimpleConsumer {
@Autowired
private TeacherService teacherService;
@RabbitListener(queues = {SimpleMQConfig.SIMPLE_QUEUE_NAME})
@RabbitHandler
public void receiveMessage(String message) {
log.info("simple consumer receive the message: ===> {}", message);
}
@RabbitHandler
@RabbitListener(queues = {SimpleMQConfig.SIMPLE_QUEUE_NAME})
public void receiveTeacher(Teacher teacher, Channel channel, Message message) throws IOException {
log.info("teacher ===> {}",teacher);
teacherService.insert(teacher);
}
}
Controller
package com.yvxiao.rabbitmq_study.demo02.user.controller;
import com.github.javafaker.Faker;
import com.yvxiao.rabbitmq_study.demo02.user.api.domain.Teacher;
import com.yvxiao.rabbitmq_study.demo02.user.service.TeacherService;
import com.yvxiao.rabbitmq_study.demo02.默认交换机.SimpleProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* TeacherController
*
* @author zouxiaoao
* @date 2022年03⽉23⽇ 15:15
*/
@RestController (省略Service,Mapper)
public class TeacherController {
@Autowired
private TeacherService teacherService;
@Autowired
private SimpleProducer simpleProducer;
@GetMapping(value = "/sendTeacher")
public void sendTeacher(){
for (int i = 0; i < 2; i++) {
Faker faker = new Faker();
Teacher teacher = new Teacher();
teacher.setId(i);
teacher.setName(faker.name().name());
simpleProducer.sendTeacher(teacher);
}
}
}