Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Simple use of RPC for Hadoop (remote procedure call protocol)

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

I. Overview of RPC

RPC refers to remote procedure call, that is, two different servers (not limited by the operating system), one application is deployed on Linux-A, one application is deployed on Windows-B or Linux-B, if A wants to call a method method () on B, because it is not in a memory space, it can not be called directly, so it needs to express the meaning of the call and convey the parameters of the call through the network.

Before the landlord comes into contact with RPC, nothing is more frequently used than WebService. WebService can be said to be based on the development of RPC. There are many RPC agreements, such as the earliest CORBA,Java RMI,Web Service, etc., as well as the hadoop project under Alibaba's Dubbo,Apache. The owner of this article mainly takes RPC of hadoop as an example.

Why does hadoop use RPC? In HDFS, we can see through jsp that there are DataNode,NameNode,SecondaryNameNode main processes (the landlord only started HDFS), our client Client communicates with NameNode, and the communication between NameNode and DataNode is between different processes and different systems.

II. RPC process

Through the following figure, we briefly analyze the execution process of RPC:

First of all, to solve the communication problem, mainly by establishing a TCP connection between Client and Server, all the exchanged data of remote procedure calls are transmitted in this connection. The connection can be an on-demand connection, which is broken at the end of the call, or a long connection, where multiple remote procedure calls share the same connection.

Second, to solve the problem of addressing, that is, how the application on the A server tells the underlying RPC framework, how to connect to the B server (such as the host or IP address) and the specific port, what is the name of the method, so that the call can be completed.

Third, when the application on Client initiates the remote procedure call, the parameters of the method need to be passed to Server through the underlying network protocol such as TCP. Because the network protocol is based on binary, the values of the parameters in memory should be serialized into binary form, that is, Serialize, and the serialized binary is sent to B server through addressing and transmission.

Fourth, after receiving the request, Server needs to deserialize the parameters (the reverse operation of serialization), revert to the in-memory expression, then find the corresponding method (part of the addressing) to make a local call, and then get the return value.

III. Simple use of hadoop-RPC

Define the interface Bizable:

1 package cn.jon.hadoop.rpc;2 3 public interface MyBizable {4 long versionID = 123456 / this field is required, otherwise it will report java.lang.NoSuchFieldException: versionID exception 5 public String doSomething (String str); 6}

Server RPCServer implements MyBizable interface and binds IP address and port number:

Package cn.jon.hadoop.rpc;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.ipc.RPC;import org.apache.hadoop.ipc.Server;public class RPCServer implements MyBizable {@ Override public String doSomething (String str) {return str } / * * @ param args * @ throws Exception * @ throws * / public static void main (String [] args) throws Exception {Server server = new RPC.Builder (new Configuration ()) .setProtocol (MyBizable.class) .setInstance (new RPCServer ()) .setBindAddress ("192.168.8.100") .setPort (8077) .build () Server.start ();}}

Client RPCClient:

Package cn.jon.hadoop.rpc;import java.net.InetSocketAddress;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.ipc.RPC Public class RPCClient {/ * * @ param args * @ throws Exception * / public static void main (String [] args) throws Exception {/ / TODO Auto-generated method stub MyBizable proxy = RPC.getProxy (MyBizable.class, 123456 new InetSocketAddress ("192.168.100", 8077), new Configuration (); String result = proxy.doSomething ("server"); System.out.println (result) RPC.stopProxy (proxy);}}

The landlord uses Linux as the client and Windows as the server. We first type the written program into jar and upload it to Linux:

Then, we start RPCServer on the windows side:

After the server starts up, we execute RPCClient.jar in Linux:

Java-jar RPCClient.jar

In the execution result, you can see that the "server" is output (the Linux time of the building owner is not set correctly):

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report