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

RMQ Fanout

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.</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..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 #fanout part start
rmq.config.exchange.fanout=order.fanout
rmq.config.queue.sms=order.sms
rmq.config.queue.push=order.push
#fanout part end

Note: fanout模式下不需要设置routing key

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");
}
}

OrderFanoutProducer.java

 package org.fool.rmq.fanout;

 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 OrderFanoutProducer {
@Autowired
private AmqpTemplate rmqTemplate; @Value("${rmq.config.exchange.fanout}")
private String exchange; public void send() {
String message = "Hello " + new Date();
rmqTemplate.convertAndSend(exchange, "", message);
}
}

SmsConsumer.java

 package org.fool.rmq.fanout;

 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.sms}", autoDelete = "true"),
exchange = @Exchange(value = "${rmq.config.exchange.fanout}", type = ExchangeTypes.FANOUT)
))
public class SmsConsumer {
@RabbitHandler
public void handle(String message) {
System.out.println("consume sms: " + message);
}
}

PushConsumer.java

 package org.fool.rmq.fanout;

 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.push}", autoDelete = "true"),
exchange = @Exchange(value = "${rmq.config.exchange.fanout}", type = ExchangeTypes.FANOUT)
))
public class PushConsumer {
@RabbitHandler
public void handle(String message) {
System.out.println("consume push: " + message);
}
}

ApplicationTest.java

 package org.fool.rmq.test;

 import org.fool.rmq.Application;
import org.fool.rmq.direct.LogProducer;
import org.fool.rmq.fanout.OrderFanoutProducer;
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; @Autowired
private OrderFanoutProducer orderFanoutProducer; @Test
public void test() {
producer.send();
} @Test
public void testDirect() {
logProducer.send();
} @Test
public void testTopic() {
userProducer.send();
productProducer.send();
orderProducer.send();
} @Test
public void testFanout() {
orderFanoutProducer.send();
}
}

Console Output

RMQ Management

RMQ Fanout的更多相关文章

  1. RMQ Terminology

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

  2. BZOJ 3489: A simple rmq problem

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

  3. UVA 11235Frequent values(RMQ)

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

  4. 51nod1174(RMQ)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1174 题意:中文题诶- 思路:RMQ模板题 关于RMQ: h ...

  5. 2016 ACM/ICPC Asia Regional Dalian Online 1008 Function 二分+RMQ

    Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submissio ...

  6. Gym 100646 F Tanks a Lot RMQ

    Problem F: Tanks a Lot Imagine you have a car with a very large gas tank - large enough to hold what ...

  7. (RMQ版)LCA注意要点

    inline int lca(int x,int y){ if(x>y) swap(x,y); ]][x]]<h[rmq[log[y-x+]][y-near[y-x+]+]])? rmq[ ...

  8. 洛谷P2412 查单词 [trie树 RMQ]

    题目背景 滚粗了的HansBug在收拾旧英语书,然而他发现了什么奇妙的东西. 题目描述 udp2.T3如果遇到相同的字符串,输出后面的 蒟蒻HansBug在一本英语书里面找到了一个单词表,包含N个单词 ...

  9. POJ3368Frequent values[RMQ 游程编码]

    Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17581   Accepted: 6346 ...

随机推荐

  1. rich-text 副文本组件 text文本组件

    rich-text 副文本组件 要知道我们小程序常用的标签是view 但是我们想使用div   span  h1 i 标签等等,这种带特性的标签,怎么办的,我们就可以使用我们的 rich-text组件 ...

  2. js常用的几种类型检测方式

    类型检测方式 平时数据类型都是清晰明了的,但有些功能函数还是需要用到类型检测,现总结一下几种常见的类型检测方式: typeof instanceof Object.prototype.toString ...

  3. Celery定时任务|计划任务

    适用场景几点几分执行特定的任务 定时任务 配置这个无需多说了和上篇文章一样 任务函数 硬菜来了 添加任务时候的写法 第一种: from celery_task.order_task import or ...

  4. 【SQL SERVER】 搭建AlwaysON高可用组

    项目需要保障数据的高可用,于是可选的方案无非是Oracle集群. 传统的主从+心跳切换访问点以及SQL Server AlwaysOn这类方案.(//经验不多,了解和实践过的方案就这类,轻拍) Ora ...

  5. nw打包vue项目exe更换图标

    web项目用nw打包好了之后发现没办法更换桌面显示图标问题,找了一下发现大多推荐Resource进行最后更换,试了第一次怎么也不管用,电脑重启了一下就行了...... 首先下载安装好了Resource ...

  6. 【ABAP系列】SAP 销售订单的行项目里条件的增强

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP 销售订单的行项目里条件的 ...

  7. 安全运维 - Windows系统应急响应

    挖矿病毒应急 传播方式: 通过社工.钓鱼方式下载和运行了挖矿程序(邮件.IM等) 利用计算机系统远程代码执行漏洞下载.上传和执行挖矿程序. 利用i算计Web或第三方软件漏洞获取计算机权限,然后下载和执 ...

  8. data plugin for vs2019

    Reporting Service projects for VS 2019https://marketplace.visualstudio.com/items?itemName=ProBITools ...

  9. IO流 -字符输入输出流,以及异常处理方法

    字符输入流 java.io.Reader: 字符输入流的顶层抽象父类 共性的成员方法: int read() 读取单个字符,并返回. int read(char[] cbuf) 将字符读入数组. ab ...

  10. API网关spring cloud gateway和负载均衡框架ribbon实战

    通常我们如果有一个服务,会部署到多台服务器上,这些微服务如果都暴露给客户,是非常难以管理的,我们系统需要有一个唯一的出口,API网关是一个服务,是系统的唯一出口.API网关封装了系统内部的微服务,为客 ...