在 译:2. RabbitMQ 之Work Queues (工做队列) 咱们学习了如何使用工做队列在多个工做人员之间分配耗时的任务。html
可是若是咱们须要在远程计算机上运行一个函数并等待结果呢?嗯,这是一个不一样的故事。此模式一般称为远程过程调用或RPC。java
在本教程中,咱们将使用RabbitMQ构建RPC系统:客户端和可伸缩的RPC服务器。因为咱们没有任何值得分发的耗时任务,咱们将建立一个返回Fibonacci数字的虚拟RPC服务。git
为了说明如何使用RPC服务,咱们将建立一个简单的客户端类。程序员
它将公开一个名为call的方法,该方法发送一个RPC请求并阻塞,直到收到答案为止:github
FibonacciRpcClient fibonacciRpc = new FibonacciRpcClient(); 字符串结果= fibonacciRpc.call(“4”); System.out.println(“fib(4)is” + result);
关于RPC的说明
尽管RPC在计算中是一种很是常见的模式,但它常常受到批评。当程序员不知道函数调用是本地的仍是慢的RPC时,会出现问题。这样的混淆致使系统不可预测,并增长了调试的没必要要的复杂性。错误使用RPC能够致使不可维护的意大利面条代码,而不是简化软件。json
考虑到这一点,请考虑如下建议:安全
- 确保明显哪一个函数调用是本地的,哪一个是远程的。
- 记录您的系统。使组件之间的依赖关系变得清晰。
- 处理错误案例。当RPC服务器长时间停机时,客户端应该如何反应?
若有疑问,请避免使用RPC。若是能够,您应该使用异步管道 - 而不是相似RPC的阻塞,将结果异步推送到下一个计算阶段。服务器
通常来讲,经过RabbitMQ进行RPC很容易。客户端发送请求消息,服务器回复响应消息。为了接收响应,咱们须要发送带有请求的“回调”队列地址。咱们可使用默认队列(在Java客户端中是独占的)。咱们来试试吧:网络
callbackQueueName = channel.queueDeclare().getQueue(); BasicProperties props = new BasicProperties .Builder() .replyTo(callbackQueueName) .build(); channel.basicPublish("", "rpc_queue", props, message.getBytes()); // ... then code to read a response message from the callback_queue ...
Message properties
The AMQP 0-9-1 protocol predefines a set of 14 properties that go with a message. Most of the properties are rarely used, with the exception of the following:并发
- deliveryMode: Marks a message as persistent (with a value of 2) or transient (any other value). You may remember this property from the second tutorial.
- contentType: Used to describe the mime-type of the encoding. For example for the often used JSON encoding it is a good practice to set this property to: application/json.
- replyTo: Commonly used to name a callback queue.
- correlationId: Useful to correlate RPC responses with requests.
咱们须要一个新的导入:
import com.rabbitmq.client.AMQP.BasicProperties;
相关ID
在上面介绍的方法中,咱们建议为每一个RPC请求建立一个回调队列。这是很是低效的,但幸运的是有更好的方法 - 让咱们为每一个客户端建立一个回调队列。
这引起了一个新问题,在该队列中收到响应后,不清楚响应属于哪一个请求。那是在使用correlationId属性的时候 。咱们将为每一个请求将其设置为惟一值。稍后,当咱们在回调队列中收到一条消息时,咱们将查看此属性,并根据该属性,咱们将可以将响应与请求进行匹配。若是咱们看到未知的correlationId值,咱们能够安全地丢弃该消息 - 它不属于咱们的请求。
您可能会问,为何咱们应该忽略回调队列中的未知消息,而不是失败并出现错误?这是因为服务器端可能存在竞争条件。虽然不太可能,但RPC服务器可能会在向咱们发送答案以后,但在发送请求的确认消息以前死亡。若是发生这种状况,从新启动的RPC服务器将再次处理请求。这就是为何在客户端上咱们必须优雅地处理重复的响应,理想状况下RPC应该是幂等的。
概要
咱们的RPC将这样工做:
- 对于RPC请求,客户端发送带有两个属性的消息: replyTo,设置为仅为请求建立的匿名独占队列;以及correlationId,设置为每一个请求的惟一值。
- 请求被发送到rpc_queue队列。
- RPC worker(aka:server)正在等待该队列上的请求。当出现请求时,它会执行该做业,并使用来自replyTo字段的队列将带有结果的消息发送回客户端。
- 客户端等待回复队列上的数据。出现消息时,它会检查correlationId属性。若是它与请求中的值匹配,则返回对应用程序的响应。
把它们放在一块儿
斐波纳契任务:
private static int fib(int n) { if (n == 0) return 0; if (n == 1) return 1; return fib(n-1) + fib(n-2); }咱们宣布咱们的斐波那契函数。它假定只有有效的正整数输入。(不要期望这个适用于大数字,它多是最慢的递归实现)。
服务器代码很是简单:
- 像往常同样,咱们首先创建链接,通道和声明队列。
- 咱们可能但愿运行多个服务器进程。为了在多个服务器上平均分配负载,咱们须要在channel.basicQos中设置 prefetchCount设置。
- 咱们使用basicConsume来访问队列,咱们以对象(DefaultConsumer)的形式提供回调,它将完成工做并发回响应。
咱们的RPC客户端的代码能够在这里找到:RPCClient.java。
客户端代码稍微复杂一些:
- 咱们创建了一个链接和渠道。
- 咱们的调用方法生成实际的RPC请求。
- 在这里,咱们首先生成一个惟一的correlationId 数并保存它 - 咱们 在RpcConsumer中的handleDelivery实现将使用该值来捕获适当的响应。
- 而后,咱们为回复建立一个专用的独占队列并订阅它。
- 接下来,咱们发布请求消息,其中包含两个属性: replyTo和correlationId。
- 在这一点上,咱们能够坐下来等待正确的响应到来。
- 因为咱们的消费者交付处理是在一个单独的线程中进行的,所以咱们须要在响应到来以前暂停主线程。使用BlockingQueue是一种可能的解决方案。这里咱们建立了ArrayBlockingQueue ,容量设置为1,由于咱们只须要等待一个响应。
- 该handleDelivery方法是作一个很简单的工做,对每一位消费响应消息它会检查的correlationID 是咱们要找的人。若是是这样,它会将响应置于BlockingQueue。
- 同时主线程正在等待响应从BlockingQueue获取它。
- 最后,咱们将响应返回给用户。
发出客户请求:
RPCClient fibonacciRpc = new RPCClient(); System.out.println(“[x] Requesting fib(30)”); 字符串响应= fibonacciRpc.call(“30”); System.out.println(“[。] Got'” + response + “'”); fibonacciRpc.close();
RPCClient.java
import java.io.IOException; import java.util.UUID; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.TimeoutException; import com.rabbitmq.client.AMQP; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; import com.rabbitmq.client.DefaultConsumer; import com.rabbitmq.client.Envelope; public class RPCClient { private Connection connection; private Channel channel; private String requestQueueName = "rpc_queue"; public RPCClient() throws IOException, TimeoutException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); connection = factory.newConnection(); channel = connection.createChannel(); } public String call(String message) throws IOException, InterruptedException { final String corrId = UUID.randomUUID().toString(); String replyQueueName = channel.queueDeclare().getQueue(); AMQP.BasicProperties props = new AMQP.BasicProperties .Builder() .correlationId(corrId) .replyTo(replyQueueName) .build(); channel.basicPublish("", requestQueueName, props, message.getBytes("UTF-8")); final BlockingQueue<String> response = new ArrayBlockingQueue<String>(1); String ctag = 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")); } } }); String result = response.take(); channel.basicCancel(ctag); return result; } public void close() throws IOException { connection.close(); } public static void main(String[] argv) { RPCClient fibonacciRpc = null; String response = null; try { fibonacciRpc = new RPCClient(); for (int i = 0; i < 32; i++) { String i_str = Integer.toString(i); System.out.println(" [x] Requesting fib(" + i_str + ")"); response = fibonacciRpc.call(i_str); System.out.println(" [.] Got '" + response + "'"); } } catch (IOException | TimeoutException | InterruptedException e) { e.printStackTrace(); } finally { if (fibonacciRpc!= null) { try { fibonacciRpc.close(); } catch (IOException _ignore) {} } } } }RPCServer.java
import java.io.IOException; import java.util.concurrent.TimeoutException; import com.rabbitmq.client.AMQP; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; import com.rabbitmq.client.Consumer; import com.rabbitmq.client.DefaultConsumer; import com.rabbitmq.client.Envelope; public class RPCServer { private static final String RPC_QUEUE_NAME = "rpc_queue"; private static int fib(int n) { if (n ==0) return 0; if (n == 1) return 1; return fib(n-1) + fib(n-2); } public static void main(String[] argv) { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = null; try { connection = factory.newConnection(); final Channel channel = connection.createChannel(); channel.queueDeclare(RPC_QUEUE_NAME, false, false, false, null); channel.queuePurge(RPC_QUEUE_NAME); channel.basicQos(1); System.out.println(" [x] Awaiting RPC requests"); Consumer consumer = new DefaultConsumer(channel) { @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(); String response = ""; try { String message = new String(body,"UTF-8"); int n = Integer.parseInt(message); System.out.println(" [.] fib(" + message + ")"); response += fib(n); } catch (RuntimeException e){ System.out.println(" [.] " + e.toString()); } finally { channel.basicPublish( "", properties.getReplyTo(), replyProps, response.getBytes("UTF-8")); channel.basicAck(envelope.getDeliveryTag(), false); // RabbitMq consumer worker thread notifies the RPC server owner thread synchronized(this) { this.notify(); } } } }; channel.basicConsume(RPC_QUEUE_NAME, false, consumer); // Wait and be prepared to consume the message from RPC client. while (true) { synchronized(consumer) { try { consumer.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } catch (IOException | TimeoutException e) { e.printStackTrace(); } finally { if (connection != null) try { connection.close(); } catch (IOException _ignore) {} } } }
此处介绍的设计并非RPC服务的惟一可能实现,但它具备一些重要优点:
- 若是RPC服务器太慢,您能够经过运行另外一个服务器来扩展。尝试在新控制台中运行第二个RPCServer。
- 在客户端,RPC只须要发送和接收一条消息。不须要像queueDeclare这样的同步调用 。所以,对于单个RPC请求,RPC客户端只须要一次网络往返。
咱们的代码仍然至关简单,并不试图解决更复杂(但重要)的问题,例如:
- 若是没有运行服务器,客户应该如何反应?
- 客户端是否应该为RPC设置某种超时?
- 若是服务器出现故障并引起异常,是否应将其转发给客户端?
- 在处理以前防止无效的传入消息(例如检查边界,键入)。