RPC是指远程过程调用,直观说法就是A通过网络调用B的过程方法。

也就是说两台serverA。B,一个应用部署在Aserver上,想要调用Bserver上应用提供的函数/方法,因为不在一个内存空间,不能直接调用。须要通过网络来表达调用的语义和传达调用的数据。

为什么RPC呢?就是无法在一个进程内,甚至一个计算机内通过本地调用的方式完毕的需求,比方比方不同的系统间的通讯,甚至不同的组织间的通讯。因为计算能力须要横向扩展。须要在多台机器组成的集群上部署应用


  1. 首先要解决寻址的问题,也就是说,Aserver上的应用怎么告诉底层的RPC框架。Bserver的IP。以及应用绑定的端口,还有方法的名称,这样才干完毕调用
  2. 方法的參数须要通过底层的网络协议如TCP传递到Bserver,因为网络协议是基于二进制的,内存中的參数的值要序列化成二进制的形式
  3. 在Bserver上完毕寻址后。须要对參数进行反序列化。恢复为内存中的表达方式。然后找到相应的方法进行本地调用,然后得到返回值。
  4. 返回值还要发送回serverA上的应用,也要经过序列化的方式发送,serverA接到后。再反序列化,恢复为内存中的表达方式,交给应用

RpcFramework

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.net.ServerSocket;
import java.net.Socket;
/**
* RpcFramework
*/
public class RpcFramework { /**
* 暴露服务
*
* @param service
* 服务实现
* @param port
* 服务端口
* @throws Exception
*/
public static void export(final Object service, int port) throws Exception {
if (service == null)
throw new IllegalArgumentException("service instance == null");
if (port <= 0 || port > 65535)
throw new IllegalArgumentException("Invalid port " + port);
System.out.println("Export service " + service.getClass().getName() + " on port " + port);
ServerSocket server = new ServerSocket(port);//前面都是验证调用是否符合规则,这里在被调用端开一个服务。以下就是死循环执行。
for (;;) {
try {
final Socket socket = server.accept();
new Thread(new Runnable() {//对每个请求new一个线程,匿名类
@Override
public void run() {
try {
try {
ObjectInputStream input = new ObjectInputStream(socket.getInputStream());
try {
String methodName = input.readUTF();
Class<?>[] parameterTypes = (Class<?>[]) input.readObject();
Object[] arguments = (Object[]) input.readObject();
ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());//接收client传来的方法名、參数类型、參数
try {
Method method = service.getClass().getMethod(methodName, parameterTypes);//在本地生成相应的方法,
Object result = method.invoke(service, arguments);//调用
output.writeObject(result);//返回结果
} catch (Throwable t) {
output.writeObject(t);
} finally {
output.close();
}
} finally {
input.close();
}
} finally {
socket.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
} catch (Exception e) {
e.printStackTrace();
}
}
} /**
* 引用服务
*
* @param <T>
* 接口泛型
* @param interfaceClass
* 接口类型
* @param host
* server主机名
* @param port
* server端口
* @return 远程服务
* @throws Exception
*/
@SuppressWarnings("unchecked")
public static <T> T refer(final Class<T> interfaceClass, final String host, final int port) throws Exception {
if (interfaceClass == null)
throw new IllegalArgumentException("Interface class == null");
if (!interfaceClass.isInterface())
throw new IllegalArgumentException("The " + interfaceClass.getName() + " must be interface class!");
if (host == null || host.length() == 0)
throw new IllegalArgumentException("Host == null!");
if (port <= 0 || port > 65535)
throw new IllegalArgumentException("Invalid port " + port);
System.out.println("Get remote service " + interfaceClass.getName() + " from server " + host + ":" + port);
return (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class<? >[] { interfaceClass },
new InvocationHandler() {//用动态代理的方法进行包装,看起来是在调用一个方法,事实上在内部通过socket通信传到server。并接收执行结果
public Object invoke(Object proxy, Method method, Object[] arguments) throws Throwable {
Socket socket = new Socket(host, port);
try {
ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());
try {
output.writeUTF(method.getName());
output.writeObject(method.getParameterTypes());
output.writeObject(arguments);
ObjectInputStream input = new ObjectInputStream(socket.getInputStream());
try {
Object result = input.readObject();
if (result instanceof Throwable) {
throw (Throwable) result;
}
return result;//返回结果
} finally {
input.close();
}
} finally {
output.close();
}
} finally {
socket.close();
}
}
});
} }

RpcProvider


public class RpcProvider {
public static void main(String[] args) throws Exception {
HelloService service = new HelloServiceImpl();
RpcFramework.export(service, 1234);
}
}

RpcConsumer


public class RpcConsumer {
public static void main(String[] args) throws Exception {
HelloService service = RpcFramework.refer(HelloService.class, "127.0.0.1", 1234);
for (int i = 0; i < Integer.MAX_VALUE; i++) {
String hello = service.hello("World" + i);
System.out.println(hello);
Thread.sleep(1000);
}
}
}

RPC远程调用概念 &amp;&amp; demo实例的更多相关文章

  1. 从0到1:全面理解RPC远程调用

    上一篇关于 WSGI 的硬核长文,不知道有多少同学,能够从头看到尾的,不管你们有没有看得很过瘾,反正我是写得很爽,总有一种将一样知识吃透了的错觉. 今天我又给自己挖坑了,打算将 rpc 远程调用的知识 ...

  2. Openstack Nova 源码分析 — RPC 远程调用过程

    目录 目录 Nova Project Services Project 的程序入口 setuppy Nova中RPC远程过程调用 nova-compute RPC API的实现 novacompute ...

  3. 測试JSON RPC远程调用(JSONclient)

    #include <string> #include <iostream> #include <curl/curl.h> /* 标题:JSonclient Auth ...

  4. 使用Socket&反射&Java流操作进行方法的远程调用(模拟RPC远程调用)

    写在前面 阅读本文首先得具备基本的Socket.反射.Java流操作的基本API使用知识:否则本文你可能看不懂... 服务端的端口监听 进行远程调用,那就必须得有客户端和服务端.服务端负责提供服务,客 ...

  5. rpc远程调用开发

    RPC即远程过程调用,适用于集群管理,集群节点就是RPCServer,而我们发起远程调用的web服务器就是RPCClient.所以是少数rpcClient(可能一个)对多个RPCServer(集群节点 ...

  6. dubbo集成zookeeper rpc远程调用

    注:下面使用dubbo依赖的是zookeeper注册中心,这里没有详细的介绍.在配置之前,请自行准备好zookeeper环境. 后续如果写zookeeper的配置会补放链接 添加Gradle依赖 co ...

  7. RabbitMQ学习之基于spring-rabbitmq的RPC远程调用

    http://blog.csdn.net/zhu_tianwei/article/details/40920985 spring-rabbitmq中实现远程接口调用,主要在com.rabbitmq.s ...

  8. netcore 基于 DispatchProxy 实现一个简单Rpc远程调用

    前言 netcore 发布以来,一直很关注netcore的进程.目前在公司负责的网站也历经波折的全部有.net framework 4.0 全部切换到netcore 2.2 版本中.虽然过程遇到的坑不 ...

  9. 详解RPC远程调用和消息队列MQ的区别

    PC(Remote Procedure Call)远程过程调用,主要解决远程通信间的问题,不需要了解底层网络的通信机制. RPC框架 知名度较高的有Thrift(FB的).dubbo(阿里的). RP ...

随机推荐

  1. 用开源项目FlipImageView实现图片的翻转效果

         开源项目地址:https://github.com/castorflex/FlipImageView 本实例我没做什么改动,就是添加了注释,方便大家阅读.和之前一样,导入library和工程 ...

  2. Eclipse启动时提示Fail to create the Java Virtual Machine的解决方法

    这个错误是Eclipse里面的一个bug,我们通过如下的设置就可以解决它. 打开eclipse安装目录下的eclipse.ini文件: 将其中的256m改为128m,512m改为256m,1024m改 ...

  3. 部署包含水晶报表Crystal Reports 的VS.NET2005应用程序[原创]

    要部署包含水晶报表Crystal Reports 的应用程序,您需要在生成解决方案之前创建一个安装项目,并且向应用程序中添加必要的合并模块. 1.打开 VS.NET2005 编程IDE. 2.在解决方 ...

  4. [转]PHP 真正多线程的使用

    From : http://blog.s135.com/pthreads/ PHP 5.3 以上版本,使用pthreads PHP扩展,可以使PHP真正地支持多线程.多线程在处理重复性的循环任务,能够 ...

  5. HttpContext.Current.Session 和 Session 的区别

    Session(会话)通常指一个动作从开始到结束不间断的一个动作. 例如“打电话”,通常是“1.拿起电话--2.拨对方号码--3.对方截图--4.挂机”.这四个步骤从完成到结束组成了一个基本的Sess ...

  6. C# 数组基础知识

    数组的属性: 数组可以是一维.多维或交错的. 数值数组元素的默认值设置为零,而引用元素的默认值设置为 null. 交错数组是数组的数组,因此,它的元素是引用类型,初始化为 null. 数组的索引从零开 ...

  7. 怎么选择软件许可证,Apache, MIT, BSD, GPL, Mozilla, LGPL

  8. go语言之进阶篇Ticker的使用

    Ticker是一个定时触发的计时器,它会以一个间隔(interval)往channel发送一个事件(当前时间),而channel的接收者可以以固定的时间间隔从channel中读取事件. 1.Ticke ...

  9. [leetcode]Distinct Subsequences @ Python

    原题地址:https://oj.leetcode.com/problems/distinct-subsequences/ 题意: Given a string S and a string T, co ...

  10. CSS渐变字体、镂空字体、input框提示信息颜色、给图片加上内阴影、3/4圆

    1.渐变字体 主要是看:-webkit-background-clip: text; 该属性 <style> .b1{ width: 500px; height: 200px; font- ...