日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

RabbitMQ--topic

發(fā)布時間:2023/12/3 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 RabbitMQ--topic 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
  • Topic類型的Exchange與Direct相比,都是可以根據(jù)RoutingKey把消息路由到不同的隊列。只不過Topic類型Exchange可以讓隊列在綁定Routing key 的時候使用通配符!

  • Routingkey 一般都是有一個或多個單詞組成,多個單詞之間以”.”分割,例如: item.insert

通配符規(guī)則:

  • #:匹配一個或多個詞

  • *:匹配不多不少恰好1個詞

舉例:

  • item.#:能夠匹配item.spu.insert 或者 item.spu

  • item.*:只能匹配item.spu


解釋:

  • Queue1:綁定的是china.# ,因此凡是以 china.開頭的routing key 都會被匹配到。包括china.news和china.weather
  • Queue2:綁定的是#.news ,因此凡是以 .news結(jié)尾的 routing key 都會被匹配。包括china.news和japan.news

舉例:

public_topic 消息生產(chǎn)者

@RunWith(SpringRunner.class) @SpringBootTest public class TestMqTopic {@Autowiredprivate RabbitTemplate rabbitTemplate;@Testpublic void testTopic1(){//發(fā)送的目標交換機String exchange = "itcast.topic";String message="我王二天下無敵1";rabbitTemplate.convertAndSend(exchange,"china.RAP",message);}@Testpublic void testTopic2(){//發(fā)送的目標交換機String exchange = "itcast.topic";String message="我王二天下無敵2";rabbitTemplate.convertAndSend(exchange,"yangshi.news",message);}@Testpublic void testTopic3(){//發(fā)送的目標交換機String exchange = "object.json";Student stu = new Student("王二", 1, new String[]{"sing", "dance", "rap"});rabbitTemplate.convertAndSend(exchange,"china.json",stu);} }

配置

spring:rabbitmq:host: 192.168.23.130port: 5672username: itcastpassword: 123321virtual-host: /

consumer_topic 消息消費者

@Component public class SpringRabbitListener {//- Topic交換機接收的消息RoutingKey必須是多個單詞,以 `**.**` 分割//- Topic交換機與隊列綁定時的bindingKey可以指定通配符//- `#`:代表0個或多個詞//- `*`:代表1個詞@RabbitListener(bindings = @QueueBinding(value = @Queue("topicQueue1"),exchange = @Exchange(value = "itcast.topic",type = ExchangeTypes.TOPIC),key = {"china.#"}))public void listenerTopicQueue1(String msg){System.out.println("topicQueue1接收的消息為"+msg);}@RabbitListener(bindings = @QueueBinding(value = @Queue("topicQueue2"),exchange = @Exchange(value = "itcast.topic",type = ExchangeTypes.TOPIC),key = {"#.news"}))public void listenerTopicQueue2(String msg){System.out.println("topicQueue2接收的消息為"+msg);}@RabbitListener(bindings = @QueueBinding(value = @Queue("topicQueue3"),exchange = @Exchange(value = "object.json",type = ExchangeTypes.TOPIC),key = {"#.json"}))public void listenerTopicQueue3(Student msg){System.out.println("topicQueue3接收的對象為"+msg);} }

啟動類配置對象轉(zhuǎn)化可在可視化界面查看

@SpringBootApplication public class MqTopicApplication {public static void main(String[] args) {SpringApplication.run(MqTopicApplication.class);}//配置消息轉(zhuǎn)換器@Beanpublic MessageConverter jsonMessageConverter(){return new Jackson2JsonMessageConverter();} }

配置

spring:rabbitmq:host: 192.168.23.130port: 5672username: itcastpassword: 123321virtual-host: /

所需依賴

<!--AMQP依賴,包含RabbitMQ--> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId> </dependency><!--json轉(zhuǎn)化依賴--><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId></dependency>

父依賴

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.9.RELEASE</version><relativePath/></parent><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><!--AMQP依賴,包含RabbitMQ--><dependencies><!--json轉(zhuǎn)化依賴--><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><!--AMQP依賴,包含RabbitMQ--><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></dependency></dependencies>

總結(jié)

以上是生活随笔為你收集整理的RabbitMQ--topic的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。