RabbitMQ 在Spring Boot上的使用
1.pom

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- rabbitmq--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency>
2.application.yml

server: port: 82 max-http-header-size: 10240 spring: profiles: active: local rabbitmq: host: 192.168.239.137 username: guest password: guest
3.RabbitMqConfig

package com.tf.demo.config; import org.springframework.amqp.core.*; import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter; import org.springframework.amqp.support.converter.MessageConverter; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class RabbitmqConfig { @Bean public MessageConverter messageConverter() { return new Jackson2JsonMessageConverter(); } //default @Bean protected Queue defaultQueue(){ return QueueBuilder.durable("defaultqueue").build(); } //direct @Bean protected Queue directQueue(){ return QueueBuilder.durable("directqueue").build(); } @Bean protected DirectExchange directExchange() { return new DirectExchange("amq.direct"); } @Bean protected Binding directBinding(Queue directQueue, DirectExchange directExchange) { return BindingBuilder.bind(directQueue).to(directExchange).with("directroutingKey"); } //fanout : 广播 。 routingKey对于fanout没有意义的 @Bean protected Queue fanoutQueue(){ return QueueBuilder.durable("fanoutqueue").build(); } @Bean protected FanoutExchange fanoutExchange() { return new FanoutExchange("amq.fanout"); } @Bean protected Binding fanoutBinding(Queue fanoutQueue, FanoutExchange fanoutExchange) { return BindingBuilder.bind(fanoutQueue).to(fanoutExchange); } //topic @Bean protected Queue topicQueue(){ return QueueBuilder.durable("topicqueue").build(); } @Bean protected TopicExchange topicExchange() { return new TopicExchange("amq.topic"); } @Bean protected Binding topicBinding(Queue topicQueue, TopicExchange topicExchange) { return BindingBuilder.bind(topicQueue).to(topicExchange).with("com.topic.*"); //*代表一个单词,#代表一个或多个 } @Bean protected Binding topicBinding2(Queue topicQueue, TopicExchange topicExchange) { return BindingBuilder.bind(topicQueue).to(topicExchange).with("com.topic.#"); //*代表一个单词,#代表一个或多个 } }
4.Publisher

package com.tf.demo.test; import com.tf.demo.DemoApplication; import com.tf.demo.service.rabbitmq.message.HelloMsg; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.amqp.core.AmqpTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * Publisher 与 exchange、routingKey 有关系 * exchange 与 routingKey 找到 queue * Consumer 与 queue 有关系 * */ @SpringBootTest(classes = DemoApplication.class) @RunWith(SpringJUnit4ClassRunner.class) public class Publisher { @Autowired private AmqpTemplate amqpTemplate; //默认交换器: direct : 公平调度 @Test public void testDefault(){ var routingKey = "defaultqueue"; var message = HelloMsg.builder().id(1L).name("hello default").build(); amqpTemplate.convertAndSend(routingKey, message); System.out.println("发送成功"); } @Test public void testDirect(){ var exchange = "amq.direct"; var routingKey = "directroutingKey"; var message = HelloMsg.builder().id(1L).name("hello direct").build(); amqpTemplate.convertAndSend(exchange, routingKey, message); System.out.println("发送成功"); } @Test public void testFanout(){ var exchange = "amq.fanout"; var message = HelloMsg.builder().id(1L).name("hello fanout").build(); amqpTemplate.convertAndSend(exchange, "", message); //routingKey 随便写 System.out.println("发送成功"); } @Test public void testTopic(){ var exchange = "amq.topic"; var routingKey = "com.topic.a"; var message = HelloMsg.builder().id(1L).name("hello topic").build(); amqpTemplate.convertAndSend(exchange, routingKey, message); System.out.println("发送成功"); } }
5.Consumer

package com.tf.demo.service.rabbitmq; import com.alibaba.fastjson.JSON; import com.tf.demo.service.rabbitmq.message.HelloMsg; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Service; @Service public class Consumer { @RabbitListener(queues = "defaultqueue") public void demo1(HelloMsg msg){ System.out.println("获取到的消息:"+ JSON.toJSONString(msg)); } @RabbitListener(queues = "directqueue") public void demo2(HelloMsg msg){ System.out.println("获取到的消息:"+msg); } @RabbitListener(queues = "fanoutqueue") public void demo3(HelloMsg msg){ System.out.println("获取到的消息:"+msg); } @RabbitListener(queues = "topicqueue") public void demo4(HelloMsg msg){ System.out.println("获取到的消息:"+msg); } }
@RabbitListener
@RabbitHandler
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
2021-07-18 打包遇到错误Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test
2019-07-18 IO读取两个Csv文件并比对key不存在的数据, 将其重新放进一个文件中
2018-07-18 java中数的表示