愿文:http://javatar.iteye.com/blog/1123915
因为要给百技上实训课,让新同学们自行实现一个简易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);
- }
- }
- }
相关推荐
使用socket/反射/序列化等技术实现了一个基本的远程调用框架,可参考博客http://blog.csdn.net/u013177446/article/details/66473066
Java rpc框架简易版,类似dubbo分布式实现 (纯socket实现).zip
java原生实现的RPC框架,使用技术:JDK动态代理、Socket通信(BIO方式)、反射、注解、Java序列化
可参考博客http://blog.csdn.net/u013177446/article/details/66473066 使用netty/反射/序列化反序列化等技术是一个一个简单的RPC框架
基于dubbo实现的rpc框架RPC 框架指的是:可以让客户端直接调用服务端方法就像调用本地方法一样简单的框架,比如我前面介绍的 Dubbo、Motan、gRPC 这些.zip
包含RPC原理、NIO操作、netty简单的api、自定义RPC框架
不依赖任何第三方类库,自己实现一个JAVA版的最简单RPC框架。对理解dubbo,thrift原理有所帮助
了解RPC框架Feign并实现简单RPC框架
根据题意,要实现一个 RPC 框架,实现对 printf 函数的远程调用。故大致可以分为两部分,RPC 框架的部分,以及 print 函数的部分,根据题目对其有不同的要求。
基于dubbo实现的rpc框架RPC 框架指的是:可以让客户端直接调用服务端方法就像调用本地方法一样简单的框架,比如我前面介绍的 Dubbo、Motan、gRPC 这些.zip
通过协调系统实现生产者和消费者的互通,其中有心跳检测等..
简介 demo-rpc(标准maven工程) 使用纯Java socket及简单多线程技术,不依赖任何第三方库类,实现简单实现类似dubbo的rpc调用。仅用于学习了解rpc调用过程, 实现略显简单,只体现rpc调用的关键步骤,存在很多优化细节,...
用Netty实现一个简单的RPC框架,基本上rpc主要的知识点都涉及到了,包括协议的定义,序列化反序列化,动态代理,Spring自动装配,Netty编解码器等。可以通过这个项目加强对Netty的学习掌握,也可以加深对RPC的理解。...
本文旨在讲述 RPC 框架设计中的几个核心问题及其解决方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
一个自己实现的极为简单的RPC框架,可以用来做Hessian等技术框架研究的参考。
基于Zookeeper+Netty+Protostuff实现的简单RPC框架源码,代码内有详细注释
详情介绍:https://www.yuque.com/sxbn/ks/100010261 基于akka与scala实现一个简单rpc框架 RPC,即 Remote Procedure Call(远程过程调用)。
## 用Python构建分布式高并发的RPC框架 ------ ### 一、为什么要写一个RPC框架? > + 不是想要造轮子,Dubbo、gRPC、Thift这些轮子已经非常好用了 > + RPC在微服务、分布式系统、Web服务器方面应用太广泛了,...
特技-Rpc-框架一个简单的RPC框架的实现
它可以有不同的实现方式。如RMI(远程方法调用)、Hessian、Http invoker等。另外,RPC是与语言无关的。RPC示意图如上图所示,假设Computer1在调用sayHi()方法,对于Computer1而言调用sayHi()方法就像调用本地方法一样...