# rabbitmq-master **Repository Path**: 199115/rabbitmq-master ## Basic Information - **Project Name**: rabbitmq-master - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2018-09-05 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # rabbitmq-master #### 项目介绍 以下是rabbitmq 的详细编码过程,如果有不懂得可以添加我的QQ:一同交流和学习 #### 软件架构 软件架构说明 rabbitmq 代码教程 #### 笔记 1.添加依赖 org.springframework.boot spring-boot-starter-amqp 2. 配置application server: port: 9000 spring: rabbitmq: host: 192.168.0.22 port: 5672 username: root password: root publisher: true 3. 创建配置类 :RabbitMqConfig package com.example.demo.config; import com.example.demo.MqCallBack.MsgSendConfirmCallBack; import org.springframework.amqp.core.AcknowledgeMode; import org.springframework.amqp.core.Binding; import org.springframework.amqp.core.BindingBuilder; import org.springframework.amqp.rabbit.connection.ConnectionFactory; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class RabbitMqConfig { /** 消息交换机的名字*/ public static final String EXCHANGE = "exchangeTest"; /** 队列key1*/ public static final String ROUTINGKEY1 = "queue_one_key1"; /** 队列key2*/ public static final String ROUTINGKEY2 = "queue_one_key2"; @Autowired private QueueConfig queueConfig; @Autowired private ExchangeConfig exchangeConfig; /** * 连接工厂 */ @Autowired private ConnectionFactory connectionFactory; /** 将消息队列1和交换机进行绑定 */ @Bean public Binding binding_one() { return BindingBuilder.bind(queueConfig.firstQueue()).to(exchangeConfig.directExchange()).with(RabbitMqConfig.ROUTINGKEY1); } /** * 将消息队列2和交换机进行绑定 */ @Bean public Binding binding_two(){ return BindingBuilder.bind(queueConfig.secondQueue()).to(exchangeConfig.directExchange()).with(RabbitMqConfig.ROUTINGKEY2); } /** * queue listener 观察 监听模式 * 当有消息到达时会通知监听在对应的队列上的监听对象 * @return */ @Bean public SimpleMessageListenerContainer simpleMessageListenerContainer_one(){ SimpleMessageListenerContainer simpleMessageListenerContainer = new SimpleMessageListenerContainer(connectionFactory); simpleMessageListenerContainer.addQueues(queueConfig.firstQueue()); simpleMessageListenerContainer.setExposeListenerChannel(true); simpleMessageListenerContainer.setMaxConcurrentConsumers(5); simpleMessageListenerContainer.setConcurrentConsumers(1); simpleMessageListenerContainer.setAcknowledgeMode(AcknowledgeMode.MANUAL); //设置确认模式手工确认 return simpleMessageListenerContainer; } /** * 定义rabbit template用于数据的接收和发送 * @return */ @Bean public RabbitTemplate rabbitTemplate() { RabbitTemplate template = new RabbitTemplate(connectionFactory); /**若使用confirm-callback或return-callback, * 必须要配置publisherConfirms或publisherReturns为true * 每个rabbitTemplate只能有一个confirm-callback和return-callback */ template.setConfirmCallback(this.msgSendConfirmCallBack()); //template.setReturnCallback(msgSendReturnCallback()); /** * 使用return-callback时必须设置mandatory为true,或者在配置中设置mandatory-expression的值为true, * 可针对每次请求的消息去确定’mandatory’的boolean值, * 只能在提供’return -callback’时使用,与mandatory互斥 */ // template.setMandatory(true); return template; } /** * 消息确认机制 * Confirms给客户端一种轻量级的方式,能够跟踪哪些消息被broker处理, * 哪些可能因为broker宕掉或者网络失败的情况而重新发布。 * 确认并且保证消息被送达,提供了两种方式:发布确认和事务。(两者不可同时使用) * 在channel为事务时,不可引入确认模式;同样channel为确认模式下,不可使用事务。 * @return */ @Bean public MsgSendConfirmCallBack msgSendConfirmCallBack(){ return new MsgSendConfirmCallBack(); } } 4. 创建消息队列配置 QueueConfig package com.example.demo.config; import org.springframework.amqp.core.Queue; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * 消息队列配置 */ @Configuration public class QueueConfig { @Bean public Queue firstQueue(){ /** durable="true" 持久化 rabbitmq重启的时候不需要创建新的队列 auto-delete 表示消息队列没有在使用时将被自动删除 默认是false exclusive 表示该消息队列是否只在当前connection生效,默认是false */ return new Queue("first-queue",true,false,false); } @Bean public Queue secondQueue() { return new Queue("second-queue",true,false,false); } } 5. 创建交换机配置类 ExchangeConfig package com.example.demo.config; import org.springframework.amqp.core.DirectExchange; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * 交换机配置类 */ @Configuration public class ExchangeConfig { @Bean public DirectExchange directExchange(){ DirectExchange exchange=new DirectExchange(RabbitMqConfig.EXCHANGE,true,false); return exchange; } } 6. 创建mq的回调机制 :MsgSendConfirmCallBack package com.example.demo.MqCallBack; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.amqp.rabbit.support.CorrelationData; /** * 消息回调 */ public class MsgSendConfirmCallBack implements RabbitTemplate.ConfirmCallback { @Override public void confirm(CorrelationData correlationData, boolean ack, String cause) { System.out.println("MsgSendConfirmCallBack , 回调id:" + correlationData); if (ack) { System.out.println("消息消费成功"); } else { System.out.println("消息消费失败:" + cause+"\n重新发送"); } } } 7. 创建消息发送着 package com.example.demo.send; import com.example.demo.config.RabbitMqConfig; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.amqp.rabbit.support.CorrelationData; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class FirstSender { @Autowired private RabbitTemplate template; /** * 发送消息 * @param uuid * @param msg */ public void sendMsg(String uuid ,Object msg){ CorrelationData correlationId = new CorrelationData(uuid); template.convertAndSend(RabbitMqConfig.EXCHANGE, RabbitMqConfig.ROUTINGKEY2, msg, correlationId); } } 8. 创建消息接受着 FirstConsumer package com.example.demo.receiver; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; /** * 接收消息 */ @Component public class FirstConsumer { @RabbitListener(queues = {"first-queue","second-queue"}, containerFactory = "rabbitListenerContainerFactory") public void handleMessage(String message) throws Exception { // 处理消息 System.out.println("FirstConsumer {} handleMessage :"+message); } } 9.创建测试的controller package com.example.demo.controller; import com.example.demo.send.FirstSender; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.util.UUID; @RestController public class SendController { @Autowired private FirstSender firstSender; @GetMapping("/send") public String send(String message){ String uuid = UUID.randomUUID().toString(); firstSender.sendMsg(uuid,message); return uuid; } } #### 项目启动说明 本项目是使用的springboot+rabbitmq 所以需要添加必要的依赖进行测试, 启动springboot 的启动类, 在首页访问 http://192.168.0.111:9000/send?message=adasd 进行测试访问