RabbitMQ入门(四)RabbitMQ交换器之Direct

前言

      本章讲解RabbitMQ交换器的第一种类型——Direct交换器

方法

1.概念

交换器是RabbitMQ的核心组件,生产者生产的消息必须先通过交换器才能够到达指定的队列,Direct(发布与订阅,完全匹配),他也是RabbitMQ默认的交换器。如果路由键完全匹配的话,消息就投递到相应的队列。

接下来我们讲解Direct交换器的代码实现。

业务需求:我们需要对电商项目中的普通订单和VIP订单做不同的业务处理,此刻将使用Direct交换器

2.环境搭建

1)创建Provider和Consumer工程

2)修改pom文件

<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>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.7.RELEASE</version>
	</parent>
	<groupId>cn.edu.ccut</groupId>
	<artifactId>srabbitmq-direct-provider</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	
	<properties>
		<!-- 设定Java的版本 -->
		<java.version>1.8</java.version>
		<!-- 解决pom.xml首行报错 -->
		<maven-jar-plugin.version>3.1.0</maven-jar-plugin.version>
	</properties>
	
	<dependencies>
		<!-- 配置springBoot的web启动器 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- 配置rabbitmq的启动器 -->
		<dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-starter-amqp</artifactId>
		</dependency>
		<!-- 配置devtools  -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>
		<!-- 配置springBoot的test启动器 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
		</dependency>
	</dependencies>
	
	 <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>cn.edu.ccut.App</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
    
</project>

3)修改application.properties

Consumer:

spring.application.name=springboot-rabbitmq
#rabbitmq config
spring.rabbitmq.host=192.168.1.108
spring.rabbitmq.port=5672
spring.rabbitmq.username=jwang
spring.rabbitmq.password=123456

#exchange config
mq.config.exchange=order.direct

mq.config.queue.general=order.general
mq.config.queue.general.routing.key=order.general.routing.key

mq.config.queue.vip=order.vip
mq.config.queue.vip.routing.key=order.vip.routing.key

Provider:

spring.application.name=springboot-rabbitmq
#rabbitmq config
spring.rabbitmq.host=192.168.1.108
spring.rabbitmq.port=5672
spring.rabbitmq.username=jwang
spring.rabbitmq.password=123456

#exchange config
mq.config.exchange=order.direct

mq.config.queue.general.routing.key=order.general.routing.key

mq.config.queue.vip.routing.key=order.vip.routing.key

3.Consumer代码编写

GeneralConsumer.java

package cn.edu.ccut;

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(name="${mq.config.queue.general}",autoDelete="true"),
				exchange=@Exchange(name="${mq.config.exchange}",type=ExchangeTypes.DIRECT),
				key="${mq.config.queue.general.routing.key}"
		)
)
public class GeneralConsumer {

	@RabbitHandler
	public void receiveMsg(String msg){
		System.out.println("General Message is "+msg);
	}
}

其中,该消费者绑定了指定的队列-交换器-路由键

VipConsumer.java类似如下:

package cn.edu.ccut;

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(name="${mq.config.queue.vip}",autoDelete="true"),
				exchange=@Exchange(name="${mq.config.exchange}",type=ExchangeTypes.DIRECT),
				key="${mq.config.queue.vip.routing.key}"
		)
)
public class VipConsumer {

	@RabbitHandler
	public void receiveMsg(String msg){
		System.out.println("Vip Message is "+msg);
	}
}

4.Provider代码编写

首先编写一下普通订单的Provider:

package cn.edu.ccut;

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;

@Component
public class Provider {

	@Autowired
	private AmqpTemplate rabbitTemplate;
	@Value("${mq.config.exchange}")
	private String exchange;
	@Value("${mq.config.queue.general.routing.key}")
	private String routingKey;
	
	public void sendMsg(String msg){
		rabbitTemplate.convertAndSend(exchange, routingKey, msg);
	}
}

编写测试类:

package cn.edu.ccut;

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=App.class)
public class RabbitMQTest {
	
	@Autowired
	private Provider provider;
	
	@Test
	public void testSendMsg() throws Exception{
		while(true){
			Thread.sleep(1000);
			provider.sendMsg("hello rabbitmq !");
		}
	}
}

5.测试系统

1)首先启动我们的Consumer端

2)运行我们Provider的测试代码,然后观察控制台输出

由此可见,我们成功实现了这个功能。

当然了,Vip的Provider编写类似!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值