RabbitMQ最常用的三大模式是什么

发布时间:2021-08-19 13:59:56 作者:小新
来源:亿速云 阅读:146

这篇文章主要介绍了RabbitMQ最常用的三大模式是什么,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

Direct 模式

RabbitMQ最常用的三大模式是什么

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class DirectProducer {
  public static void main(String[] args) throws Exception {
    //1. 创建一个 ConnectionFactory 并进行设置
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    factory.setVirtualHost("/");
    factory.setUsername("guest");
    factory.setPassword("guest");

    //2. 通过连接工厂来创建连接
    Connection connection = factory.newConnection();

    //3. 通过 Connection 来创建 Channel
    Channel channel = connection.createChannel();

    //4. 声明
    String exchangeName = "test_direct_exchange";
    String routingKey = "item.direct";

    //5. 发送
    String msg = "this is direct msg";
    channel.basicPublish(exchangeName, routingKey, null, msg.getBytes());
    System.out.println("Send message : " + msg);

    //6. 关闭连接
    channel.close();
    connection.close();
  }
}
import com.rabbitmq.client.*;
import java.io.IOException;

public class DirectConsumer {

  public static void main(String[] args) throws Exception {
    //1. 创建一个 ConnectionFactory 并进行设置
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    factory.setVirtualHost("/");
    factory.setUsername("guest");
    factory.setPassword("guest");
    factory.setAutomaticRecoveryEnabled(true);
    factory.setNetworkRecoveryInterval(3000);
   
    //2. 通过连接工厂来创建连接
    Connection connection = factory.newConnection();

    //3. 通过 Connection 来创建 Channel
    Channel channel = connection.createChannel();

    //4. 声明
    String exchangeName = "test_direct_exchange";
    String queueName = "test_direct_queue";
    String routingKey = "item.direct";
    channel.exchangeDeclare(exchangeName, "direct", true, false, null);
    channel.queueDeclare(queueName, false, false, false, null);

    //一般不用代码绑定,在管理界面手动绑定
    channel.queueBind(queueName, exchangeName, routingKey);

    //5. 创建消费者并接收消息
    Consumer consumer = new DefaultConsumer(channel) {
      @Override
      public void handleDelivery(String consumerTag, Envelope envelope,
                    AMQP.BasicProperties properties, byte[] body)
          throws IOException {
        String message = new String(body, "UTF-8");
        System.out.println(" [x] Received '" + message + "'");
      }
    };

    //6. 设置 Channel 消费者绑定队列
    channel.basicConsume(queueName, true, consumer);

  }
}
 Send message : this is direct msg
 
 [x] Received 'this is direct msg'

Topic 模式

可以使用通配符进行模糊匹配

例如

RabbitMQ最常用的三大模式是什么

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class TopicProducer {

  public static void main(String[] args) throws Exception {
    //1. 创建一个 ConnectionFactory 并进行设置
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    factory.setVirtualHost("/");
    factory.setUsername("guest");
    factory.setPassword("guest");

    //2. 通过连接工厂来创建连接
    Connection connection = factory.newConnection();

    //3. 通过 Connection 来创建 Channel
    Channel channel = connection.createChannel();

    //4. 声明
    String exchangeName = "test_topic_exchange";
    String routingKey1 = "item.update";
    String routingKey2 = "item.delete";
    String routingKey3 = "user.add";

    //5. 发送
    String msg = "this is topic msg";
    channel.basicPublish(exchangeName, routingKey1, null, msg.getBytes());
    channel.basicPublish(exchangeName, routingKey2, null, msg.getBytes());
    channel.basicPublish(exchangeName, routingKey3, null, msg.getBytes());
    System.out.println("Send message : " + msg);

    //6. 关闭连接
    channel.close();
    connection.close();
  }
}
import com.rabbitmq.client.*;
import java.io.IOException;

public class TopicConsumer {

  public static void main(String[] args) throws Exception {
    //1. 创建一个 ConnectionFactory 并进行设置
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    factory.setVirtualHost("/");
    factory.setUsername("guest");
    factory.setPassword("guest");
    factory.setAutomaticRecoveryEnabled(true);
    factory.setNetworkRecoveryInterval(3000);

    //2. 通过连接工厂来创建连接
    Connection connection = factory.newConnection();

    //3. 通过 Connection 来创建 Channel
    Channel channel = connection.createChannel();

    //4. 声明
    String exchangeName = "test_topic_exchange";
    String queueName = "test_topic_queue";
    String routingKey = "item.#";
    channel.exchangeDeclare(exchangeName, "topic", true, false, null);
    channel.queueDeclare(queueName, false, false, false, null);

    //一般不用代码绑定,在管理界面手动绑定
    channel.queueBind(queueName, exchangeName, routingKey);

    //5. 创建消费者并接收消息
    Consumer consumer = new DefaultConsumer(channel) {
      @Override
      public void handleDelivery(String consumerTag, Envelope envelope,
                    AMQP.BasicProperties properties, byte[] body)
          throws IOException {
        String message = new String(body, "UTF-8");
        System.out.println(" [x] Received '" + message + "'");
      }
    };
    //6. 设置 Channel 消费者绑定队列
    channel.basicConsume(queueName, true, consumer);

  }
}
Send message : this is topc msg

[x] Received 'this is topc msg'
[x] Received 'this is topc msg'

Fanout 模式

不处理路由键,只需要简单的将队列绑定到交换机上发送到交换机的消息都会被转发到与该交换机绑定的所有队列上。
Fanout交换机转发消息是最快的。

RabbitMQ最常用的三大模式是什么

import com.rabbitmq.client.*;
import java.io.IOException;

public class FanoutConsumer {
  public static void main(String[] args) throws Exception {
    //1. 创建一个 ConnectionFactory 并进行设置
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    factory.setVirtualHost("/");
    factory.setUsername("guest");
    factory.setPassword("guest");
    factory.setAutomaticRecoveryEnabled(true);
    factory.setNetworkRecoveryInterval(3000);

    //2. 通过连接工厂来创建连接
    Connection connection = factory.newConnection();

    //3. 通过 Connection 来创建 Channel
    Channel channel = connection.createChannel();

    //4. 声明
    String exchangeName = "test_fanout_exchange";
    String queueName = "test_fanout_queue";
    String routingKey = "item.#";
    channel.exchangeDeclare(exchangeName, "fanout", true, false, null);
    channel.queueDeclare(queueName, false, false, false, null);

    //一般不用代码绑定,在管理界面手动绑定
    channel.queueBind(queueName, exchangeName, routingKey);

    //5. 创建消费者并接收消息
    Consumer consumer = new DefaultConsumer(channel) {
      @Override
      public void handleDelivery(String consumerTag, Envelope envelope,
                    AMQP.BasicProperties properties, byte[] body)
          throws IOException {
        String message = new String(body, "UTF-8");
        System.out.println(" [x] Received '" + message + "'");
      }
    };

    //6. 设置 Channel 消费者绑定队列
    channel.basicConsume(queueName, true, consumer);
  }
}
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class FanoutProducer {

  public static void main(String[] args) throws Exception {
    //1. 创建一个 ConnectionFactory 并进行设置
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    factory.setVirtualHost("/");
    factory.setUsername("guest");
    factory.setPassword("guest");

    //2. 通过连接工厂来创建连接
    Connection connection = factory.newConnection();

    //3. 通过 Connection 来创建 Channel
    Channel channel = connection.createChannel();

    //4. 声明
    String exchangeName = "test_fanout_exchange";
    String routingKey1 = "item.update";
    String routingKey2 = "";
    String routingKey3 = "ookjkjjkhjhk";//任意routingkey

    //5. 发送
    String msg = "this is fanout msg";
    channel.basicPublish(exchangeName, routingKey1, null, msg.getBytes());
    channel.basicPublish(exchangeName, routingKey2, null, msg.getBytes());
    channel.basicPublish(exchangeName, routingKey3, null, msg.getBytes());
    System.out.println("Send message : " + msg);

    //6. 关闭连接
    channel.close();
    connection.close();
  }
}
Send message : this is fanout msg

[x] Received 'this is fanout msg'
[x] Received 'this is fanout msg'
[x] Received 'this is fanout msg'

感谢你能够认真阅读完这篇文章,希望小编分享的“RabbitMQ最常用的三大模式是什么”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

推荐阅读:
  1. Rabbitmq常用操作
  2. 一文解析 RabbitMQ 最常用的三大模式

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

rabbit mq

上一篇:怎么提高MySQL的Order by语句查询效率

下一篇:服务器常见错误代码有哪些

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》