美文网首页
RabbitMQ(四) exchange 三种类型 fanout

RabbitMQ(四) exchange 三种类型 fanout

作者: 尼尔君 | 来源:发表于2020-08-20 16:19 被阅读0次

    exchange 三种类型
    fanout: 广播模式 —>不需要配置routerkey 所有队列都可以接收到信息
    topic : 模糊匹配路由key 的队列 才会接收到数据
    direct : 直连 routerkey需要完全匹配的队列才能接受到数据

    fanout广播模式

    Channel channel = connection.createChannel();    
    channel.exchangeDeclare("exchangeName", "fanout"); //direct fanout topic    
    channel.queueDeclare("queueName");    
    //这种模式需要提前将Exchange与Queue进行绑定,一个Exchange可以绑定多个Queue,一个Queue可以同多个Exchange进行绑定。
    channel.queueBind("queueName", "exchangeName", "routingKey");    
        
    channel.queueDeclare("queueName1");    
    channel.queueBind("queueName1", "exchangeName", "routingKey1");    
        
    byte[] messageBodyBytes = "hello world".getBytes();    
    //路由键需要设置为空    
    channel.basicPublish("exchangeName", "", MessageProperties.PERSISTENT_TEXT_PLAIN, messageBodyBytes);   
    

    topic 模式

    Channel channel = connection.createChannel();    
    channel.exchangeDeclare("exchangeName", "topic"); //direct fanout topic    
    channel.queueDeclare("queueName");    
    channel.queueBind("queueName", "exchangeName", "routingKey.*");    
        
    byte[] messageBodyBytes = "hello world".getBytes();    
    channel.basicPublish("exchangeName", "routingKey.one", MessageProperties.PERSISTENT_TEXT_PLAIN, messageBodyBytes); 
    

    Direct模式

    Channel channel = connection.createChannel();    
    channel.exchangeDeclare("exchangeName", "direct"); //direct fanout topic    
    channel.queueDeclare("queueName");    
    channel.queueBind("queueName", "exchangeName", "routingKey");    
        
    byte[] messageBodyBytes = "hello world".getBytes();    
    //需要绑定路由键    
    channel.basicPublish("exchangeName", "routingKey", MessageProperties.PERSISTENT_TEXT_PLAIN, messageBodyBytes);  
    

    相关文章

      网友评论

          本文标题:RabbitMQ(四) exchange 三种类型 fanout

          本文链接:https://www.haomeiwen.com/subject/xdtejktx.html