簡單瞭解 RPC 實現原理

之前一直在用rpc框架,內部原理今天學習了一下,原作者樑飛,在此記錄下他非常簡潔的rpc實現思路,通過該思路瞭解一下原理。

        一個完整的RPC架構裏面包含了四個核心的組件,分別是Client,Client Stub,Server以及Server Stub,這個Stub可以理解爲存根。

  • 客戶端(Client),服務的調用方。
  • 客戶端存根(Client Stub),存放服務端的地址消息,再將客戶端的請求參數打包成網絡消息,然後通過網絡遠程發送給服務方。
  • 服務端(Server),真正的服務提供者。
  • 服務端存根(Server Stub),接收客戶端發送過來的消息,將消息解包,並調用本地的方法。

RPC調用過程:

(1) 客戶端(client)以本地調用方式(即以接口的方式)調用服務;

(2) 客戶端存根(client stub)接收到調用後,負責將方法、參數等組裝成能夠進行網絡傳輸的消息體(將消息體對象序列化爲二進制);

(3) 客戶端通過sockets將消息發送到服務端;

(4) 服務端存根( server stub)收到消息後進行解碼(將消息對象反序列化);

(5) 服務端存根( server stub)根據解碼結果調用本地的服務;

(6) 本地服務執行並將結果返回給服務端存根( server stub);

(7) 服務端存根( server stub)將返回結果打包成消息(將結果消息對象序列化);

(8) 服務端(server)通過sockets將消息發送到客戶端;

(9) 客戶端存根(client stub)接收到結果消息,並進行解碼(將結果消息發序列化);

(10) 客戶端(client)得到最終結果。

RPC的目標是要把2、3、4、7、8、9這些步驟都封裝起來。

 

一、核心框架類

/*
 * 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();
                }
            }
        });
    }

}

二、定義服務接口

/*
 * 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);

}

三、實現服務

/*
 * 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;
    }

}

四、暴露服務

/*
 * 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);
    }

}

五、引用服務

/*
 * 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 IO流來進行server和client的通信,也就是rpc應用中服務提供方和服務消費方。並且是端對端的,用端口號來直接進行通信。方法的遠程調用使用的是jdk的動態代理,參數的序列化也是使用的最簡單的objectStream。

       真實的rpc框架會對上面的實現方式進行替換,採用更快更穩定,更高可用易擴展,更適宜分佈式場景的中間件,技術來替換。例如使用netty的nio特性達到非阻塞的通信,使用zookeeper統一管理服務註冊與發現,解決了端對端不靈活的劣勢。代理方式有cglib字節碼技術。序列化方式有hession2,fastjson等等。不過樑飛大大的博客使用原生的jdk api就展現給各位讀者一個生動形象的rpc demo,實在是強。rpc框架解決的不僅僅是技術層面的實現,還考慮到了rpc調用中的諸多問題,重試機制,超時配置…這些就需要去了解成熟的rpc框架是如果考慮這些問題的了。

 

 

發佈了62 篇原創文章 · 獲贊 14 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章