Fanout交换器-编写消费者

我们已经把环境搭建好了,我们就来编写Provider和Consumer,我们首先 来编写Consumer,然后回到我们的代码当中,

我们看一下配置文件,在这个配置文件当中呢,配置了交换器的名称

mq.config.exchange=order.fanout
mq.config.queue.sms=order.sms
mq.config.queue.push=order.push

配置了短信队列名称,短信平台服务队列名称,这个是push服务队列名称,我们把它更新到自己的服务平台当中,

那么这两个名称大家应该知道表示什么了,是我们看到的这两个Queue的名称,我们打算给这两个Queue取这两个名字,

然后交换器起的名字,现在我们-直接拷贝direct项目下的,还是日志记录的Receiver,那我们是不是要把这两个Receiver

给改一下,首先名称我们不能叫InfoReceiver,比如我们先去做短信服务,SmsReceiver,然后ErrorReceiver,就是Push服务的,

PushReceiver,首先名字改好以后呢,接下来我们再看,在@RabbitListener我们该怎么没去做呢,首先第一个value这一块,绑定了

队列的名称,现在还是info这里是不是得改了,那这里绑谁呢,看我们的配置文件,这里是不是要绑sms所对应的value,因为我们

给队列起名叫sms吗,所以这块是sms,这是要改动的第一个地方,第二个地方是exchange,这个值不用动吧,这个key值不就是他吗,

所以这个名字就不用动,我们已经改好了

mq.config.exchange=order.fanout

关键是type这里,type我们这里用的还是direct,得把它改成fanout,然后再往后看,这里还有一个key的属性,这个key表示的是什么,

我们之前也说过,这个key就是标示路由键,其实我们通过写前两个案例,direct和topic的,应该也能体会到路由键的特点,其实路由键

的特点就是告诉交换器,现在我要将哪个信息交换到哪个队列当中,那我们现在用的fanout,它是广播模式,所有的队列都要有,既然所有

队列都要有了,还有区分这个概念吗,就没有了,没有区分这个概念,那么路由键就不需要存在了,是的,这块大家需要注意,在我们的fanout

交换器当中,不需要路由键的,如果你要给他加路由键,不是广播模式了,那就是根据你特定的路由键,将信息发送到指定的队列当中了,

就是特定的队列当中了,并不是将消息传递到所有的消息队列当中了,所以一定要注意,我们一定要把路由键给删掉,

@RabbitListener(
	bindings=@QueueBinding(
			value=@Queue(value="${mq.config.queue.sms}",autoDelete="true"),
			exchange=@Exchange(value="${mq.config.exchange}",type=ExchangeTypes.FANOUT)
	)
		)

这样一个sms的服务就修改完了,然后我们在这里再改一改这个名字,

@RabbitHandler
public void process(String msg){
	System.out.println("Sms........receiver: "+msg);
}

然后PushReceiver,这个也是同样的道理,首先需要把消息队列的名称给改一下,这个叫push

value="${mq.config.queue.push}"

应该是叫push,然后这儿,type=ExchangeTypes.FANOUT,这个应该是fanout,然后他也是没有key,没有路由键,有路由键的反而

收不到消息的,我们现在把这个代码粘到笔记当中

@RabbitListener(
	bindings=@QueueBinding(
			value=@Queue(value="${mq.config.queue.push}",autoDelete="true"),
			exchange=@Exchange(value="${mq.config.exchange}",type=ExchangeTypes.FANOUT)
	)
)

这个是smsReceiver,然后接下来是PushReceiver,这样我们的Consumer就编写完了,我们建立了两个模拟服务的类,

一个是SmsReceiver,一个是PushReceiver,然后把它标记一下,在这里,这一块${mq.config.queue.sms}是需要改变的,

第二个就是这个type=ExchangeTypes.FANOUT,交换器的类型,这儿也是,value="${mq.config.queue.push}",这块需要

改动一下,然后还有交换器的类型type=ExchangeTypes.FANOUT
<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>com.learn</groupId>
  <artifactId>rabbitmq-fanout-consumer</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.12.RELEASE</version>
		<relativePath/> 
	</parent>
	
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
		<thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>
	</properties>
	
	<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>
	
	<!-- 这个插件,可以将应用打包成一个可执行的jar包 -->
	<build>
	    <plugins>
	        <plugin>
	            <groupId>org.springframework.boot</groupId>
	            <artifactId>spring-boot-maven-plugin</artifactId>
	        </plugin>
	    </plugins>
	</build>
  
  
</project>
spring.application.name=rabbitmq-fanout-consumer

spring.rabbitmq.host=59.110.158.145
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

mq.config.exchange=order.fanout
mq.config.queue.sms=order.sms
mq.config.queue.push=order.push
package com.learn;

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;

/**
 * 消息接收者
 * @author Administrator
 * @RabbitListener bindings:绑定队列
 * @QueueBinding  value:绑定队列的名称
 *                exchange:配置交换器
 *                key:路由键
 * 
 * @Queue value:配置队列名称
 *        autoDelete:是否是一个可删除的临时队列
 * 
 * @Exchange value:为交换器起个名称
 *           type:指定具体的交换器类型
 */
@Component
@RabbitListener(
			bindings=@QueueBinding(
					value=@Queue(value="${mq.config.queue.sms}",autoDelete="true"),
					exchange=@Exchange(value="${mq.config.exchange}",type=ExchangeTypes.FANOUT)
			)
		)
public class SmsReceiver {

	/**
	 * 接收消息的方法。采用消息队列监听机制
	 * @param msg
	 */
	@RabbitHandler
	public void process(String msg){
		System.out.println("Sms........receiver: "+msg);
	}
}
package com.learn;

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;

/**
 * 消息接收者
 * @author Administrator
 * @RabbitListener bindings:绑定队列
 * @QueueBinding  value:绑定队列的名称
 *                exchange:配置交换器
 * 
 * @Queue value:配置队列名称
 *        autoDelete:是否是一个可删除的临时队列
 * 
 * @Exchange value:为交换器起个名称
 *           type:指定具体的交换器类型
 */
@Component
@RabbitListener(
			bindings=@QueueBinding(
					value=@Queue(value="${mq.config.queue.push}",autoDelete="true"),
					exchange=@Exchange(value="${mq.config.exchange}",type=ExchangeTypes.FANOUT)
			)
		)
public class PushReceiver {

	/**
	 * 接收消息的方法。采用消息队列监听机制
	 * @param msg
	 */
	@RabbitHandler
	public void process(String msg){
            System.out.println("Push..........receiver: "+msg);
	}
}
package com.learn;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class RabbitFanoutConsumerApplication {

	public static void main(String[] args) {
		SpringApplication.run(RabbitFanoutConsumerApplication.class, args);
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值