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;
@Configuration
@Getter
public class SimpleMQConfig {
public static final String SIMPLE_QUEUE_NAME = "com.yvxiao.mq.simple";
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;
@Slf4j
@Component
public class SimpleProducer {
private RabbitTemplate rabbitTemplate;
@Autowired
public SimpleProducer(RabbitTemplate rabbitTemplate) {
this.rabbitTemplate = rabbitTemplate;
}
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;
@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;
@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);
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2021-03-23 Docker安装Nginx,Redis并进行配置