栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

RabbitMq的work&topic&fanout模式

RabbitMq的work&topic&fanout模式

一、rabbitMq应用场景


1、异步处理:传统的注册成功,注册写入数据库,在发送邮箱,在发送短信,则返回注册成功,利用rabbitmq异步处理,直接写入数据库后返回注册成功,之后再去rabbitmq消费发送邮箱和短信。

2、双11狂欢节,传统做法应用解耦:订单系统访问库存系统,这种做法有缺点,库存系统出现故障,订单会丢失,可以采用rabbitmq,订单系统下单成功,写入rabbitmq返回下单成功,订阅模式,库存系统去rabbitmq消费。

3、流量削峰,秒杀活动中,人流量太大,存入rabbitmq:可以控制人数,订单超过值,直接丢弃,秒杀失败,缓解短时间流量太大压垮应用。

二、rabbitMq代码实例

首先引入maven包,集成springboot在代码配置文件加上:

  
            org.springframework.boot
            spring-boot-starter-amqp
        

# ----- 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);
    }

    
    @Bean
    TopicExchange topicExchange() {
        return new TopicExchange("topicExchange");
    }

    
    @Bean
    Binding bindExchangeMessage(Queue topicMessage, TopicExchange topicExchange) {
        return BindingBuilder.bind(topicMessage).to(topicExchange).with("topic.message");
    }

    
    @Bean
    Binding bindExchangeMessages(Queue topicMessages, TopicExchange topicExchange) {
        return BindingBuilder.bind(topicMessages).to(topicExchange).with("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);
    }

    
    @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);
    }
}

生产者和消费者

 
    @RequestMapping("/fanout")
    public void fanout() {
        String fanout = "fanout";
        rabbitTemplate.convertAndSend("fanoutExchange", "", 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);
    }

(完...)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/663441.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号