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); } }