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

RMQ Topic

Project Directory

Maven Dependency

 <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>org.fool.rmq</groupId>
<artifactId>rmq</artifactId>
<version>1.0-SNAPSHOT</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
</parent> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency> <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>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

application.properties

 spring.application.name=rmq
server.port= spring.rabbitmq.host=localhost
spring.rabbitmq.port=
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin #direct part start
rmq.config.exchange=log.direct
rmq.config.queue.info=log.info
rmq.config.queue.info.routing.key=log.info.routing.key
rmq.config.queue.error=log.error
rmq.config.queue.error.routing.key=log.error.routing.key
#direct part end #topic part start
rmq.config.exchange.topic=log.topic
rmq.config.queue.topic.info=log.topic.info
rmq.config.queue.topic.error=log.topic.error
rmq.config.queue.topic.all=log.topic.all rmq.config.queue.user.info.routing.key=user.log.info
rmq.config.queue.user.debug.routing.key=user.log.debug
rmq.config.queue.user.warn.routing.key=user.log.warn
rmq.config.queue.user.error.routing.key=user.log.error rmq.config.queue.product.info.routing.key=product.log.info
rmq.config.queue.product.debug.routing.key=product.log.debug
rmq.config.queue.product.warn.routing.key=product.log.warn
rmq.config.queue.product.error.routing.key=product.log.error rmq.config.queue.order.info.routing.key=order.log.info
rmq.config.queue.order.debug.routing.key=order.log.debug
rmq.config.queue.order.warn.routing.key=order.log.warn
rmq.config.queue.order.error.routing.key=order.log.error
#topic part end

Source Code

Application.java

 package org.fool.rmq;

 import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

RmqConfig.java

 package org.fool.rmq.config;

 import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class RmqConfig { @Bean
public Queue queue() {
return new Queue("hello-rmq");
}
}

UserProducer.java

 package org.fool.rmq.topic;

 import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import java.util.Date; @Component
public class UserProducer {
@Autowired
private AmqpTemplate rmqTemplate; @Value("${rmq.config.exchange.topic}")
private String exchange; @Value("${rmq.config.queue.user.info.routing.key}")
private String infoRoutingKey; @Value("${rmq.config.queue.user.debug.routing.key}")
private String debugRoutingKey; @Value("${rmq.config.queue.user.warn.routing.key}")
private String warnRoutingKey; @Value("${rmq.config.queue.user.error.routing.key}")
private String errorRoutingKey; public void send() {
String message = "Hello User";
rmqTemplate.convertAndSend(exchange, infoRoutingKey, message + " info");
rmqTemplate.convertAndSend(exchange, debugRoutingKey, message + " debug");
rmqTemplate.convertAndSend(exchange, warnRoutingKey, message+ " warn");
rmqTemplate.convertAndSend(exchange, errorRoutingKey, message + " error");
}
}

ProductProducer.java

 package org.fool.rmq.topic;

 import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import java.util.Date; @Component
public class ProductProducer {
@Autowired
private AmqpTemplate rmqTemplate; @Value("${rmq.config.exchange.topic}")
private String exchange; @Value("${rmq.config.queue.product.info.routing.key}")
private String infoRoutingKey; @Value("${rmq.config.queue.product.debug.routing.key}")
private String debugRoutingKey; @Value("${rmq.config.queue.product.warn.routing.key}")
private String warnRoutingKey; @Value("${rmq.config.queue.product.error.routing.key}")
private String errorRoutingKey; public void send() {
String message = "Hello Product";
rmqTemplate.convertAndSend(exchange, infoRoutingKey, message + " info");
rmqTemplate.convertAndSend(exchange, debugRoutingKey, message + " debug");
rmqTemplate.convertAndSend(exchange, warnRoutingKey, message + " warn");
rmqTemplate.convertAndSend(exchange, errorRoutingKey, message + " error");
}
}

OrderProducer.java

 package org.fool.rmq.topic;

 import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import java.util.Date; @Component
public class OrderProducer {
@Autowired
private AmqpTemplate rmqTemplate; @Value("${rmq.config.exchange.topic}")
private String exchange; @Value("${rmq.config.queue.order.info.routing.key}")
private String infoRoutingKey; @Value("${rmq.config.queue.order.debug.routing.key}")
private String debugRoutingKey; @Value("${rmq.config.queue.order.warn.routing.key}")
private String warnRoutingKey; @Value("${rmq.config.queue.order.error.routing.key}")
private String errorRoutingKey; public void send() {
String message = "Hello Order";
rmqTemplate.convertAndSend(exchange, infoRoutingKey, message + " info");
rmqTemplate.convertAndSend(exchange, debugRoutingKey, message + " debug");
rmqTemplate.convertAndSend(exchange, warnRoutingKey, message + " warn");
rmqTemplate.convertAndSend(exchange, errorRoutingKey, message + " error");
}
}

TopicLogInfoConsumer.java

 package org.fool.rmq.topic;

 import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component; @Component
@RabbitListener(bindings = @QueueBinding(
value = @Queue(value = "${rmq.config.queue.topic.info}", autoDelete = "true"),
exchange = @Exchange(value = "${rmq.config.exchange.topic}", type = ExchangeTypes.TOPIC),
key = "*.log.info"
))
public class TopicLogInfoConsumer {
@RabbitHandler
public void handle(String message) {
System.out.println("consume info: " + message);
}
}

TopicLogErrorConsumer.java

 package org.fool.rmq.topic;

 import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component; @Component
@RabbitListener(bindings = @QueueBinding(
value = @Queue(value = "${rmq.config.queue.topic.error}", autoDelete = "true"),
exchange = @Exchange(value = "${rmq.config.exchange.topic}", type = ExchangeTypes.TOPIC),
key = "*.log.error"
))
public class TopicLogErrorConsumer {
@RabbitHandler
public void handle(String message) {
System.out.println("consume error: " + message);
}
}

TopicLogAllConsumer.java

 package org.fool.rmq.topic;

 import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component; @Component
@RabbitListener(bindings = @QueueBinding(
value = @Queue(value = "${rmq.config.queue.topic.all}", autoDelete = "true"),
exchange = @Exchange(value = "${rmq.config.exchange.topic}", type = ExchangeTypes.TOPIC),
key = "*.log.*"
))
public class TopicLogAllConsumer {
@RabbitHandler
public void handle(String message) {
System.out.println("consume all: " + message);
}
}

ApplicationTest.java

 package org.fool.rmq.test;

 import org.fool.rmq.Application;
import org.fool.rmq.direct.LogProducer;
import org.fool.rmq.producer.Producer;
import org.fool.rmq.topic.OrderProducer;
import org.fool.rmq.topic.ProductProducer;
import org.fool.rmq.topic.UserProducer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class ApplicationTest { @Autowired
private Producer producer; @Autowired
private LogProducer logProducer; @Autowired
private UserProducer userProducer; @Autowired
private ProductProducer productProducer; @Autowired
private OrderProducer orderProducer; @Test
public void test() {
producer.send();
} @Test
public void testDirect() {
logProducer.send();
} @Test
public void testTopic() {
userProducer.send();
productProducer.send();
orderProducer.send();
}
}

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. CentOS和Windows互相远程桌面方法

    https://blog.csdn.net/libaineu2004/article/details/49407883

  2. win7不正常开关机,系统恢复选项

    会win7不正常开关机后,会默认进入“系统回复选项”. 无人值守的机器远程会无法连接,默认会进入此界面. 解决方法: 到现场,鼠标.键盘.显示器, 开机后选择正常启动, 在命令行模式输入以下命令bcd ...

  3. TypeScript快速笔记(一)

    刚学习TypeScript,但因为马上要用,主要是寻求先快速上手,而后再求精. 推荐学习网站: 1)https://www.runoob.com/typescript/ts-tutorial.html ...

  4. BeautifulSoup模块学习文档

    一.BeautifulSoup简介 1.BeautifulSoup模块 Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档 ...

  5. html php插入百度地图定位

    CSS样式 1 2 3 4 <style> .iw_poi_title {color:#CC5522;font-size:14px;font-weight:bold;overflow:hi ...

  6. oracle--优化思路

  7. Flash存储模块调试

    Flash存储模块 Flash存储模块之前SPI一直读deviceID有问题原因如下: 用正点原子的例程是可以的,但是转移到自己的工程项目里就不行!!原因是正点原子没有在SPI初始化里     RCC ...

  8. [2019杭电多校第七场][hdu6646]A + B = C(hash)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6646 题意为求a*10x+b*10y=c*10z满足公式的任意一组解x,y,z. 因为c有可能会由a+ ...

  9. 洛谷P1095守望者的逃离题解-伪动态规划/贪心

    链接 题目描述 恶魔猎手尤迪安野心勃勃,他背叛了暗夜精灵,率领深藏在海底的娜迦族企图叛变.守望者在与尤迪安的交锋中遭遇了围杀,被困在一个荒芜的大岛上.为了杀死守望者,尤迪安开始对这个荒岛施咒,这座岛很 ...

  10. java 多态 (知识点不完整)

    1.多态的条件 1.要有继承 2.方法重写 3.父类引用指向子类对象 多态中成员访问 1.   访问成员变量:  编译看左边,运行也看左边 f.num = 10 因为这里是父类的,看是父类Father ...