rabbitMQ:先放两张图 方便记忆java
视频教程:https://study.163.com/course/courseMain.htm?courseId=1004576013web
环境:首先要安装好 rabbitMQspring
springboot 集成 rabbitMQ仍是挺简单的。springboot
rabbitMQ的原理是(我的理解),本身作好的demo已经保存到百度网盘下。app
生产者:发送消息给交换器 exchange 消费者:监听队列消息 队列:队列和交换器绑定 交换器:生产者发送消息给路由器 交换器根据规则发送消息到队列
1.建立一个springboot项目 依赖:web,rabbitMQide
2.配置propertiesthis
spring.application.name=rabbitMQ server.port=8080 spring.rabbitmq.host=172.17.0.50 spring.rabbitmq.port=5672 spring.rabbitmq.username=hanhao spring.rabbitmq.password=hanhao
第二种配置:directspa
spring.application.name=rabbitDirectProvider server.port=9999 spring.rabbitmq.host=172.17.0.50 spring.rabbitmq.port=5672 spring.rabbitmq.username=hanhao spring.rabbitmq.password=hanhao mq.config.exchange=log.direct
第三种:topic 匹配模式debug
消费者:code
package com.bicon.directconsumer; import org.springframework.amqp.core.ExchangeTypes; import org.springframework.amqp.rabbit.annotation.Exchange; import org.springframework.amqp.rabbit.annotation.Queue; import org.springframework.amqp.rabbit.annotation.QueueBinding; import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component @RabbitListener(bindings=@QueueBinding( value=@Queue(value="${mq.config.queue.error}",autoDelete="true"), exchange=@Exchange(value="${mq.config.exchange}",type=ExchangeTypes.TOPIC), key="*.log.error" ) ) public class ErrorReceive { @RabbitHandler public void process(String msg) { System.out.println("接收到ERROR:"+msg); } }
生产者:
package com.bicon.directprovider; import javax.validation.Valid; import net.bytebuddy.asm.Advice.This; import org.springframework.amqp.core.AmqpTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class OrderSender { @Autowired private AmqpTemplate rabbitTemplate; @Value("${mq.config.exchange}") private String exchange; public void send(String msg){ this.rabbitTemplate.convertAndSend(this.exchange,"order.log.info",msg); this.rabbitTemplate.convertAndSend(this.exchange,"order.log.debug",msg); this.rabbitTemplate.convertAndSend(this.exchange,"order.log.warn",msg); this.rabbitTemplate.convertAndSend(this.exchange,"order.log.error",msg); } }