安装RebbitMq可以参考:https://blog.csdn.net/qq_47588845/article/details/107986373
1.添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> <version>1.5.14.RELEASE</version> </dependency>
2.添加配置
spring:
#配置rabbitMq 服务器
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
3.添加配置类DirectRabbitConfig
package com.llltony.springboot.utils;
import org.apache.log4j.Logger;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* DirectRabbitConfig
*
* @author: lll
* @date: 2021年04月11日 15:04:38
*/
@Configuration
public class DirectRabbitConfig {
//队列 起名:EmployeeDirectQueue
@Bean
public Queue EmployeeDirectQueue() {
// durable:是否持久化,默认是false,持久化队列:会被存储在磁盘上,当消息代理重启时仍然存在,暂存队列:当前连接有效
// exclusive:默认也是false,只能被当前创建的连接使用,而且当连接关闭后队列即被删除。此参考优先级高于durable
// autoDelete:是否自动删除,当没有生产者或者消费者使用此队列,该队列会自动删除。
// return new Queue("EmployeeDirectQueue",true,true,false);
//一般设置一下队列的持久化就好,其余两个就是默认false
return new Queue("EmployeeDirectQueue",true);
}
//Direct交换机 起名:EmployeeDirectExchange
@Bean
DirectExchange EmployeeDirectExchange() {
// return new DirectExchange("EmployeeDirectExchange",true,true);
return new DirectExchange("EmployeeDirectExchange",true,false);
}
//绑定 将队列和交换机绑定, 并设置用于匹配键:EmployeeDirectRouting
@Bean
Binding bindingDirect() {
return BindingBuilder.bind(EmployeeDirectQueue()).to(EmployeeDirectExchange()).with("EmployeeDirectRouting");
}
@Bean
DirectExchange lonelyDirectExchange() {
return new DirectExchange("lonelyDirectExchange");
}
}
4.添加监听类DirectReceiver
package com.llltony.springboot.listener; import org.apache.log4j.Logger; import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; import java.util.Map; /** * 消费者队列监听 * * @author: lll * @date: 2021年04月11日 16:04:14 */ @Component @RabbitListener(queues = "EmployeeDirectQueue")//监听的队列名称 EmployeeDirectQueue public class DirectReceiver { private static final Logger logger = Logger.getLogger(com.llltony.springboot.service.Impl.EmployeeServiceImpl.class); @RabbitHandler public void process(Map testMessage) { logger.info("DirectReceiver消费者收到消息 : " + testMessage.toString()); } }
5.添加调用
package com.llltony.springboot.controller; import com.llltony.springboot.bean.Employee; import com.llltony.springboot.bean.EmployeeVo; import com.llltony.springboot.bean.ResultMap; import com.llltony.springboot.service.EmployeeService; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.io.IOException; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; @RestController @RequestMapping("/employee") public class EmployeeController { @Resource EmployeeService employeeService; @Resource RabbitTemplate rabbitTemplate;//通过消息队列异步增加学生 @RequestMapping(value = "/addRebbitMq", method = RequestMethod.POST) public ResultMap addRebbitMq(@RequestBody EmployeeVo employeeVo) { ResultMap resultMap = new ResultMap(); try { String messageId = String.valueOf(UUID.randomUUID()); String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); Map<String, Object> map = new HashMap<>(); map.put("messageId", messageId); map.put("messageData", employeeVo); map.put("createTime", createTime); //将消息携带绑定键值:TestDirectRouting 发送到交换机TestDirectExchange rabbitTemplate.convertAndSend("EmployeeDirectExchange", "EmployeeDirectRouting", map); // employeeService.saveEmp(employeeVo); resultMap.setStatus("200"); resultMap.setMessage("保存成功"); } catch (Exception e) { resultMap.setStatus("500"); resultMap.setMessage("保存失败"); } return resultMap; } }
6.实际效果
7.资源
源码:https://github.com/CodingPandaLLL/tsl.git
源码压缩包地址:https://codeload.github.com/CodingPandaLLL/tsl/zip/refs/tags/1.0.3