(中文) RPC框架技术初窥 - Cobub
(中文) RPC框架技术初窥

(中文) RPC框架技术初窥

7 years ago 1 6457

What is RPC?

RPC (Remote Procedure Call Protocol) – Remote Procedure Call Protocol, which is a Protocol for requesting services from Remote computer programs over a network without the need to understand underlying network technologies.
RPC USES client/server mode.A requester is a client, and a service provider is a server.First, the client call process sends a process parameter call to the service process and waits for the reply message.On the server side, the process stays asleep until the call is reached.When a call information to the server process parameters, the calculation results, send a reply information, and then wait for the next call information, in the end, the client calling process to receive a reply information, obtain process as a result, and then call to continue execution.
The above is baidu encyclopedia’s explanation to RPC.
A popular description is that the client invokes an object that exists on a remote machine, just like the object in a local application, without knowing the invocation details.

The background of RPC

In the early days of the single machine, there were many processes running on one computer.If A process requires A drawing function, B process also requires A drawing function, and the programmer must write A drawing of the function for both processes.Isn’t that the whole thing?The IPC (inter-process communication, the inter-process communication between processes running in a single machine) occurs.OK, now that A has the function of drawing, B will call the drawing function on process A.
In the Internet age, everyone’s computers are connected.Previous programs can only invoke processes on their own computers, can they invoke processes on other machines?The programmer then extends the IPC to the network, which makes RPC.
This time the drawing function can be used as an independent service for clients.

RPC framework features

The RPC protocol

Since the agreement is just a set of specifications, it needs someone to follow the specification.Currently, typical RPC implementations include: Dubbo, Thrift, GRPC, Hetty, etc.

Network protocol and network IO transparent

Now that the RPC client considers itself to be calling the local object.So the transport layer USES TCP/UDP or the HTTP protocol, which is some other network protocol that doesn’t need to be concerned.Now that the network protocol is transparent to it, there is no need to care about which network IO model caller is used during the call process.

The information format is transparent to it

In a local application, an object call needs to pass some parameters, and a call result is returned.The inside of the object is how to use these parameters and calculate the result of the processing, and the caller is not concerned.So for the RPC, these parameters will be in a message format is passed to another computer on the network, how the information format is built up, the caller is not need to care about.

Language skills

The caller doesn’t actually know what language the remote server’s application is using.So for the caller, whatever language is used on the server side, this call should be successful, and the return value should be in accordance with the caller procedures described in the form of language can understand.

How the RPC framework works


1. Invoking the client handle; Execute transmission parameter
2. Call the local system kernel to send network messages
3. The message is sent to the remote host
4. The server handle gets the message and takes the parameters
5. Perform remote procedure
6. The execution process returns the result to the server handle
7. The server handle returns the result and invokes the remote system kernel
8. The message returns to the local host
9. The client handle receives the message from the kernel
10. The customer receives the data from the handle

Work on your own RPC framework

Code implementation to do the work

1. Design external interfaces

1
2
3
4
5
public interface IService extends Remote {  
 
    public String queryName(String no) throws RemoteException;  
 
}

2. Service implementation of the server

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class ServiceImpl extends UnicastRemoteObject implements IService {  
 
    private static final long serialVersionUID = 682805210518738166L;  
 
    protected ServiceImpl() throws RemoteException {  
        super();  
    }  
 
    @Override  
    public String queryName(String no) throws RemoteException {  
        // 方法的具体实现  
        return String.valueOf(System.currentTimeMillis());  
    }  
}

3. RMI server implementation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Server {  
    public static void main(String[] args) {    
        Registry registry = null;  
        try {  
            // 创建一个服务注册管理器  
            registry = LocateRegistry.createRegistry(8088);  
        } catch (RemoteException e) {        
        }  
        try {  
            // 创建一个服务  
            ServiceImpl server = new ServiceImpl();  
            // 将服务绑定命名  
            registry.rebind("vince", server);   
        } catch (RemoteException e) {                
        }  
    }  
}

4. client-side implementation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Client {  
    public static void main(String[] args) {  
        Registry registry = null;  
        try {  
            // 获取服务注册管理器  
            registry = LocateRegistry.getRegistry("127.0.0.1",8088);   
        } catch (RemoteException e) {  
        }  
        try {  
            // 根据命名获取服务  
            IService server = (IService) registry.lookup("vince");  
            // 调用远程方法 ,获取结果。
            String result = server.queryName("ha ha ha ha");   
        } catch (Exception e)  {            
        }  
    }  
}