rabbitMQ学习笔记(七) RPC 远程过程调用
关于RPC的介绍请参考百度百科里的关于RPC的介绍:http://baike.baidu.com/view/32726.htm#sub32726
现在来看看Rabbitmq中RPC吧!RPC的工作示意图如下:
上图中的C代表客户端,S表示服务器端;Rabbitmq中的RPC流程如下:
1、首先客户端发送一个reply_to和corrention_id的请求,发布到RPC队列中;
2、服务器端处理这个请求,并把处理结果发布到一个回调Queue,此Queue的名称应当与reply_to的名称一致
3、客户端从回调Queue中得到先前correlation_id设定的值的处理结果。如果碰到和先前不一样的corrention_id的值,将会忽略而不是抛出异常。
对于上面所提到的回调Queue中的消费处理使用的是BasicProperties类;而消息
属性在AMQP的协议中规定有14个;而很多大部分我们没有用到。常用的几个属性有:
Message properties
The AMQP protocol predefine a set of properties that go with a message. Most of the properties are rarely used, with the exception of the following: delivery_mode: Marks a message as persistent (with a value of ) or transient (any other value). You may remember this property from the second tutorial.
content_type: 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.
reply_to: Commonly used to name a callback queue.
correlation_id: Useful to correlate RPC responses with requests.
delivery_mode : 标记消息是持久性消息还是瞬态信息。在前面的“Work Queue”中我们已经提到过;
content_type : 用来描述MIME的类型。如把其类型设定为JSON;
reply_to : 用于命名一个回调Queue;
correlation_id : 用于与相关联的请求的RPC响应.
当客户端想要调用服务器的某个方法来完成某项功能时,就可以使用rabbitMQ支持的PRC服务。
其实RPC服务与普通的收发消息的区别不大, RPC的过程其实就是
客户端向服务端定义好的Queue发送消息,其中携带的消息就应该是服务端将要调用的方法的参数 ,并使用Propertis告诉服务端将结果返回到指定的Queue。
示例:
package com.zf.rabbitmq07; import java.io.IOException; import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.ConsumerCancelledException;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.QueueingConsumer.Delivery;
import com.rabbitmq.client.ShutdownSignalException; public class RPCServer { public static final String RPC_QUEUE_NAME = "rpc_queue"; public static String sayHello(String name){
return "hello " + name ;
} public static void main(String[] args) throws IOException, ShutdownSignalException, ConsumerCancelledException, InterruptedException { ConnectionFactory connFac = new ConnectionFactory() ;
connFac.setHost("localhost"); Connection conn = connFac.newConnection() ; Channel channel = conn.createChannel() ; channel.queueDeclare(RPC_QUEUE_NAME, false, false, false, null) ; QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(RPC_QUEUE_NAME, false , consumer) ; while(true){
System.out.println("服务端等待接收消息..");
Delivery deliver = consumer.nextDelivery() ;
System.out.println("服务端成功收到消息..");
BasicProperties props = deliver.getProperties() ; String message = new String(deliver.getBody() , "UTF-8") ; String responseMessage = sayHello(message) ; BasicProperties responseProps = new BasicProperties.Builder()
.correlationId(props.getCorrelationId())
.build() ; //将结果返回到客户端Queue
channel.basicPublish("", props.getReplyTo() , responseProps , responseMessage.getBytes("UTF-8") ) ; //向客户端确认消息
channel.basicAck(deliver.getEnvelope().getDeliveryTag(), false);
System.out.println("服务端返回消息完成..");
} } }
package com.zf.rabbitmq07; import java.io.IOException;
import java.util.UUID; import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.ConsumerCancelledException;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.QueueingConsumer.Delivery;
import com.rabbitmq.client.ShutdownSignalException; public class RPCClient { public static final String RPC_QUEUE_NAME = "rpc_queue"; public static void main(String[] args) throws IOException, ShutdownSignalException, ConsumerCancelledException, InterruptedException { ConnectionFactory connFac = new ConnectionFactory() ;
connFac.setHost("localhost");
Connection conn = connFac.newConnection() ;
Channel channel = conn.createChannel() ; //响应QueueName ,服务端将会把要返回的信息发送到该Queue
String responseQueue = channel.queueDeclare().getQueue() ; String correlationId = UUID.randomUUID().toString() ; BasicProperties props = new BasicProperties.Builder()
.replyTo(responseQueue)
.correlationId(correlationId)
.build(); String message = "is_zhoufeng";
channel.basicPublish( "" , RPC_QUEUE_NAME , props , message.getBytes("UTF-8")); QueueingConsumer consumer = new QueueingConsumer(channel) ; channel.basicConsume( responseQueue , consumer) ; while(true){ Delivery delivery = consumer.nextDelivery() ; if(delivery.getProperties().getCorrelationId().equals(correlationId)){
String result = new String(delivery.getBody()) ;
System.out.println(result);
} }
} }
rabbitMQ学习笔记(七) RPC 远程过程调用的更多相关文章
- RabbitMQ入门教程(八):远程过程调用RPC
原文:RabbitMQ入门教程(八):远程过程调用RPC 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.cs ...
- RabbitMQ学习系列(五): RPC 远程过程调用
前面讲过一些RabbitMQ的安装和用法,也说了说RabbitMQ在一般的业务场景下如何使用.不知道的可以看我前面的博客,http://www.cnblogs.com/zhangweizhong/ca ...
- RPC远程过程调用
什么是RPC: 将一个函数运行在远程计算机上并且等待获取那里的结果,这个称作RPC: (Remote Procedure Call远程过程调用) RPC是一个计算机通信协议. rpc指的是在计算机A上 ...
- (扫盲)RPC远程过程调用
https://blog.csdn.net/mindfloating/article/details/39473807 https://blog.csdn.net/mindfloating/artic ...
- openstack学习笔记一 虚拟机启动过程代码跟踪
openstack学习笔记一 虚拟机启动过程代码跟踪 本文主要通过对虚拟机创建过程的代码跟踪.观察虚拟机启动任务状态的变化,来透彻理解openstack各组件之间的作用过程. 当从horizon界面发 ...
- go微服务框架kratos学习笔记七(kratos warden 负载均衡 balancer)
目录 go微服务框架kratos学习笔记七(kratos warden 负载均衡 balancer) demo demo server demo client 池 dao service p2c ro ...
- 官网英文版学习——RabbitMQ学习笔记(一)认识RabbitMQ
鉴于目前中文的RabbitMQ教程很缺,本博主虽然买了一本rabbitMQ的书,遗憾的是该书的代码用的不是java语言,看起来也有些不爽,且网友们不同人学习所写不同,本博主看的有些地方不太理想,为此本 ...
- (转)Qt Model/View 学习笔记 (七)——Delegate类
Qt Model/View 学习笔记 (七) Delegate 类 概念 与MVC模式不同,model/view结构没有用于与用户交互的完全独立的组件.一般来讲, view负责把数据展示 给用户,也 ...
- Typescript 学习笔记七:泛型
中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...
随机推荐
- 【整合篇】Activiti业务与流程的整合
对于不管是Activtit还是jbpm来说,业务与流程的整合均类似.启动流程是绑定业务.流程与业务的整合放到动态代理中 [java] view plain copy print" style ...
- 【POJ 1222】 EXTENDED LIGHTS OUT
[题目链接] http://poj.org/problem?id=1222 [算法] 列出异或方程组,用高斯消元求解即可 [代码] #include <algorithm> #includ ...
- 【POJ 1830】 开关问题
[题目链接] http://poj.org/problem?id=1830 [算法] 列出异或方程组,用高斯消元求解 [代码] #include <algorithm> #include ...
- javascript设计模式-工厂模式(简单工厂)
接口在工厂模式中起着很重要的作用,如果不对对象进行某种类型检查的以确保其实现了必要的方法,工厂模式的好处也就所剩无几了,举个简单的例子. Interface.js // Constructor. va ...
- getElementById和querySelector区别
1.常见的获取元素的方法有3种,分别是通过元素ID document.getElementById('idName');.通过标签名字document.getElementsByTagName(tag ...
- [XJOI]noip40 T2统计方案
统计方案 小B写了一个程序,随机生成了n个正整数,分别是a[1]..a[n],他取出了其中一些数,并把它们乘起来之后模p,得到了余数c.但是没过多久,小B就忘记他选了哪些数,他想把所有可能的取数方案都 ...
- Hadoop MapReduce编程 API入门系列之wordcount版本5(九)
这篇博客,给大家,体会不一样的版本编程. 代码 package zhouls.bigdata.myMapReduce.wordcount1; import java.io.IOException; i ...
- 图片文字滚动插件jQuery Scrollbox
图片文字滚动插件jQuery Scrollbox附件中提供了五种图片.文字滚动样式,只需调用jquery库和jQuery Scrollbox插件,然后再加一段简单的jquery代码即可使用,兼容性良好 ...
- root密码忘记怎么办?
忘记root密码:按 e进入内核在按e,后面加1 .按b启动 进入命令行输入passwd,设置新的密码后exit退出即可
- 记我安装Caffe的血泪史(1)
Caffe的安装我主要参考的是这篇博客 http://blog.csdn.net/ubunfans/article/details/47724341 按照他的方法我很快就安好了cuda8(为后面安装了 ...