一、rabbitMq应用场景
1、异步处理:传统的注册成功,注册写入数据库,在发送邮箱,在发送短信,则返回注册成功,利用rabbitmq异步处理,直接写入数据库后返回注册成功,之后再去rabbitmq消费发送邮箱和短信。
2、双11狂欢节,传统做法应用解耦:订单系统访问库存系统,这种做法有缺点,库存系统出现故障,订单会丢失,可以采用rabbitmq,订单系统下单成功,写入rabbitmq返回下单成功,订阅模式,库存系统去rabbitmq消费。
3、流量削峰,秒杀活动中,人流量太大,存入rabbitmq:可以控制人数,订单超过值,直接丢弃,秒杀失败,缓解短时间流量太大压垮应用。
二、rabbitMq代码实例
首先引入maven包,集成springboot在代码配置文件加上:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> # ----- RabbitMq -------- # spring.rabbitmq.virtual-host=/ spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=admin spring.rabbitmq.password=admin
work模式(多对多使用):
@Component public class RabbitQuestConfig { @Bean public Queue create(){ return new Queue("work"); } }
接口里写上:
@RequestMapping("/work") public void work() { String str = "work"; for (int i = 0; i < 100; i++) { rabbitTemplate.convertAndSend("work", str); } }
@Component @Slf4j public class ConsumerRabbitMq { @Resource private RabbitMqMapper rabbitMqMapper; @RabbitListener(queues = "work") public void test(String message) { System.out.println("work消费成功1:" + message); } @RabbitListener(queues = "work") public void test2(String message) { System.out.println("work消费成功2:" + message); } }
Topic Exchage(主题订阅)
topic 是RabbitMQ中最灵活的一种方式,可以根据routing_key自由的绑定不同的队列
@Configuration public class TopicRabbitConfig { final static String message = "topic_message"; final static String messages = "topic_messages"; @Bean public Queue topicMessage() { return new Queue(TopicRabbitConfig.message); } @Bean public Queue topicMessages() { return new Queue(TopicRabbitConfig.messages); } /** * 声明topic交换机 * * @return */ @Bean TopicExchange topicExchange() { return new TopicExchange("topicExchange"); } /** * 绑定queue交换机 * 只能消费message */ @Bean Binding bindExchangeMessage(Queue topicMessage, TopicExchange topicExchange) { return BindingBuilder.bind(topicMessage).to(topicExchange).with("topic.message"); } /** * 绑定queue交换机 * 全部都可以消费,包含message,messages */ @Bean Binding bindExchangeMessages(Queue topicMessages, TopicExchange topicExchange) { return BindingBuilder.bind(topicMessages).to(topicExchange).with("topic.#"); } }@Configuration public class TopicRabbitConfig { final static String message = "topic_message"; final static String messages = "topic_messages"; @Bean public Queue topicMessage() { return new Queue(TopicRabbitConfig.message); } @Bean public Queue topicMessages() { return new Queue(TopicRabbitConfig.messages); } /** * 声明topic交换机 * * @return */ @Bean TopicExchange topicExchange() { return new TopicExchange("topicExchange"); } /** * 绑定queue交换机 * 只能消费message */ @Bean Binding bindExchangeMessage(Queue topicMessage, TopicExchange topicExchange) { return BindingBuilder.bind(topicMessage).to(topicExchange).with("topic.message"); } /** * 绑定queue交换机 * 全部都可以消费,包含message,messages */ @Bean Binding bindExchangeMessages(Queue topicMessages, TopicExchange topicExchange) { return BindingBuilder.bind(topicMessages).to(topicExchange).with("topic.#"); } }
/** * topic */ @RequestMapping("/topic") public void topic() { for (int i = 0; i < 50; i++) { String topicMessage1 = "topicMessage1"; rabbitTemplate.convertAndSend("topicExchange", "topic.message", topicMessage1); String topicMessage2 = "2topicMessage2"; rabbitTemplate.convertAndSend("topicExchange", "topic.messages", topicMessage2); } } //消费者: @RabbitListener(queues = "topic_message") public void topicMessage1(String message) { System.out.println("topicMessage 消费成功只能消费 topicMessage1:" + message); } /** * topic */ @RabbitListener(queues = "topic_messages") public void topicMessage2(String message) { System.out.println("topicMessages 消费 topicMessage1 & topicMessage2:" + message); }
Fanout Exchange(广播模式):
@Configuration public class FanoutRabbitConfig { @Bean public Queue aMessage(){ return new Queue("a_message"); } @Bean public Queue bMessage(){ return new Queue("b_message"); } @Bean FanoutExchange fanoutExchange(){ return new FanoutExchange("fanoutExchange"); } @Bean Binding bindingExchangeA(Queue aMessage,FanoutExchange fanoutExchange){ return BindingBuilder.bind(aMessage).to(fanoutExchange); } @Bean Binding bindingExchangeB(Queue bMessage,FanoutExchange fanoutExchange){ return BindingBuilder.bind(bMessage).to(fanoutExchange); } }
生产者和消费者
/** * fanoutExchange */ @RequestMapping("/fanout") public void fanout() { String fanout = "fanout"; rabbitTemplate.convertAndSend("fanoutExchange", "", fanout); } /** * fanout */ @RabbitListener(queues = {"a_message"}) public void fanoutA(String message) { System.out.println("fanout广播消费:" + message); } @RabbitListener(queues = {"b_message"}) public void fanoutB(String message) { System.out.println("fanout广播消费:" + message); }
(完...)