目录
在RabbitMQ中,生产者发送信息不会直接将消息投递到队列中,而是将消息投递到交换机中,再由交换机转发到具体的队列中,队列再将消息以推送或者拉取方式给消费进行消费

在交换机诞生了两个概念
1、路由键:

2、绑定键:

3、两者中的关系


如图所示:
2.2主题交换机(Topic Exchange)
2.3扇形交换机(Fanout Exchange) 


DirectQueueConfig:生成队列,交换机,以及路由键,定义三个队列
package com.zj.provider;
import lombok.With;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@SuppressWarnings("all")
public class DirectQueueConfig {
/**
* 生成一个队列
* @return
*/
@Bean
public Queue directQueueA(){
return new Queue("directQueueA",true);
}
@Bean
public Queue directQueueB(){
return new Queue("directQueueB",true);
}
@Bean
public Queue directQueueC(){
return new Queue("directQueueC",true);
}
@Bean
public DirectExchange directExchange(){
return new DirectExchange("directExchange");
}
@Bean
public Binding bindingA(){
return BindingBuilder.bind(directQueueA()).to(directExchange()).with("AA");
}
@Bean
public Binding bindingB(){
return BindingBuilder.bind(directQueueB()).to(directExchange()).with("BB");
}
@Bean
public Binding bindingC(){
return BindingBuilder.bind(directQueueC()).to(directExchange()).with("CC");
}
}
DirectController:其中rabbitTemplate用来发送信息辅助类
package com.zj.provider;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
@RequestMapping("/sendDirect")
@SuppressWarnings("all")
public class DirectController {
@Autowired
private RabbitTemplate rabbitTemplate;
@RequestMapping("/sendDirect")
public String sendDirect(String routerKey) {
rabbitTemplate.convertAndSend("directExchange", routerKey, "Hello world");
return "yes";
}
}
DirectReciverA:再生成连个同样的类但是要注意的是必须要打@RabbitHandler和@RabbitListener(queues = "directQueueA")第一个是对队列处理者,第二个是队列的监听者,监听队列,不加第一个注解,消息将会接收不到
package com.zj.consumer.mq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@SuppressWarnings("all")
@Slf4j
@RabbitListener(queues = "directQueueA")
public class DirectReciverA {
@RabbitHandler
public void process(String message){
log.warn("A接收到了"+message);
}
}
结果运行成功:

TopicQueueConfig:注意:这里面需要特定指定键
注意:必须在绑定键前加一个Topic来区分,必须介以区别,不然将会报错,因为加入了bean对象
package com.zj.provider.MQ;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@SuppressWarnings("all")
public class TopicQueueConfig {
private final static String KEY_A="*.orange.*";
private final static String KEY_B="*.*.rabbit";
private final static String KEY_C="lazy.#";
/**
* 生成一个队列
* @return
*/
@Bean
public Queue topicQueueA(){
return new Queue("topicQueueA",true);
}
@Bean
public Queue topicQueueB(){
return new Queue("topicQueueB",true);
}
@Bean
public Queue topicQueueC(){
return new Queue("topicQueueC",true);
}
@Bean
public TopicExchange topicExchange(){
return new TopicExchange("topicExchange");
}
@Bean
public Binding topicbindingA(){
return BindingBuilder.bind(topicQueueA()).to(topicExchange()).with(KEY_A);
}
@Bean
public Binding topicbindingB(){
return BindingBuilder.bind(topicQueueB()).to(topicExchange()).with(KEY_B);
}
@Bean
public Binding topicbindingC(){
return BindingBuilder.bind(topicQueueC()).to(topicExchange()).with(KEY_C);
}
}
@RequestMapping("/sendTopic")
public String sendTopic(String routerKey) {
rabbitTemplate.convertAndSend("topicExchange", routerKey, "Hello world");
return "yes";
}
package com.zj.consumer.mq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@SuppressWarnings("all")
@Slf4j
@RabbitListener(queues = "topicQueueA")
public class TopicReciverA {
@RabbitHandler
public void process(String message){
log.info("A接收到了"+message);
}
}
注意:需要进行发信息才能在RabbitMQ发现队列

显示出队列:

接收成功:

扇形交换机和其他两个交换机不一样,扇形交换机不用绑定键,因为他会进行广播,同样的在队列与交换机进行绑定时,需要加上不同的名字来进行区分
package com.zj.provider.MQ;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@SuppressWarnings("all")
public class FanoutQueueConfig {
/**
* 生成一个队列
* @return
*/
@Bean
public Queue fanoutQueueA(){
return new Queue("fanoutQueueA",true);
}
@Bean
public Queue fanoutQueueB(){
return new Queue("fanoutQueueB",true);
}
@Bean
public Queue fanoutQueueC(){
return new Queue("fanoutQueueC",true);
}
@Bean
public FanoutExchange fanoutExchange(){
return new FanoutExchange("fanoutExchange");
}
@Bean
public Binding fanoutbindingA(){
return BindingBuilder.bind(fanoutQueueA()).to(fanoutExchange());
}
@Bean
public Binding fanoutbindingB(){
return BindingBuilder.bind(fanoutQueueB()).to(fanoutExchange());
}
@Bean
public Binding fanoutbindingC(){
return BindingBuilder.bind(fanoutQueueC()).to(fanoutExchange());
}
}
没有绑定键,但是要写空值,不然fanoutExchange会被认为是路由键 @RequestMapping("/sendFanout") public String sendFanout() { rabbitTemplate.convertAndSend("fanoutExchange", "null" ,"Hello world"); return "yes"; }
package com.zj.consumer.mq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@SuppressWarnings("all")
@Slf4j
@RabbitListener(queues = "fanoutQueueA")
public class FanoutReciverA {
@RabbitHandler
public void process(String message){
log.info("A接收到了"+message);
}
}
生产者运行效果:

消费者接收到信息
今天的知识就分享到这了,希望能够帮助到你!
我在通过YAML在Perl和Ruby之间交换数据时遇到问题。我有一些看起来像数字:数字的值,例如1:16。Perl的YAML库(Tiny和XS)将其编码为1:16,不带引号。Ruby的YAML库(Psych)不会将其解释为字符串,而是以某种方式变成Fixnum值4560。我不知道如何在任何一方解决这个转换问题。我用例的YAML中的每个值都应该是一个对象或字符串。因此,如果存在这样的选项,我可以告诉PerlYAML库引用所有值。或者有什么方法可以告诉RubyYAML库将所有值解释为字符串?有任何想法吗?从逻辑上讲,改变任何一方的语言都不是一种选择。Perl:useYAML::XSqw
1、按照拓扑连接网络,观察交换机端口指示灯2、连接网线前通过console线连接交换机,观察网线连接后CRT有何种提示信息3、配置PC的IP地址,要求如下:192.168.x.1 192.168.x.2,配置完成后检查PC的IP地址以及MAC地址(图形化和命令行模式)4、测试PC的连通性(ping目的IP地址)5、通过displaymac-address检查交换机的显示信息,得出MACAddress列与Port列的规律远程登录设备--telnet1、PC通过console线配置路由器2、配置路由器选定的接口地址进入接口试图interfacexxxx配置IP地址PC:192.168.x.1路由器
绝对详细的RabbitMQ实践操作手册,看完本系列就够了。一、什么是MQ?1、MQ的概念2、理解消息队列二、MQ的优势和劣势1、优势和作用2、劣势三、MQ的应用场景四、AMQP五、工作原理一、什么是MQ?1、MQ的概念MQ全称MessageQueue(消息队列),是在消息的传输过程中保存消息的容器。多用于系统之间的异步通信。下面用图来理解异步通信,并阐明与同步通信的区别。同步通信:甲乙两人面对面交流,你一句我一句必须同步进行,两人除此之外不做任何事情异步通信:异步通信相当于通过第三方转述对话,可能有消息的延迟,但不需要二人时刻保持联系,消息传给第三方后,两人可以做其他自己想做的事情,当需要获取
简介本文介绍了华为交换机日志的定义、分类以及输出方法,通过配置举例详细说明了两种常用的日志输出方法。什么是日志日志属于Log信息,其范围比较广。按照ITU-T定义,凡是管理对象事件和异常活动都可以以日志形式记录下来。日志具有跟踪用户活动、管理系统安全的功能,同时也能为系统进行诊断和维护提供依据,是操作维护、定位问题的重要手段。日志有哪些分类?日志分为用户日志、运维日志、诊断日志及安全日志。各类日志的定义如表1所示。!!说明:系统记录的诊断日志、运维日志仅用于问题定位,不会记录用户的敏感信息。如何输出日志?打开设备的信息中心功能可实现各种信息的输出。为了实现对各类信息的输出控制,信息中心定义了1
目录前言一、DHCP配置二、三层交换机的定义三、实验配置步骤1.配置VLAN2.配置DHCP(在三层交换机中)3.测试实验前言本文旨在用于自我学习记录。本文以一台三层交换机3560、一台二层交换机2960和两台主机配置DHCP。一、DHCP配置1.DHCP(动态主机配置协议)是一个局域网的网络协议。指的是由服务器控制一段IP地址范围,客户机登录服务器时就可以自动获得服务器分配的IP地址和子网掩码。2.DHCP的IP地址分配机制1)自动分配方式(AutomaticAllocation),DHCP服务器为主机指定一个永久性的IP地址,一旦DHCP客户端第一次成功从DHCP服务器端租用到IP地址后,
如何交换哈希中的键和值?我有以下哈希:{:a=>:one,:b=>:two,:c=>:three}我想转换成:{:one=>:a,:two=>:b,:three=>:c}使用map似乎相当乏味。有更短的解决方案吗? 最佳答案 Ruby有一个Hash辅助方法,让您可以将Hash视为倒置的(本质上,通过让您通过值访问键):{a:1,b:2,c:3}.key(1)=>:a如果你想保留反转哈希,那么Hash#invert应该适用于大多数情况:{a:1,b:2,c:3}.invert=>{1=>:a,2=>:b,3=>:c}但是...如果您有
我在捐赠表格的顶部有一条默认消息,我希望它根据用户悬停或点击的数量动态变化。每个金额以及“€其他”都应该有相应的消息。例如:“用5.00欧元,我们可以做到这一点……”用10.00欧元,我们可以做到这一点……”这些消息应该在悬停时相应地改变,但如果选择了相应的选项,它们也会保持可见。如果用户取消选择之前选择的选项或者没有选择任何选项,则默认消息应该重新出现。我已经尝试了不同的方法但没有成功,我真的很感激能帮助我实现这一目标。FIDDLEHTMLChoosebelowtheamountofyourdonation€05.00€10.00€15.00€20.00JavaScript$('in
我正在尝试交换给定字符串中所有出现的一对子字符串。例如,我可能想将所有出现的“咖啡”替换为“茶”,将所有出现的“茶”替换为“咖啡”。这是我第一个想到的:varnewString=oldString.replace(/coffee/g,"__").replace(/tea/g,"coffee").replace(/__/g,"tea");它大部分时间都有效,但如果我的输入字符串包含子字符串“__”,它将无法正常工作。我正在寻找无论我提供什么输入都有效的东西,所以我想了更多并想出了这个:varpieces=oldString.split("coffee");for(vari=0;i它工作正
华为ensp配置物理电脑连接模拟交换机SSH登录配置虚拟网卡 开始交换机配置system-view[Huawei]sysnameS1[S1]undoinfo-centerenable[S1]interfaceGigabitEthernet0/0/1[S1-GigabitEthernet0/0/1]portlink-typeaccess[S1-GigabitEthernet0/0/1]portdefaultvlan10[S1-GigabitEthernet0/0/1]qui[S1]interfacevlanif10[S1-Vlanif10]ipaddress192.168.10.124[S1]s
我有大约100个,100和100我文档中的标签。我需要在JavaScript中实现以下操作:将背景所有foos更改为红色,所有条形更改为绿色,所有bazes更改为蓝色。将背景所有foos更改为绿色,所有条形更改为蓝色,所有bazes更改为红色。将背景所有foos更改为蓝色,所有条形更改为红色,所有bazes更改为绿色。我将总共调用这些操作大约1000次,因此我想避免使用附加的解决方案标记到每次做手术。有什么比遍历所有更简单、更快或更好的方法吗?带有document.getElementsByTagName('span')的元素,并更改或附加到.className每个元素的DOM属性?