springboot实现路由(direct)模式

发送端

路由模式:在绑定交换机与队列时,多了一个路由key(routingKey)

消息生产者

package com.zhang.rabbitmq.springbootrabbitmqproducer.service;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
* 路由模式:消息生产者
*/
@Component
public class OrederDirectService {

   @Autowired
   private RabbitTemplate rabbitTemplate;

   public void directMorikOrder(String userId, String producerId, int number){
       String orderId = UUID.randomUUID().toString();
       String order_id = UUID.randomUUID().toString();
       String exchangeName = "direct_order_exchange";
       rabbitTemplate.convertAndSend(exchangeName,"sms",orderId);
       rabbitTemplate.convertAndSend(exchangeName,"email",order_id);
  }
}

交换机与队列绑定

package com.zhang.rabbitmq.springbootrabbitmqproducer.config;

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
public class DirectConfigurationProducer {
   // 1:声明交换机与类型
   @Bean
   public DirectExchange directExchange(){
       return new DirectExchange("direct_order_exchange",true,false);
  }
   // 2:声明队列 sms.direct.queue email.direct.queue duanxin.direct.queue
   @Bean
   public Queue smsDirectQueue(){
       return new Queue("sms.direct.queue",true);
  }
   @Bean
   public Queue emailDirectQueue(){
       return new Queue("email.direct.queue", true);
  }
   @Bean
   public Queue duanxinDirectQueue(){
       return new Queue("duanxin.direct.queue",true);
  }
   // 3:绑定交换机与队列
   @Bean
   public Binding smsDirectBinding(){
       return BindingBuilder.bind(smsDirectQueue()).to(directExchange()).with("sms");
  }
   @Bean
   public Binding emailDirectBinding(){
       return BindingBuilder.bind(emailDirectQueue()).to(directExchange()).with("email");
  }
   @Bean
   public Binding duanxinDirectBinding(){
       return BindingBuilder.bind(duanxinDirectQueue()).to(directExchange()).with("duanxin");
  }
}

消息消费者

package com.zhang.rabbitmq.springbootrabbitmqconsumer.service.direct;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
* 路由(dirent)模式:消息消费者
*/
@Component
@RabbitListener(queues = "duanxin.direct.queue")
public class DuanXinDirectConsumer {

   @RabbitHandler
   public void reviceDirect(String message) {
       System.out.println("direct duanxin--接收到的订单信息是-->" + message);
  }
}
package com.zhang.rabbitmq.springbootrabbitmqconsumer.service.direct;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
* 路由(direct)模式:消息接收者
*/
@Component
@RabbitListener(queues = {"email.direct.queue"})
public class EmailDirectConsumer {

   @RabbitHandler
   public void revierDirect(String message){
       System.out.println("direct email--接收到的订单信息是-->" + message);
  }
}
package com.zhang.rabbitmq.springbootrabbitmqconsumer.service.direct;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
* 路由(direct)模式
*/
@Component
@RabbitListener(queues = {"sms.direct.queue"})
public class SMSDirectConsumer {

   @RabbitHandler
   public void reviceDirect(String message){
       System.out.println("direct sms--接收到的订单信息是-->" + message);
  }
}

 

上一篇:RabbitMQ - 基础介绍


下一篇:RabbitMQ 入门之基础概念