原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11794927.html

RMQ Topic

Project Directory

Maven Dependency

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6.  
  7. <groupId>org.fool.rmq</groupId>
  8. <artifactId>rmq</artifactId>
  9. <version>1.0-SNAPSHOT</version>
  10.  
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>2.2.0.RELEASE</version>
  15. </parent>
  16.  
  17. <dependencies>
  18. <dependency>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-starter-amqp</artifactId>
  21. </dependency>
  22.  
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-web</artifactId>
  26. </dependency>
  27.  
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-test</artifactId>
  31. <scope>test</scope>
  32. </dependency>
  33. </dependencies>
  34.  
  35. <build>
  36. <plugins>
  37. <plugin>
  38. <groupId>org.springframework.boot</groupId>
  39. <artifactId>spring-boot-maven-plugin</artifactId>
  40. </plugin>
  41. </plugins>
  42. </build>
  43. </project>

application.properties

  1. spring.application.name=rmq
  2. server.port=
  3.  
  4. spring.rabbitmq.host=localhost
  5. spring.rabbitmq.port=
  6. spring.rabbitmq.username=admin
  7. spring.rabbitmq.password=admin
  8.  
  9. #direct part start
  10. rmq.config.exchange=log.direct
  11. rmq.config.queue.info=log.info
  12. rmq.config.queue.info.routing.key=log.info.routing.key
  13. rmq.config.queue.error=log.error
  14. rmq.config.queue.error.routing.key=log.error.routing.key
  15. #direct part end
  16.  
  17. #topic part start
  18. rmq.config.exchange.topic=log.topic
  19. rmq.config.queue.topic.info=log.topic.info
  20. rmq.config.queue.topic.error=log.topic.error
  21. rmq.config.queue.topic.all=log.topic.all
  22.  
  23. rmq.config.queue.user.info.routing.key=user.log.info
  24. rmq.config.queue.user.debug.routing.key=user.log.debug
  25. rmq.config.queue.user.warn.routing.key=user.log.warn
  26. rmq.config.queue.user.error.routing.key=user.log.error
  27.  
  28. rmq.config.queue.product.info.routing.key=product.log.info
  29. rmq.config.queue.product.debug.routing.key=product.log.debug
  30. rmq.config.queue.product.warn.routing.key=product.log.warn
  31. rmq.config.queue.product.error.routing.key=product.log.error
  32.  
  33. rmq.config.queue.order.info.routing.key=order.log.info
  34. rmq.config.queue.order.debug.routing.key=order.log.debug
  35. rmq.config.queue.order.warn.routing.key=order.log.warn
  36. rmq.config.queue.order.error.routing.key=order.log.error
  37. #topic part end

Source Code

Application.java

  1. package org.fool.rmq;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5.  
  6. @SpringBootApplication
  7. public class Application {
  8. public static void main(String[] args) {
  9. SpringApplication.run(Application.class, args);
  10. }
  11. }

RmqConfig.java

  1. package org.fool.rmq.config;
  2.  
  3. import org.springframework.amqp.core.Queue;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6.  
  7. @Configuration
  8. public class RmqConfig {
  9.  
  10. @Bean
  11. public Queue queue() {
  12. return new Queue("hello-rmq");
  13. }
  14. }

UserProducer.java

  1. package org.fool.rmq.topic;
  2.  
  3. import org.springframework.amqp.core.AmqpTemplate;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.beans.factory.annotation.Value;
  6. import org.springframework.stereotype.Component;
  7.  
  8. import java.util.Date;
  9.  
  10. @Component
  11. public class UserProducer {
  12. @Autowired
  13. private AmqpTemplate rmqTemplate;
  14.  
  15. @Value("${rmq.config.exchange.topic}")
  16. private String exchange;
  17.  
  18. @Value("${rmq.config.queue.user.info.routing.key}")
  19. private String infoRoutingKey;
  20.  
  21. @Value("${rmq.config.queue.user.debug.routing.key}")
  22. private String debugRoutingKey;
  23.  
  24. @Value("${rmq.config.queue.user.warn.routing.key}")
  25. private String warnRoutingKey;
  26.  
  27. @Value("${rmq.config.queue.user.error.routing.key}")
  28. private String errorRoutingKey;
  29.  
  30. public void send() {
  31. String message = "Hello User";
  32. rmqTemplate.convertAndSend(exchange, infoRoutingKey, message + " info");
  33. rmqTemplate.convertAndSend(exchange, debugRoutingKey, message + " debug");
  34. rmqTemplate.convertAndSend(exchange, warnRoutingKey, message+ " warn");
  35. rmqTemplate.convertAndSend(exchange, errorRoutingKey, message + " error");
  36. }
  37. }

ProductProducer.java

  1. package org.fool.rmq.topic;
  2.  
  3. import org.springframework.amqp.core.AmqpTemplate;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.beans.factory.annotation.Value;
  6. import org.springframework.stereotype.Component;
  7.  
  8. import java.util.Date;
  9.  
  10. @Component
  11. public class ProductProducer {
  12. @Autowired
  13. private AmqpTemplate rmqTemplate;
  14.  
  15. @Value("${rmq.config.exchange.topic}")
  16. private String exchange;
  17.  
  18. @Value("${rmq.config.queue.product.info.routing.key}")
  19. private String infoRoutingKey;
  20.  
  21. @Value("${rmq.config.queue.product.debug.routing.key}")
  22. private String debugRoutingKey;
  23.  
  24. @Value("${rmq.config.queue.product.warn.routing.key}")
  25. private String warnRoutingKey;
  26.  
  27. @Value("${rmq.config.queue.product.error.routing.key}")
  28. private String errorRoutingKey;
  29.  
  30. public void send() {
  31. String message = "Hello Product";
  32. rmqTemplate.convertAndSend(exchange, infoRoutingKey, message + " info");
  33. rmqTemplate.convertAndSend(exchange, debugRoutingKey, message + " debug");
  34. rmqTemplate.convertAndSend(exchange, warnRoutingKey, message + " warn");
  35. rmqTemplate.convertAndSend(exchange, errorRoutingKey, message + " error");
  36. }
  37. }

OrderProducer.java

  1. package org.fool.rmq.topic;
  2.  
  3. import org.springframework.amqp.core.AmqpTemplate;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.beans.factory.annotation.Value;
  6. import org.springframework.stereotype.Component;
  7.  
  8. import java.util.Date;
  9.  
  10. @Component
  11. public class OrderProducer {
  12. @Autowired
  13. private AmqpTemplate rmqTemplate;
  14.  
  15. @Value("${rmq.config.exchange.topic}")
  16. private String exchange;
  17.  
  18. @Value("${rmq.config.queue.order.info.routing.key}")
  19. private String infoRoutingKey;
  20.  
  21. @Value("${rmq.config.queue.order.debug.routing.key}")
  22. private String debugRoutingKey;
  23.  
  24. @Value("${rmq.config.queue.order.warn.routing.key}")
  25. private String warnRoutingKey;
  26.  
  27. @Value("${rmq.config.queue.order.error.routing.key}")
  28. private String errorRoutingKey;
  29.  
  30. public void send() {
  31. String message = "Hello Order";
  32. rmqTemplate.convertAndSend(exchange, infoRoutingKey, message + " info");
  33. rmqTemplate.convertAndSend(exchange, debugRoutingKey, message + " debug");
  34. rmqTemplate.convertAndSend(exchange, warnRoutingKey, message + " warn");
  35. rmqTemplate.convertAndSend(exchange, errorRoutingKey, message + " error");
  36. }
  37. }

TopicLogInfoConsumer.java

  1. package org.fool.rmq.topic;
  2.  
  3. import org.springframework.amqp.core.ExchangeTypes;
  4. import org.springframework.amqp.rabbit.annotation.Exchange;
  5. import org.springframework.amqp.rabbit.annotation.Queue;
  6. import org.springframework.amqp.rabbit.annotation.QueueBinding;
  7. import org.springframework.amqp.rabbit.annotation.RabbitHandler;
  8. import org.springframework.amqp.rabbit.annotation.RabbitListener;
  9. import org.springframework.stereotype.Component;
  10.  
  11. @Component
  12. @RabbitListener(bindings = @QueueBinding(
  13. value = @Queue(value = "${rmq.config.queue.topic.info}", autoDelete = "true"),
  14. exchange = @Exchange(value = "${rmq.config.exchange.topic}", type = ExchangeTypes.TOPIC),
  15. key = "*.log.info"
  16. ))
  17. public class TopicLogInfoConsumer {
  18. @RabbitHandler
  19. public void handle(String message) {
  20. System.out.println("consume info: " + message);
  21. }
  22. }

TopicLogErrorConsumer.java

  1. package org.fool.rmq.topic;
  2.  
  3. import org.springframework.amqp.core.ExchangeTypes;
  4. import org.springframework.amqp.rabbit.annotation.Exchange;
  5. import org.springframework.amqp.rabbit.annotation.Queue;
  6. import org.springframework.amqp.rabbit.annotation.QueueBinding;
  7. import org.springframework.amqp.rabbit.annotation.RabbitHandler;
  8. import org.springframework.amqp.rabbit.annotation.RabbitListener;
  9. import org.springframework.stereotype.Component;
  10.  
  11. @Component
  12. @RabbitListener(bindings = @QueueBinding(
  13. value = @Queue(value = "${rmq.config.queue.topic.error}", autoDelete = "true"),
  14. exchange = @Exchange(value = "${rmq.config.exchange.topic}", type = ExchangeTypes.TOPIC),
  15. key = "*.log.error"
  16. ))
  17. public class TopicLogErrorConsumer {
  18. @RabbitHandler
  19. public void handle(String message) {
  20. System.out.println("consume error: " + message);
  21. }
  22. }

TopicLogAllConsumer.java

  1. package org.fool.rmq.topic;
  2.  
  3. import org.springframework.amqp.core.ExchangeTypes;
  4. import org.springframework.amqp.rabbit.annotation.Exchange;
  5. import org.springframework.amqp.rabbit.annotation.Queue;
  6. import org.springframework.amqp.rabbit.annotation.QueueBinding;
  7. import org.springframework.amqp.rabbit.annotation.RabbitHandler;
  8. import org.springframework.amqp.rabbit.annotation.RabbitListener;
  9. import org.springframework.stereotype.Component;
  10.  
  11. @Component
  12. @RabbitListener(bindings = @QueueBinding(
  13. value = @Queue(value = "${rmq.config.queue.topic.all}", autoDelete = "true"),
  14. exchange = @Exchange(value = "${rmq.config.exchange.topic}", type = ExchangeTypes.TOPIC),
  15. key = "*.log.*"
  16. ))
  17. public class TopicLogAllConsumer {
  18. @RabbitHandler
  19. public void handle(String message) {
  20. System.out.println("consume all: " + message);
  21. }
  22. }

ApplicationTest.java

  1. package org.fool.rmq.test;
  2.  
  3. import org.fool.rmq.Application;
  4. import org.fool.rmq.direct.LogProducer;
  5. import org.fool.rmq.producer.Producer;
  6. import org.fool.rmq.topic.OrderProducer;
  7. import org.fool.rmq.topic.ProductProducer;
  8. import org.fool.rmq.topic.UserProducer;
  9. import org.junit.Test;
  10. import org.junit.runner.RunWith;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.boot.test.context.SpringBootTest;
  13. import org.springframework.test.context.junit4.SpringRunner;
  14.  
  15. @RunWith(SpringRunner.class)
  16. @SpringBootTest(classes = Application.class)
  17. public class ApplicationTest {
  18.  
  19. @Autowired
  20. private Producer producer;
  21.  
  22. @Autowired
  23. private LogProducer logProducer;
  24.  
  25. @Autowired
  26. private UserProducer userProducer;
  27.  
  28. @Autowired
  29. private ProductProducer productProducer;
  30.  
  31. @Autowired
  32. private OrderProducer orderProducer;
  33.  
  34. @Test
  35. public void test() {
  36. producer.send();
  37. }
  38.  
  39. @Test
  40. public void testDirect() {
  41. logProducer.send();
  42. }
  43.  
  44. @Test
  45. public void testTopic() {
  46. userProducer.send();
  47. productProducer.send();
  48. orderProducer.send();
  49. }
  50. }

Console Output

RMQ Management

RMQ Topic的更多相关文章

  1. RMQ Fanout

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11795256.html RMQ Fanout Project Directory Maven Depe ...

  2. vivo 基于原生 RabbitMQ 的高可用架构实践

    一.背景说明 vivo 在 2016 年引入 RabbitMQ,基于开源 RabbitMQ 进行扩展,向业务提供消息中间件服务. 2016~2018年,所有业务均使用一个集群,随着业务规模的增长,集群 ...

  3. RMQ Terminology

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11784644.html RMQ模型架构 RMQ Terminology Message 消息,消息是不 ...

  4. Kafka 如何读取offset topic内容 (__consumer_offsets)

    众所周知,由于Zookeeper并不适合大批量的频繁写入操作,新版Kafka已推荐将consumer的位移信息保存在Kafka内部的topic中,即__consumer_offsets topic,并 ...

  5. Kafka如何创建topic?

    Kafka创建topic命令很简单,一条命令足矣:bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-facto ...

  6. Kafka0.8.2.1删除topic逻辑

    前提条件: 在启动broker时候开启删除topic的开关,即在server.properties中添加:  delete.topic.enable=true 命令: bin/kafka-topics ...

  7. BZOJ 3489: A simple rmq problem

    3489: A simple rmq problem Time Limit: 40 Sec  Memory Limit: 600 MBSubmit: 1594  Solved: 520[Submit] ...

  8. [bigdata] kafka基本命令 -- 迁移topic partition到指定的broker

    版本 0.9.2 创建topic bin/kafka-topics.sh --create --topic topic_name --partition 6 --replication-factor ...

  9. UVA 11235Frequent values(RMQ)

    训练指南P198 题意:给出一个非降序排列的整数数组a1, a2…… an,你的任务是对于一系列询问(i,j),回答ai, ai+1 ……aj 中出现的次数最多的次数 这题不仅学到了rmq的应用还学到 ...

随机推荐

  1. 封装redis(set/get/delete)str和哈希类型

    将Redis的常用操作封装了一下: import redis class MyRedis(): def __init__(self,ip,passwd,port=6379,db=0): #构造函数 t ...

  2. 从DT时代云栖大会聊聊恒生电子

    从IT到DT,除了HOMS和配资,本文结合互联网金融的背景,帮助读者对恒生电子600570了解更多. 马云在2015杭州·云栖大会Computing Conference发表致辞时多次强调DT(Dat ...

  3. 【openstf】自己的云测平台——mac安装openstf

    openstf的github地址:https://github.com/openstf/stf 上图可以清晰看出openstf的使用场景和效果   openstf是一个web应用程序,用于远程调试智能 ...

  4. idea的热部署

    1:先找到你要热部署的tomcat之后 ,在设置tomcat时  先选择 server,里面有On 'Update' action ()  和 On frame deactivation 这两项  都 ...

  5. Java课堂笔记(二):面向对象

    几乎每一本介绍Java语言的书中都会提到“面向对象”的这个概念,然而博主初学Java时看到这方面的内容一般都是草草地看一看,甚至是直接略过.原因很简单:考试基本不考,而且初学阶段写代码也很少用上.但事 ...

  6. [BZOJ 3731] Gty的超级妹子树 (树分块)

    [BZOJ 3731] Gty的超级妹子树 (树分块) 题面 给出一棵树(或森林),每个点都有一个值.现在有四种操作 1.查询x子树里>y的值有多少个 2.把点x的值改成y 3.添加一个新节点, ...

  7. [常用类]排序及Arrays类(简单介绍)

    冒泡排序bubble sort  轻的上浮,重的下沉.两个相邻位置比较,如果前面元素比后面的元素大就换位置 选择排序 select sort 用一个索引上的元素,依次和其他位置上的元素比较,小的放前面 ...

  8. ofbiz16.11.04(环境搭建)

    ofbiz16.11.04(环境搭建) 版本说明: ofbiz 16.11.04 下载地址:http://ofbiz.apache.org/download.html gradle 4.9 下载地址: ...

  9. spark复习笔记(2)

    之前工作的时候经常用,隔了段时间,现在学校要用学的东西也忘了,翻翻书谢谢博客吧. 1.什么是spark? Spark是一种快速.通用.可扩展的大数据分析引擎,2009年诞生于加州大学伯克利分校AMPL ...

  10. k3 cloud单据体首行过滤功能

    #实现单据体首行过滤  clr.AddReference('System') clr.AddReference('Kingdee.BOS.Core') from Kingdee.BOS.Core.Dy ...