Using RPC API provided by Hadoop to realize simple RPC program
[toc]
Using RPC API provided by Hadoop to realize simple RPC program
The usage API of RPC service is provided in Hadoop, and it is very easy to build a remote procedure call program through its API use. Here is a simple example.
Project structure
For ease of operation, all the code is placed in a local project, in fact, you can put the code on a different server, this is the concept of RPC, not too much introduction here.
The project structure is as follows:
Rpc/ ├── HelloServiceImpl.java ├── IHelloService.java ├── RPCClientDriver.java └── RPCServerDriver.java program code
This is just a simple example, and very detailed comments are given in the code, so the program code is given directly.
IHelloService.javapackage com.uplooking.bigdata.rpc;import org.apache.hadoop.ipc.VersionedProtocol;/** * Interface * if you want to use the RPC service provided by hadoop, you must inherit VersionedProtocol * / public interface IHelloService extends VersionedProtocol {public long versionID = 1L; public String sayHi (String name); public String hearBeat (String beat);} HelloServiceImpl.javapackage com.uplooking.bigdata.rpc;import org.apache.hadoop.ipc.ProtocolSignature;import java.io.IOException / * HelloService service implementation class * / public class HelloServiceImpl implements IHelloService {public String sayHi (String name) {System.out.println ("name..." + name); return "Hi," + name;} public String hearBeat (String beat) {System.out.println ("- heartbeat----" + beat); return System.currentTimeMillis () + "-->" + beat } public long getProtocolVersion (String protocol, long clientVersion) throws IOException {return versionID;} public ProtocolSignature getProtocolSignature (String protocol, long clientVersion, int clientMethodsHash) throws IOException {return new ProtocolSignature ();}} RPCServerDriver.javapackage com.uplooking.bigdata.rpc;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.ipc.RPC;import java.io.IOException / * RPC server program to start and publish the service * / public class RPCServerDriver {public static void main (String [] args) throws IOException {/ / create the configuration of RPC Configuration configuration = new Configuration (); / / build the builder object of RPC RPC.Builder builder = new RPC.Builder (configuration) / / set the information of RPC Server, and return a server object RPC.Server server = builder.setBindAddress ("localhost") .setPort (4893) .setProtocol (IHelloService.class) .setInstance (new HelloServiceImpl ()) .build () / / start RPC Server / / this is a daemon, so the main function will not exit server.start (); System.out.println ("- service started -");}} RPCClientDriver.javapackage com.uplooking.bigdata.rpc;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.ipc.RPC;import java.io.IOException;import java.net.InetAddress;import java.net.InetSocketAddress / * RPC client program * / public class RPCClientDriver {public static void main (String [] args) throws IOException {/ / build InetSocketAddress object InetSocketAddress address = new InetSocketAddress (InetAddress.getByName ("localhost"), 4893) / / obtain the type object of the proxy object / * @ param protocol interface through the RPC.getProxy method * @ param clientVersion version number * @ param addr server address * @ param conf configuration information * / IHelloService helloServiceProxy = RPC.getProxy (IHelloService.class, IHelloService.versionID, address) New Configuration () String result = helloServiceProxy.sayHi ("little Akita"); System.out.println (result);}} test
Start RPCServerDriver and the output is as follows:
-the service has started-
Start RPCClicentDriver and the output is as follows:
Hi, Akita
At this point, check the output of the server:
-the service has started-name... Xiao Qiuda
In this way, a simple RPC program is implemented by using the RPC API provided by Hadoop.