原创转载请注明出处: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. vim推荐的光标移动配置文件?

    http://roclinux.cn/?p=1466 inoremap jk inoremap ... 参考较好的vim设置文件 : 共享粘贴板: set clipboard+=unnamed 除了映 ...

  2. centos输入正确的账号和密码登陆不进去

    vm下启动centos,输入正确的账号和密码,依然登陆不进去,一直处于这个界面: 暂时的解决方法是:先等待一段时间.重启,然后再输入密码,然后,ctrl+c 不停地ctrl+c,然后就登陆进去了.什么 ...

  3. 阶段1 语言基础+高级_1-3-Java语言高级_1-常用API_1_第3节 Random类_8-Random概述和基本使用

    用来产生随即数字 构造方法有一个是为空的 每次运行数值都不一样

  4. python3 configparser模块

    配置文件如下: import configparser conf = configparser.ConfigParser() print(type(conf)) #conf是类 conf.read(' ...

  5. Prometheus Alertmanager Grafana 监控警报

    Prometheus Alertmanager Grafana 监控警报 #node-exporter, Linux系统信息采集组件 #prometheus , 抓取.储存监控数据,供查询指标 #al ...

  6. [转载]Parsing X.509 Certificates with OpenSSL and C

    Parsing X.509 Certificates with OpenSSL and C Zakir Durumeric | October 13, 2013 While OpenSSL has b ...

  7. Vue+Java实现在页面树形展示文件目录

    getInfo.class /** * @author Sue * @create 2019-07-16 15:06 **/ @RestController public class getInfo ...

  8. bootstrap-select控件全选,全不选,查询功能实现

    先引入先在你的页面引入 bootstrap-select.css 和 bootstrap-select.js <link href="~/Content/plugins/bootstr ...

  9. web 前端1 html5基础

    HTML web sockent 实例 import socket def handle_request(client): buf = client.recv(1024) client.sendall ...

  10. 实验报告2&&第四周课程总结

    实验报告: 写一个名为Rectangle的类表示矩形.其属性包括宽width.高height和颜色color,width和height都是double型的,而color则是String类型的.要求该类 ...