与主题交换一起发布和订阅模型

时间:2017-11-29 11:35:45

标签: architecture rabbitmq message-queue rpc publish-subscribe

我正在使用事件驱动架构的缓存服务器,它将按以下方式工作:

architecture

我希望将Set个操作发送到所有副本(扇出交换(?))和Get一个任意一个(默认交换(?))。

我已阅读Publish&Subscribe模式,并且能够使用fanout exchange使所有服务器响应。我已经阅读了RPC模型,并能够随意进行服务器响应。但我无法将这种方法统一到一个架构中。请帮忙。

Qustions:

  1. 组织MQ实现此行为的最佳方法是什么?
  2. 我应该将两个队列绑定到一个交换中吗?
  3. 我想通过correlationId从服务器响应客户端。我应该重用现有的队列/交换机还是创建新的队列/交换机?

2 个答案:

答案 0 :(得分:1)

在浏览您的问题域后,我了解到 - 在运行时,多个客户端将发送&#34;设置&#34; &#34;获取&#34; < / strong>发送给RabbitMQ的消息以及每个&#34; set&#34; 消息将由当时活动的每个服务器缓存处理。并且&#34; get&#34; 消息需要由任何一个服务器缓存处理,并且响应消息需要发送回发送<的客户端强>&#34;得到&#34; 消息。

如果我错了,请纠正我。

在这种情况下,可以假设客户端会有单独的触发点来生成/发布&#34; get&#34; /&#34; set&#34; 消息。因此,逻辑上&#34; get&#34; 消息制作者和&#34;设置&#34; 消息&#34;出版商将是两个独立的课程/班级。

因此,您对pub / sub和RPC模型的选择看似合乎逻辑。您唯一需要做的就是联合&#34;设置&#34; &#34;获取&#34; 消息处理和服务器缓存,很容易使用两个独立的通道(在同一个连接中),一个用于服务器缓存上的每个 set get 消息。 参考下面附带的代码。我使用了你在问题中提到的相同样本(来自rabbitmq网站)的java代码。一些小的修改,这是非常直接的。在python中做同样的事情并不困难。

现在向你提问 -

  

组织MQ实现此行为的最佳方法是什么?

您选择的pub / sub和RPC模型看起来合乎逻辑。 客户会将&#34;设置&#34; 消息发布到交易所(输入扇出,例如名称&#34; set_ex&#34; )并且每个服务器缓存实例将监听其临时队列(持续到连接为止)将被绑定以交换&#34; set_ex&#34; 。 客户将向交易所发送&#34; get&#34; 消息(类型直接,例如名称&#34; get_ex&#34; )和队列&#34; get_q&#34; 将与其queuename绑定到此交换。每个服务器缓存都将监听此&#34; get_q&#34; 。服务器缓存将结果消息发送到通过&#34; get&#34; 消息传递的临时字符串。客户端收到响应消息后,将关闭连接并删除临时队列。 (注意 - 在下面的示例代码中,我在默认交换中绑定了&#34; get_q&#34;就像rabbitmq网站上的示例一样。但是绑定&#34; get_q&#并不困难34;单独交换(直接输入)以获得更好的可管理性。)

  

我应该将两个队列绑定到一个交换中吗?

我认为这不是一个正确的选择,因为对于pub / sub场景,您将明确需要Fanout交换,并且发送到扇出交换的每条消息都会复制到绑定到交换机的每个队列。我们不希望将消息推送到所有服务器缓存。

  

我想通过correlationId从Server响应客户端。   我应该重用现有的队列/交换机还是创建新的队列/交换机?

您需要做的就是将响应消息从服务器发送到 tempQueueName ,该消息与原始&#34; get&#34; 消息一起传递,如正在使用rabbitmq提供的样本。

发布的客户端代码&#34; set&#34;消息。

public class Client {

    private static final String EXCHANGE_NAME_SET = "set_ex";

    public static void main(String[] args) throws IOException, TimeoutException {

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare(EXCHANGE_NAME_SET, BuiltinExchangeType.FANOUT);

        String message = getMessage(args);

        channel.basicPublish(EXCHANGE_NAME_SET, "", null, message.getBytes("UTF-8"));
        System.out.println("Sent '" + message + "'");


        channel.close();
        connection.close();

    }

    private static String getMessage(String[] strings) {
        if (strings.length < 1)
            return "info: Hello World!";
        return joinStrings(strings, " ");
    }

    private static String joinStrings(String[] strings, String delimiter) {
        int length = strings.length;
        if (length == 0)
            return "";
        StringBuilder words = new StringBuilder(strings[0]);
        for (int i = 1; i < length; i++) {
            words.append(delimiter).append(strings[i]);
        }
        return words.toString();
    }


}

用于制作&#34; get&#34;的客户代码消息并接收回复消息。

public class RPCClient {

    private static final String EXCHANGE_NAME_GET = "get_ex";
    private Connection connection;
      private Channel channel;
      private String requestQueueName = "get_q";
      private String replyQueueName;

    public RPCClient() throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");

        connection = factory.newConnection();
        channel = connection.createChannel();

        replyQueueName = channel.queueDeclare().getQueue();
      }

    public String call(String message) throws IOException, InterruptedException {
        String corrId = UUID.randomUUID().toString();

        AMQP.BasicProperties props = new AMQP.BasicProperties
                .Builder()
                .correlationId(corrId)
                .replyTo(replyQueueName)
                .build();

        //channel.basicPublish("", requestQueueName, props, message.getBytes("UTF-8"));
        channel.basicPublish("", requestQueueName, props, message.getBytes("UTF-8"));

        final BlockingQueue<String> response = new ArrayBlockingQueue<String>(1);

        channel.basicConsume(replyQueueName, true, new DefaultConsumer(channel) {
          @Override
          public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
            if (properties.getCorrelationId().equals(corrId)) {
              response.offer(new String(body, "UTF-8"));
            }
          }
        });

        return response.take();
      }

     public void close() throws IOException {
            connection.close();
          }


    public static void main(String[] args) throws IOException, TimeoutException {

        RPCClient rpcClient = null;
        String response = null;
        try {
            rpcClient = new RPCClient();

          System.out.println(" sending get message");
          response = rpcClient.call("30");
          System.out.println(" Got '" + response + "'");
        }
        catch  (IOException | TimeoutException | InterruptedException e) {
          e.printStackTrace();
        }
        finally {
          if (rpcClient!= null) {
            try {
                rpcClient.close();
            }
            catch (IOException _ignore) {}
          }
        }

    }


}

订阅&#34;设置&#34;的服务器代码消息和消费&#34;得到&#34;消息。

public class ServerCache1 {

    private static final String EXCHANGE_NAME_SET = "set_ex";
    private static final String EXCHANGE_NAME_GET = "get_ex";
    private static final String RPC_GET_QUEUE_NAME = "get_q";
    private static final String s = UUID.randomUUID().toString();

    public static void main(String[] args) throws IOException, TimeoutException {

        System.out.println("Server Id " + s);

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        Connection connection = factory.newConnection();

        // set server to receive and process set messages
        Channel channelSet = connection.createChannel();
        channelSet.exchangeDeclare(EXCHANGE_NAME_SET, BuiltinExchangeType.FANOUT);

        String queueName = channelSet.queueDeclare().getQueue();
        channelSet.queueBind(queueName, EXCHANGE_NAME_SET, "");

        System.out.println("waiting for set message");

        Consumer consumerSet = new DefaultConsumer(channelSet) {
              @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("Received '" + message + "'");
              }
        };

        channelSet.basicConsume(queueName, true, consumerSet);

        // here onwards following code is to set up Get message processing at Server cache

        Channel channelGet = connection.createChannel();
        channelGet.queueDeclare(RPC_GET_QUEUE_NAME, false, false, false, null);

        channelGet.basicQos(1);

        System.out.println("waiting for get message");

        Consumer consumerGet = new DefaultConsumer(channelGet) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {

                AMQP.BasicProperties replyProps = new AMQP.BasicProperties
                        .Builder()
                        .correlationId(properties.getCorrelationId())
                        .build();

                System.out.println("received get message");
                String response = "get response from server " + s;

                channelGet.basicPublish( "", properties.getReplyTo(), replyProps, response.getBytes("UTF-8"));
                channelGet.basicAck(envelope.getDeliveryTag(), false);
                // RabbitMq consumer worker thread notifies the RPC server owner thread 
                synchronized(this) {
                    this.notify();
                }
            }
        };

        channelGet.basicConsume(RPC_GET_QUEUE_NAME, false, consumerGet);
     // Wait and be prepared to consume the message from RPC client.
        while (true) {
            synchronized(consumerGet) {
                try {
                    consumerGet.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();            
                }
            }
        }

    }

}

希望这有帮助。

答案 1 :(得分:0)

您需要topic队列进行设置操作。这样,N个客户端就可以接收/响应消息。