RabbitMq 配置分離 - 職責單一原則
阿新 • • 發佈:2019-01-07
文章目錄
RabbitMq 配置分離 - 職責單一原則
基於單個交換機
基於Topic 型別交換機
1、建立交換機
import org.springframework.amqp.core.TopicExchange; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @author Created by 譚健 on 2018/12/19. 星期三. 14:35. * © All Rights Reserved. * * MQ 交換機中心 * 負責管理交換機 */ @Configuration public class RabbitMqExchange { /** * 交換機配置中心 */ public static class ExchangeCenter{ // A 號交換機 public static final String TOPIC_A = "csxy_topic_a"; } @Bean TopicExchange topicExchange() { return new TopicExchange(ExchangeCenter.TOPIC_A); } }
2、定義佇列
/**
* @author Created by 譚健 on 2018/12/19. 星期三. 14:39.
* © All Rights Reserved.
* <p>
* MQ 佇列中心
* 用來定義佇列
*/
public interface RabbitMqQueue {
String SMS_QUEUE = "SMS";
// 其它佇列
}
3、建立佇列工廠向Spring 註冊佇列Bean
/** * @author Created by 譚健 on 2018/12/19. 星期三. 14:48. * © All Rights Reserved. * * 佇列工廠 * 負責向spring 註冊佇列 */ @Configuration public class RabbitMqQueueFactory implements RabbitMqQueue{ @Bean(SMS) Queue a() { return new Queue(SMS); } }
4、指定路由規則
mport org.springframework.amqp.core.Binding; import org.springframework.amqp.core.BindingBuilder; import org.springframework.amqp.core.Queue; import org.springframework.amqp.core.TopicExchange; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @author Created by 譚健 on 2018/11/28. 星期三. 17:04. * © All Rights Reserved. * <p> * 交換機路由規則 * * 負責向spring 註冊路由規則 */ @Configuration public class RabbitMqRoutingRules implements RabbitMqQueue { // Queue 引數的名稱必須跟 RabbitMqQueue 中定義的佇列名稱的值一致 @Bean Binding bindingSms(Queue sms, TopicExchange topicExchange) { return BindingBuilder.bind(sms).to(topicExchange).with(SMS); } }