Java秒杀实战(十一) RabbitMQ四种常见应用模式

先把数据放到路由上。

【Direct 模式】

添加配置类

package com.wings.seckill.config;
 
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
@Configuration
public class MQConfig {
 
	public static final String QUEUE = "queue";
 
	@Bean
	public Queue queue(){
		return new Queue(QUEUE, true);
	}
 
}

发送者

package com.wings.seckill.rabbitmq;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import com.wings.seckill.config.MQConfig;
import com.wings.seckill.redis.RedisService;
 
@Service
public class MQSender {
 
	private Logger logger = LoggerFactory.getLogger(MQSender.class);
 
	@Autowired
	private RedisService redisService;
	
	@Autowired
	private AmqpTemplate amqpTemplate;
	
	/**
	 * Direct 模式交换机
	 * @param obj
	 */
	public void send(Object obj){
		String msg = redisService.beanToString(obj);
		logger.info("sender send:" + msg);
		amqpTemplate.convertAndSend(MQConfig.QUEUE, msg);
	}
}

接收者

package com.wings.seckill.rabbitmq;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;
 
import com.wings.seckill.config.MQConfig;
 
@Service
public class MQReceiver {
 
	private Logger logger = LoggerFactory.getLogger(MQReceiver.class);
	
	@RabbitListener(queues = MQConfig.QUEUE)
	public void receive(String msg){
		logger.info("receive:" + msg);
	}
 
}

DemoController添加以下测试方法

	@RequestMapping("/mq")
	@ResponseBody
	public Result<Boolean> mq() {
		mqSender.send("秒杀商品成功!");
		return Result.success(true);
	}

结果如下

com.imooc.miaosha.rabbitmq.MQSender     : sender send:秒杀商品成功

com.imooc.miaosha.rabbitmq.MQSender     : receive:秒杀商品成功

【topic模式】

配置类添加以下配置方法

 
	public static final String TOPIC_QUEUE1 = "topic.queue1";
	public static final String TOPIC_QUEUE2 = "topic.queue2";
	public static final String TOPIC_EXCHANGE = "topicExchage";;
	
 
	@Bean
	public Queue topQueue1(){
		return new Queue(TOPIC_QUEUE1, true);
	}
	
	@Bean
	public Queue topQueue2(){
		return new Queue(TOPIC_QUEUE2, true);
	}
	
	@Bean
	public TopicExchange topicExchange(){
		return new TopicExchange(TOPIC_EXCHANGE);
	}
	
	@Bean
	public Binding topicBind1(){
		return BindingBuilder.bind(topQueue1()).to(topicExchange()).with("topic.key1");
	}
	
	@Bean
	public Binding topicBind2(){
		return BindingBuilder.bind(topQueue2()).to(topicExchange()).with("topic.#");
	}
    //如果发送方,发的是key1的话,这里不仅会绑定到Queue1的队列中,也会绑定到Queue2的队列中.
    //如果发送方,发的是key2的话,这里仅会绑定到Queue2的队列中.

发送者添加以下方法

	public void sendTopic(Object message) {
		String msg = redisService.beanToString(message);
		logger.info("send topic message:" + msg);
		amqpTemplate.convertAndSend(MQConfig.TOPIC_EXCHANGE, "topic.key1", msg + "1");
		amqpTemplate.convertAndSend(MQConfig.TOPIC_EXCHANGE, "topic.key2", msg + "2");
	}

接受者添加以下方法

	@RabbitListener(queues = MQConfig.TOPIC_QUEUE1)
	public void receiveTopic1(String msg){
		logger.info("receiveTopic1:" + msg);
	}
	
	@RabbitListener(queues = MQConfig.TOPIC_QUEUE2)
	public void receiveTopic2(String msg){
		logger.info("receiveTopic2:" + msg);
	}

结果如下

send topic message:秒杀商品成功
receiveTopic1:秒杀商品成功 1
receiveTopic2:秒杀商品成功 1
receiveTopic2:秒杀商品成功 2

【Fanout模式】

配置类添加以下配置方法

	@Bean
	public FanoutExchange fanoutExchange(){
		return new FanoutExchange(FANOUT_EXCHANGE);
	}
	
	@Bean
	public Binding fanoutBind1(){
		return BindingBuilder.bind(topQueue1()).to(fanoutExchange());
	}
	
	@Bean
	public Binding fanoutBind2(){
		return BindingBuilder.bind(topQueue2()).to(fanoutExchange());
	}
//广播模式,会将一个条信息关联到两个queue上

发送者添加以下方法

	public void sendFanout(Object message) {
		String msg = redisService.beanToString(message);
		logger.info("send fanout message:" + msg);
		amqpTemplate.convertAndSend(MQConfig.FANOUT_EXCHANGE, "", msg);
	}

send fanout message:秒杀商品成功
receiveTopic1:秒杀商品成功 
receiveTopic2:秒杀商品成功 

【headers模式】

添加配置类

    public static final String HEADERS_EXCHANGE = "headersExchage";
/**
     * Header模式 交换机Exchange
     * */
    @Bean
    public HeadersExchange headersExchage(){
        return new HeadersExchange(HEADERS_EXCHANGE);
    }
    @Bean
    public Queue headerQueue1() {
        return new Queue(HEADER_QUEUE, true);
    }
    @Bean
    public Binding headerBinding() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("header1", "value1");
        map.put("header2", "value2");
        return BindingBuilder.bind(headerQueue1()).to(headersExchage()).whereAll(map).match();
    }

发送者添加以下方法

	public void sendHeaders(Object message) {
		String msg = redisService.beanToString(message);
		logger.info("send sendHeaders message:" + msg);
		
		MessageProperties props = new MessageProperties();
		props.setHeader("header1", "value1");
		props.setHeader("header2", "value2");
		Message obj = new Message(msg.getBytes(), props);
		amqpTemplate.convertAndSend(MQConfig.HEADERS_EXCHANGE, "", obj);
	}
//发送方必须和配置里面传递的数据保持一致

接收者添加以下方法

	@RabbitListener(queues = MQConfig.HEADERS_QUEUE)
	public void receiveHeaders(byte[] msg){
		logger.info("receiveHeaders:" + new String(msg));
	}

send sendHeaders message:秒杀商品成功
receiveHeaders::秒杀商品成功 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值