转于自己在公司的Blog:
http://pt.alibaba-inc.com/wp/experience_1330/simple-rpc-framework.html
因为要给百技上实训课,让新同学们自行实现一个简易RPC框架,在准备PPT时,就想写个示例,发现原来一个RPC框架只要一个类,10来分钟就可以写完了,虽然简陋,也晒晒:
用起来也像模像样:
(1) 定义服务接口
(2) 实现服务
(3) 暴露服务
(4) 引用服务
http://pt.alibaba-inc.com/wp/experience_1330/simple-rpc-framework.html
因为要给百技上实训课,让新同学们自行实现一个简易RPC框架,在准备PPT时,就想写个示例,发现原来一个RPC框架只要一个类,10来分钟就可以写完了,虽然简陋,也晒晒:
- /*
- * Copyright 2011 Alibaba.com All right reserved. This software is the
- * confidential and proprietary information of Alibaba.com ("Confidential
- * Information"). You shall not disclose such Confidential Information and shall
- * use it only in accordance with the terms of the license agreement you entered
- * into with Alibaba.com.
- */
- package com.alibaba.study.rpc.framework;
- 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
- *
- * @author william.liangf
- */
- 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() {
- @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());
- 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 服务器主机名
- * @param port 服务器端口
- * @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() {
- 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();
- }
- }
- });
- }
- }
用起来也像模像样:
(1) 定义服务接口
- /*
- * Copyright 2011 Alibaba.com All right reserved. This software is the
- * confidential and proprietary information of Alibaba.com ("Confidential
- * Information"). You shall not disclose such Confidential Information and shall
- * use it only in accordance with the terms of the license agreement you entered
- * into with Alibaba.com.
- */
- package com.alibaba.study.rpc.test;
- /**
- * HelloService
- *
- * @author william.liangf
- */
- public interface HelloService {
- String hello(String name);
- }
(2) 实现服务
- /*
- * Copyright 2011 Alibaba.com All right reserved. This software is the
- * confidential and proprietary information of Alibaba.com ("Confidential
- * Information"). You shall not disclose such Confidential Information and shall
- * use it only in accordance with the terms of the license agreement you entered
- * into with Alibaba.com.
- */
- package com.alibaba.study.rpc.test;
- /**
- * HelloServiceImpl
- *
- * @author william.liangf
- */
- public class HelloServiceImpl implements HelloService {
- public String hello(String name) {
- return "Hello " + name;
- }
- }
(3) 暴露服务
- /*
- * Copyright 2011 Alibaba.com All right reserved. This software is the
- * confidential and proprietary information of Alibaba.com ("Confidential
- * Information"). You shall not disclose such Confidential Information and shall
- * use it only in accordance with the terms of the license agreement you entered
- * into with Alibaba.com.
- */
- package com.alibaba.study.rpc.test;
- import com.alibaba.study.rpc.framework.RpcFramework;
- /**
- * RpcProvider
- *
- * @author william.liangf
- */
- public class RpcProvider {
- public static void main(String[] args) throws Exception {
- HelloService service = new HelloServiceImpl();
- RpcFramework.export(service, 1234);
- }
- }
(4) 引用服务
- /*
- * Copyright 2011 Alibaba.com All right reserved. This software is the
- * confidential and proprietary information of Alibaba.com ("Confidential
- * Information"). You shall not disclose such Confidential Information and shall
- * use it only in accordance with the terms of the license agreement you entered
- * into with Alibaba.com.
- */
- package com.alibaba.study.rpc.test;
- import com.alibaba.study.rpc.framework.RpcFramework;
- /**
- * RpcConsumer
- *
- * @author william.liangf
- */
- 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机制把本地调用转换成远程调用,参考的是SUN的官方文档。
拍拍贷微服务rpc框架源码.zip # 拍拍贷微服务体系 拍拍贷微服务体系是拍拍贷基础框架部总结内部微服务多年实践,参考、吸收大量业内解决方案形成的适合中型互联网公司的微服务解决方案。 拍拍贷微服务体系主要组成...
RPC8211FS是一种高度集成的以太网收发器,符合10Base-T、100Base-TX 和1000Base-T IEEE 802.3标准。它提供了通过 CAT.5 UTP 电缆传输和接收以太网数据包所需的所有物理层功能。 RPC8211FS为基于 IEEE 1588和802.1 AS...
(1)用java写的RPC调用的demo。通过这个demo,可以了解RPC的基本原理。 (2)没有用到其他的jar包,java对象和流之间的转换用的是java.io.ObjectInputStream和java.io.ObjectOutputStream。 (3)下载解压,可以...
通过RPC转到插件系统 go-plugin是基于RPC的Go(golang)插件系统。 这是HashiCorp工具使用超过4年的插件系统。 当最初为创建时,它又被 , 和。 虽然插件系统是通过RPC进行的,但目前仅设计为在本地[可靠]网络上...
1、JS2Java RPC:能够在javascript的web浏览器环境中指定java类,获取该类实例的属性, 调用该实例的方法 2、支持复杂js对象作为入参:能够传递复杂的JavaScript Object对象作为调用参数, 在java中得到对应的Map...
NULL 博文链接:https://chembo.iteye.com/blog/905038
Java的JSON-RPC 该项目旨在提供轻松实现Java编程语言的JSON-RPC的功能。 jsonrpc4j使用库在json对象(以及与JSON-RPC相关的其他东西)之间来回转换java对象。 功能包括: 流服务器( InputStream \ OutputStream ...
OCaml-RPC-远程过程调用(RPC)库 ocaml-rpc是一个库,提供使用XML或JSON作为传输编码的远程过程调用(RPC)。 传输机制本身不在此库的范围内,因为所有转换都是从字符串到字符串的。 odoc生成的文档可在。 RPC类型 ...
Conflux节点使用与以太坊JSON-RPC稍有不同的函数调用 使用JSON-RPC服务器作为中继 中继将呼叫转换为ETH呼叫转换为CFX呼叫(预处理) 中继将CFX响应转换为ETH响应(后处理) 指令 yarn start :启动继电器 yarn ...
Java转换JSON的包
由于项目组现在用的rpc是基于google protobuf rpc协议实现的,所以花了点时间了解下protobuf rpc。rpc对于做分布式系统的人来说肯定不陌生,对于rpc不了解的童鞋可以自行google,这里只是...代理封装调用信息并将调用转
使用RPC系数,对高分三号一级产品进行几何校正
如果您将aria2 1.15.2(或更高版本)与'JSON-RPC PATH'一起使用,例如设置--rpc-user=<username> --rpc-passwd= 使用http://localhost:6800/jsonrpc#max-connection-per-server=5&split=10设置特定文件的下载选项...
rpc Discord RPC使您的Discord配置文件看起来更漂亮。 指示 首先进入VSC(Visual ...转到cmd和cd进入rpc的目录。 然后输入以下内容:节点main.js,现在将其保留在那里。 当您关闭它时,RPC将消失,就像我的父亲一样
xml-rpc.js 跨域 JavaScript 的 XML-RPC 库。 基于 Carlos Eduardo Goncalves 最初编写的 Mimic JS 库; 许可证见下文。 为 XML-RPC 提供易于使用的面向对象的接口。 将对象透明地转换为 JS 文字是实用的(例如 ...
简单的JSON-RPC 一个用于将协议简单集成到Java应用程序的库。 目的是提供一种简单,快速和可靠的方法,以将JSON-RPC 2.0协议集成到服务器和/或客户端上的Java应用程序中。 为此,您需要配置JsonRpcClient或...
简单的节俭教程创建一个定义服务的.thrift文件thrift --gen java ... 处理器是IDL编译器生成的服务器端存根,它将网络RPC转换为对处理程序的调用。 MultiplicationClient.java-构造了客户端对象并将其提供给
ts-open-rpc 用于将TypeScript接口转换为OpenRPC接口描述的命令行实用程序。 注意:早期MVP。 许多待办事项。 用法 首先,拉回购和yarn link或npm link 。 程序输出到标准输出: ts-to-open-rpc METHOD_NAME ...
只需转到“选项卡并下载最新版本。 从源头运行 确保已安装Python 3。 如果您使用的是Linux,请运行python pip install -r requirements.txt或python3 -m pip install --requirement requirements.txt 。 然后,...