RabbitMQ的几种模式
1."Hello world"(点对点的模式)
2.发布订阅模式
3.主题模式(带了条件而已)
常用的几种交换机
1.Direct交换机
2.主题交换机(* #)
3.分裂模式(跟路由键是没有关系的,只要交换机绑定的队列都是可以收到消息的)
4.Header模式
使用javaAPI操作RabbitMQ
1.先要创建连接工厂 ConnectionFactory
2.创建连接对象 Connection
3.创建信道 Channel
3.信道消费或者发送消息
使用SpringBoot整合RabbitMQ
1.先启动RabbitMQ服务
systemctl restart rabbitmq-server.service
2.创建消息的消费者
-
application.yml
spring: rabbitmq: host: 192.168.126.144
-
pom.xml
<dependencies> <!--springboot整合rmq的依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit-test</artifactId> <scope>test</scope> </dependency> </dependencies>
-
RabbitConfig.java
package com.ddmzx.springboot_rabbitvip_consumer.config; import org.springframework.amqp.core.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.annotation.Resource; @Configuration public class RabbitConfig { //定义三种类型的交换机 //直连交换机 @Bean public DirectExchange directExchange(){ return new DirectExchange("DIRECT_EXCHANGE"); } //主题交换机 @Bean public TopicExchange topicExchange(){ return new TopicExchange("TOPIC_EXCHANGE"); } //广播交换机 @Bean public FanoutExchange fanoutExchange(){ return new FanoutExchange("FANOUT_EXCHANGE"); } //还需要创建队列(创建四种类型的队列) @Bean public Queue firstQueue(){ return new Queue("FIRST_QUEUE"); } @Bean public Queue secondQueue(){ return new Queue("SECOND_QUEUE"); } @Bean public Queue thirdQueue(){ return new Queue("THIRD_QUEUE"); } @Bean public Queue fourthQueue(){ return new Queue("FOURTH_QUEUE"); } //Autowired是根据类型匹配的 /*@Autowired @Qualifier("studentDaoMySQLImpl") @Resource(name = "studentDaoMySQLImpl") private StudentDao studentDao;(StudentDaoMySQLImpl,StudentDaoOracleImpl)*/ //进行交换机和队列的绑定 @Bean public Binding bindFirst(@Qualifier("firstQueue") Queue queue, @Qualifier("directExchange") DirectExchange directExchange){ return BindingBuilder.bind(queue).to(directExchange).with("ddm.ange"); } @Bean public Binding bindSecond(@Qualifier("secondQueue") Queue queue, @Qualifier("topicExchange") TopicExchange topicExchange){ return BindingBuilder.bind(queue).to(topicExchange).with("*.ddm.*"); } @Bean public Binding bindThird(@Qualifier("thirdQueue") Queue queue, @Qualifier("fanoutExchange") FanoutExchange fanoutExchange){ return BindingBuilder.bind(queue).to(fanoutExchange); } @Bean public Binding bindFourth(@Qualifier("fourthQueue") Queue queue, @Qualifier("fanoutExchange") FanoutExchange fanoutExchange){ return BindingBuilder.bind(queue).to(fanoutExchange); } }
-
消费者
package com.ddmzx.springboot_rabbitvip_consumer.consumer; import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer; import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; /*指定当前的消费者监听哪个队列中的消息,一旦有消息过来就可以进行消费了*/ /*说明一个消费者可以监听多个队列中内容*/ @Component @RabbitListener(queues = {"FIRST_QUEUE"}) public class FirstConsumer { @RabbitHandler public void process(String msg){ System.out.println("first queue received msg : " + msg); } }
2.消息的生产者
-
application.yml
spring: rabbitmq: host: 192.168.126.144
-
MyProducer.java
package com.ddmzx.springboot_rabbitvip_producer.producer; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class MyProducer { @Autowired private RabbitTemplate rabbitTemplate; public void sendMsg(){ /* * 1.交换机的名称 * 2.路由键 * 3.具体的消息内容 * */ rabbitTemplate.convertAndSend("DIRECT_EXCHANGE","ddm.ange","a direct message"); rabbitTemplate.convertAndSend("TOPIC_EXCHANGE","shanghai.ddm.student","a topic message shanghai"); rabbitTemplate.convertAndSend("TOPIC_EXCHANGE","changsha.ddm.student","a topic message changsha"); rabbitTemplate.convertAndSend("FANOUT_EXCHANGE","","a fanout message"); } }
-
测试类
package com.ddmzx.springboot_rabbitvip_producer; import com.ddmzx.springboot_rabbitvip_producer.producer.MyProducer; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class SpringbootRabbitvipProducerApplicationTests { @Autowired private MyProducer myProducer; @Test void contextLoads() { myProducer.sendMsg(); } }