说明:本文章主要是Direct定向/直连类型交换机的使用,它的大致流程是将一个队列绑定到一个直连交换机上,并赋予一个路由键 routingkey,当一个消息携带着路由值为routingkey,这个消息通过生产者发送给交换机时,交换机就会根据这个路由值routingkey去寻找绑定的队列。
工程图:
A.总体maven引入
pom.xml
4.0.0spring-boot-starter-parentorg.springframework.boot2.2.2.RELEASE RabbitMqSpringbootDemoRabbitMqSpringbootDemo1.0-SNAPSHOTpomMqCustomerMqProducerRabbitMqSpringbootDemo Maven Webapphttp://www.example.comUTF-81.81.8RabbitMqSpringbootDemomaven-clean-plugin3.1.0maven-resources-plugin3.0.2maven-compiler-plugin3.8.0maven-surefire-plugin2.22.1maven-war-plugin3.2.2maven-install-plugin2.5.2maven-deploy-plugin2.8.2
B.生产者MqProducer
1.pom.xml
RabbitMqSpringbootDemoRabbitMqSpringbootDemo1.0-SNAPSHOT4.0.0MqProducerjarMqProducer Maven Webapphttp://www.example.comUTF-81.81.8org.springframework.bootspring-boot-starterorg.springframework.bootspring-boot-starter-testtestorg.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-devtoolstrueorg.springframework.bootspring-boot-starter-amqpcom.alibabafastjson1.2.78org.apache.commonscommons-lang33.4org.projectlomboklombok1.16.10MqProducermaven-clean-plugin3.1.0maven-resources-plugin3.0.2maven-compiler-plugin3.8.0maven-surefire-plugin2.22.1maven-war-plugin3.2.2maven-install-plugin2.5.2maven-deploy-plugin2.8.2
2.application.yml
server:port: 8080spring:rabbitmq:port: 5672host: 192.168.18.145username: adminpassword: adminvirtual-host: /
3.DirectQueueRabbitConfig
package com.dev.config;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;/** * 类名称:Direct定向/直连类型交换机的使用 * * @author lqw * @date 2024年02月28日 10:40 */@Configurationpublic class DirectQueueRabbitConfig {//队列 起名:directQueue@Beanpublic Queue directQueue() {return new Queue("directQueue",true);}//Direct交换机 起名:directExchange@BeanDirectExchange directExchange() {return new DirectExchange("directExchange",true,false);}//绑定将队列和交换机绑定, 并设置用于匹配键:directRoutingKey@BeanBinding bindingDirect() {return BindingBuilder.bind(directQueue()).to(directExchange()).with("directRoutingKey");}}
4.DirectQueueController
package com.dev.controller;import lombok.extern.slf4j.Slf4j;import org.springframework.amqp.rabbit.core.RabbitTemplate;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;import java.util.Map;/** * 类名称:直接交换机 消息生产者 * * @author lqw * @date 2024年02月27日 14:47 */@Slf4j@RestController@RequestMapping("directQueue")public class DirectQueueController {@AutowiredRabbitTemplate rabbitTemplate;//使用RabbitTemplate,这提供了接收/发送等等方法/** * Direct测试 * @return */@GetMapping("/sendMessage")public String sendMessage() {Map map = new HashMap();map.put("id","lalala");map.put("name","zhanglong");rabbitTemplate.convertAndSend("directExchange", "directRoutingKey", map);return "ok";}}
5.AppP
package com.dev;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;/** * 类名称: * * @author 李庆伟 * @date 2024年02月27日 14:20 */@SpringBootApplicationpublic class AppP {public static void main(String[] args) {SpringApplication.run(AppP.class);}}
C.消费者:MqCustomer
1.pom.xml
RabbitMqSpringbootDemoRabbitMqSpringbootDemo1.0-SNAPSHOT4.0.0MqCustomerjarMqCustomer Maven Webapphttp://www.example.comUTF-81.81.8org.springframework.bootspring-boot-starterorg.springframework.bootspring-boot-starter-testtestorg.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-devtoolstrueorg.springframework.bootspring-boot-starter-amqporg.apache.commonscommons-lang33.4org.projectlomboklombok1.16.10MqCustomermaven-clean-plugin3.1.0maven-resources-plugin3.0.2maven-compiler-plugin3.8.0maven-surefire-plugin2.22.1maven-war-plugin3.2.2maven-install-plugin2.5.2maven-deploy-plugin2.8.2
2.application.yml
server:port: 8081spring:rabbitmq:port: 5672host: 192.168.18.145username: adminpassword: admin
3.DirectQueueRabbitListener
package cn.ct.listeners;import org.springframework.amqp.rabbit.annotation.RabbitHandler;import org.springframework.amqp.rabbit.annotation.RabbitListener;import org.springframework.stereotype.Component;import java.util.Map;/** * 类名称:消息消费者 * 直连 * @author lqw * @date 2024年02月27日 14:58 */@Componentpublic class DirectQueueRabbitListener {@RabbitListener(queues = "directQueue")@RabbitHandlerpublic void process(Map msg) {System.out.println("Rabbitmq Direct : " + msg);System.out.println("Rabbitmq Direct : " + msg);}}
4.AppC
package cn.ct;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;/** * 类名称: * * @author lqw * @date 2024年02月27日 14:20 */@SpringBootApplicationpublic class AppC {public static void main(String[] args) {SpringApplication.run(AppC.class);}}