转:javatar.iteye.com/blog/1123915
因为要给百技上实训课,让新同学们自行实现一个简易RPC框架,在准备PPT时,就想写个示例,发现原来一个RPC框架只要一个类,10来分钟就可以写完了,虽然简陋,也晒晒:
用起来也像模像样:
(1) 定义服务接口
(2) 实现服务
(3) 暴露服务
(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.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);
- }
- }
- }
<iframe id="aswift_0" style="left: 0px; position: absolute; top: 0px;" name="aswift_0" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" width="728" height="90"></iframe>
相关推荐
本项目是一个使用Java语言实现的简易RPC框架,旨在为开发者提供一个易于理解和使用的远程过程调用解决方案。 该项目的核心是构建一个简易的RPC框架,其包含了多个关键组件。首先是提供服务的RPC服务器端,其次是...
本项目是一个基于Java语言和Vert.x框架的简易RPC框架的设计与实现,通过提供一套完整的源码,为开发者提供了一种高效且简洁的RPC解决方案,特别适合于服务间频繁通信的跨服务场景。 Java作为一种广泛使用的编程语言...
基于Java的简易RPC框架 项目简介 本项目是一个简易的RPC(远程过程调用)框架,旨在通过模拟实现一个基本的RPC调用流程,帮助理解RPC的核心概念和实现原理。服务端采用Tomcat服务器,消费端使用HTTP协议发送网络...
本项目是一个基于Netty、Kyro和Zookeeper实现的简易RPC框架。通过这个框架,客户端可以像调用本地方法一样调用远程服务端的方法。项目代码注释详细,结构清晰,并且集成了Check Style规范代码结构,非常适合阅读和...
本项目提供了一个简易版的Java RPC框架实现,旨在模仿著名的Dubbo框架,但采用了更基础的Socket通信方式进行分布式服务的搭建。以下是这个项目的核心知识点: 1. **RPC原理**:RPC使得客户端可以像调用本地方法一样...
在这个基于Java实现的简易RPC框架项目中,我们将深入探讨如何构建这样一个框架,并实现对`printf`函数的远程调用。 首先,我们要理解RPC的基本原理。RPC使得客户端可以像调用本地方法一样调用远程服务器上的方法,...
这个简易的JSON-RPC框架是作者自己的实践成果,其设计灵感来源于已有的RPC解决方案,但具体实现代码是作者独立编写的。经过不断的优化和调整,该框架已经在实际的生产环境中稳定运行,每天支持约8000万次的调用,...
总结,构建一个简易的Go RPC框架,主要涉及服务端的函数映射和调用、客户端的函数调用代理、数据传输格式的设计以及反射技术的运用。这个过程既展示了Go语言的强类型特性,也体现了其在处理网络通信时的灵活性。
简易版RPC框架中包含RPC框架运行的核心基本组件: 消费者模块、服务提供者模块、公共模块、Web服务器、本地服务注册器、序列化器、请求处理器和基于代理模式(动态代理)。 简易版框架已经实现了远程过程调用的整体...
csharp/c#/.net使用 TCP + JsonUtility + Task 的简易 RPC 网络框架 Simple RPC network framework using TCP + JsonUtility + Task.zip
另外,xs-rpc-frame-core模块是整个框架的核心,负责核心逻辑的处理,而xs-rpc-frame-simple则提供了一个简易版本的RPC框架,便于理解和入门。xs-rpc-spring-boot-starter则是一个Spring Boot的启动器,方便Spring ...
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。...如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。
# 基于Netty+Kyro+Zookeeper的RPC框架 [中文](./README.md)|English ## 前言 通过这个简易的轮子,你可以学到 RPC 的底层原理及原理以及各种 Java 编码实践的运用。 ## 介绍 由于 Guide哥自身精力和能力...
读书笔记:简易版 RPC 框架实战拉勾教育专栏《Netty 核心原理剖析与 RPC 实践》源码
本压缩包中包含的文件主要与实现一个类似GRPC的简易RPC框架相关。GRPC是Google开发的一个高性能、开源和通用的RPC框架,基于HTTP/2协议传输,使用Protocol Buffers作为接口描述语言。在这个压缩包中,我们可以看到...
客户端从服务列表中选取其中一个的服务地址,并将数据通过网络发送给服务端; 服务端接收到数据后进行解码,得到请求信息; 服务端根据解码后的请求信息调用对应的服务,然后将调用结果返回给客户端。 模块依赖 rpc-...
本文档将通过一系列章节详细介绍如何基于Netty 4.1版本实现一个简易的RPC框架。 #### 二、基础知识概述 ##### 2.1 RPC概念 - **定义**:RPC(Remote Procedure Call Protocol)即远程过程调用协议,它允许程序...
rpc框架前言学习javaGuide,自己动手造个轮子,通过这个简易的轮子,可以学到RPC的扭曲原理和原理以及各种Java编码实践的运用。介绍是一种基于Netty + Kyro + Zookeeper实现的RPC框架。设计思路一个基本的RPC框架...
熔断降级机制简易RPC框架-SPI熔断降级实现影响上下文机制,后续更新解决基于注解的锁Spring boot实践WEBValidator (未同步代码)多字段动态运算符HandlerMethodArgumentResolver (未同步代码)接口参数注入消息...
guide 目前只实现了RPC框架最基本的功能,一些可优化点都在下面提到了,有兴趣的小伙伴可以自我完善。 通过这个简易的轮子,你可以学到RPC的替代原理和原理以及各种Java编码实践的运用。 你甚至可以把当做你的毕设/...