RabbitMQ入门(五)RabbitMQ交换器之Topic

前言

      本章讲解RabbitMQ的交换器之Topic

方法

1.概念

topic(主题,规则匹配),该类型交换器主要根据路由键进行模糊匹配

需求:在我们的普通订单和VIP订单的业务逻辑中,会产生相关的日志。我们知道,日志是分级别的,这里我们仅仅采集INFO、ERROR级别的日志。如果按照Direct交换器的处理规则,我们需要创建四个队列来处理,即普通订单的INFO日志、普通订单的ERROR日志、VIP订单的INFO日志、VIP订单的ERROR日志,这里我们认为没有必要,完全可以分为两类,即INFO日志和ERROR日志的队列分别的进行处理。由此,我们将使用模糊匹配的规则,即Topic交换器

2.环境搭建

1)创建Provider和Consumer工程

RabbitMQ入门(五)RabbitMQ交换器之Topic_第1张图片

2)修改pom文件


	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.1.7.RELEASE
	
	cn.edu.ccut
	rabbitmq-topic-provider
	0.0.1-SNAPSHOT
	
	
		
		1.8
		
		3.1.0
	
	
	
		
		
			org.springframework.boot
			spring-boot-starter-web
		
		
		
		    org.springframework.boot
		    spring-boot-starter-amqp
		
		
		
			org.springframework.boot
			spring-boot-devtools
		
		
		
			org.springframework.boot
			spring-boot-starter-test
		
	
	
	 
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    cn.edu.ccut.App
                
            
        
    
    

3)修改application.properties

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=log.topic

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=log.topic

mq.config.queue.info=log.info

mq.config.queue.error=log.error

2.编写Provider

创建两个Provider类,GeneralProvider和VipProvider

RabbitMQ入门(五)RabbitMQ交换器之Topic_第2张图片

GeneralProvider:

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 GeneralProvider {

	@Autowired
	private AmqpTemplate rabbitTemplate;
	@Value("${mq.config.exchange}")
	private String exchange;
	
	public void sendMsg(String msg){
		//发送普通订单的日志消息
		rabbitTemplate.convertAndSend(exchange, "general.log.info", "general...info..."+msg);
		rabbitTemplate.convertAndSend(exchange, "general.log.error", "general...error..."+msg);
	}
}

VipProvider:

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 VipProvider {

	@Autowired
	private AmqpTemplate rabbitTemplate;
	@Value("${mq.config.exchange}")
	private String exchange;
	
	public void sendMsg(String msg){
		//发送普通订单的日志消息
		rabbitTemplate.convertAndSend(exchange, "vip.log.info", "vip...info..."+msg);
		rabbitTemplate.convertAndSend(exchange, "vip.log.error", "vip...error..."+msg);
	}
}

3.编写Consumer代码

创建两个Consumer类,ErrorConsumer和InfoConsumer来分别处理两个级别的日志

RabbitMQ入门(五)RabbitMQ交换器之Topic_第3张图片

ErrorConsumer:

路由键采用的是模糊匹配的方式:*.log.error

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.error}",autoDelete="true"),
				exchange=@Exchange(name="${mq.config.exchange}",type=ExchangeTypes.TOPIC),
				key="*.log.error"
		)
)
public class ErrorConsumer {

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

InfoConsumer:

路由键采用的是模糊匹配的方式:*.log.info

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.info}",autoDelete="true"),
				exchange=@Exchange(name="${mq.config.exchange}",type=ExchangeTypes.TOPIC),
				key="*.log.info"
		)
)
public class InfoConsumer {

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

4.编写测试类进行测试

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 GeneralProvider generalProvider;
	
	@Autowired
	private VipProvider vipProvider;
	
	@Test
	public void testSendMsg() throws Exception{
		generalProvider.sendMsg("general message !");
		
		vipProvider.sendMsg("vip message !");
	}
}

启动Consumer,然后运行Provider测试代码,我们会在控制台发现如下结果:

这说明,我们的测试完全的成功。

你可能感兴趣的:(RabbitMQ)