愿文: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、反射和序列化等技术来实现一个简单的RPC框架。 首先,让我们了解RPC的基本原理。在RPC模型中,客户端(Client)发起一个函数调用请求,这个请求包含了目标函数的名称和参数。RPC框架...
本项目提供了一个简易版的Java RPC框架实现,旨在模仿著名的Dubbo框架,但采用了更基础的Socket通信方式进行分布式服务的搭建。以下是这个项目的核心知识点: 1. **RPC原理**:RPC使得客户端可以像调用本地方法一样...
在这个“JAVA实现简单RPC框架”的项目中,我们将探讨如何利用Java的核心特性来构建这样的框架。以下是关键知识点的详细说明: 1. **JDK动态代理**: JDK动态代理是Java提供的一种机制,可以在运行时创建一个实现了...
本文将深入探讨如何基于Java实现一个简单的RPC框架,并涉及心跳检测和生产消费者模型这两个关键概念。 首先,RPC允许一个程序调用另一个在不同地址空间中的程序,这个地址空间可能在同一个机器上,也可能在远程网络...
通过Zookeeper,RPC框架可以实现简单的负载均衡策略,如轮询、随机等。当有多个服务提供者时,消费者可以从Zookeeper获取到所有可用的服务实例,然后根据负载均衡策略选择一个合适的实例进行调用,这样可以有效分摊...
实现一个简单的RPC框架主要涉及以下几个关键点: 1. **网络通信库**:Netty是一个高性能、异步事件驱动的网络应用框架,常用于构建高并发、低延迟的服务器。在RPC框架中,Netty可以处理客户端与服务端之间的网络...
在这个场景中,你提到的是你自己编写了一个RPC框架,这是一项技术挑战,因为RPC框架涉及到网络通信、序列化、服务注册与发现等多个关键领域。 在你提供的博客链接中...
总结来说,Hadoop的RPC框架为开发者提供了一种简单而强大的方式来实现跨进程通信。通过上述步骤,你可以轻松地在自己的项目中集成和使用Hadoop的RPC框架,实现服务间的高效交互。理解并熟练掌握这一技术,对于构建...
在Java中实现一个简单的RPC框架,我们需要理解以下几个关键概念和技术: 1. **网络通信**:RPC的核心是通过网络进行通信。在Java中,我们可以使用Socket API来实现客户端和服务器端之间的数据传输。Socket提供了低...
在Java世界中,JSON-RPC作为一个高性能的开源RPC框架,为分布式系统中的服务调用提供了便利。这个框架允许应用程序通过网络在不同的进程之间传递方法调用,仿佛这些方法是在本地对象上调用一样。 JSON-RPC的核心...
本项目是一个使用Java语言实现的简易RPC框架,旨在为开发者提供一个易于理解和使用的远程过程调用解决方案。 该项目的核心是构建一个简易的RPC框架,其包含了多个关键组件。首先是提供服务的RPC服务器端,其次是...
总的来说,基于Dubbo的RPC框架为开发者提供了高效、灵活的分布式服务调用解决方案,使得服务之间的交互变得简单而透明,极大地提升了系统的扩展性和可维护性。在Java开发中,掌握Dubbo的使用和配置,对于构建大规模...
通过学习Dubbo的设计思想和实现机制,我们可以借鉴其优点,如简单易用的API、丰富的服务治理功能等,来构建我们的自定义RPC框架。 在自定义RPC框架的实现过程中,你需要关注以下几个关键点: 1. **服务接口与实现*...
包含RPC原理、NIO操作、netty简单的api、自定义RPC框架
QiuRPC是基于Java实现的一个轻量级RPC框架,其设计目标是简单易用,易于理解和扩展。下面将从几个关键组件来分析QiuRPC的实现原理: 1. **服务接口与实现**: - QiuRPC允许开发者定义服务接口,然后在服务提供者侧...
在RPC框架中,动态代理通常用于实现服务调用的透明化。服务接口被定义为一个或多个Java接口,服务提供者实现这些接口。服务消费者并不直接与服务提供者交互,而是通过代理对象调用接口方法。当调用代理对象的方法时...
# 基于Java的RPC框架实现 ## 项目简介 本项目是一个基于Java的RPC(远程过程调用)框架实现,旨在提供一个简单、高效的分布式系统通信解决方案。RPC框架允许客户端像调用本地方法一样调用远程服务,支持服务注册与...
总结来说,Dubbo作为一款强大的RPC框架,它的设计和实现涉及到了服务治理的多个重要方面。通过对Dubbo的深入学习和源码分析,我们可以提升分布式系统的设计和运维能力,为构建高可用、高并发的微服务架构打下坚实...
本压缩包中包含的文件主要与实现一个类似GRPC的简易RPC框架相关。GRPC是Google开发的一个高性能、开源和通用的RPC框架,基于HTTP/2协议传输,使用Protocol Buffers作为接口描述语言。在这个压缩包中,我们可以看到...
RPC框架让远程服务调用如同本地方法调用一样简单,极大地简化了分布式系统之间的通信。 Nfs-rpc框架的设计目标是高性能和高可用性,其核心优势在于其优秀的网络通信能力以及对并发处理的优化。该框架采用异步通信...