RPC usage in Hadoop

import package:

Understand: rpc is a "remote procedure call protocol"

RPC uses a client/server model. The requestor is a client, and the service provider is a server. First, the client calling process sends a call message with process parameters to the server process, and then waits for a reply message. On the server side, the process stays asleep until the call message arrives. When a call message arrives, the server obtains the process parameters, calculates the result, sends a reply message , and then waits for the next call message. Finally, the client calls the process to receive the reply message, obtain the process result, and then the call execution continues.

 

Specific use: Aspects can be understood as, for example, the background data of a company supports the use of multiple platforms of this company, if each platform writes a set of calling logic, if the background logic is adjusted, each platform needs to be involved; The operation of accessing the back-end of the platform is released in advance and placed on the server side to provide services, and then all platforms can be used; and the back-end is also hidden from the platform development.

Development Note:

Communication protocol interface between rpc server and rpc client. Both the server and the client must have the same path

Examples are as follows:

One: Build two projects:

Client project: democlient

Server-side project: demonamenode

Both server-side and client-side development need to build the same interface: a protocol that both sides of network communication must follow

1> The development path is the same; --hadoop.rpctest

2> The names are the same. --DemoNamenodeProtocol.java

The server side needs a protocol implementation class: for example DemoNamenodeImpl.java;

The server side also needs to publish the implementation of the protocol to the RPC service

Test implementation here: ServerPublisher.java

Two: After the server project is implemented, the Runnable JAR needs to be packaged and uploaded to the server.

Release: java -jar ***.jar

 

Three: After the server is published, the client can call this service.

 

The code implementation is attached:

-------------------------------------------------- -------------------start
custom protocol interface: DemoNamenodeProtocol

package hadoop.rpctest;
/*
 * 接口定义网络通信双方共同遵循的约定或协议
 */
public interface DemoNamenodeProtocol {
       
       //定义通信上方一致的版本号
       public static final long versionID = 1L;
//定义通信双方可以调用的方法
       public String getMetaData(String filePath);
}

--------------------------------------------------------------end

-------------------------------------------------- -------------------start
server-side implementation class: DemoNamenodeImpl

package hadoop.rpctest;
public class DemoNamenodeImpl implements DemoNamenodeProtocol{
       public String getMetaData(String filePath) {
              return filePath + "-----我给你开玩笑呢" ;
       }
}

--------------------------------------------------------------end

-------------------------------------------------- -------------------start
service publishing class: ServerPublisher

package hadoop.rpctest;
import java.io.IOException;
import org.apache.hadoop.HadoopIllegalArgumentException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.ipc.RPC;
import org.apache.hadoop.ipc.RPC.Builder;
import org.apache.hadoop.ipc.RPC.Server;
//用于把我们定义的业务功能发布为rpc服务
public class ServerPublisher {
    public static void main(String[] args) throws HadoopIllegalArgumentException, IOException {
        // TODO Auto-generated method stub
        //获取一个创建rpc服务的builder
        Builder builder = new RPC.Builder(new Configuration());
        //通过Builder与我名定义的业务功能建立关系
        //设置服务端服务实例实现的接口--及通信双方功能遵循的协议
        builder.setProtocol(DemoNamenodeProtocol.class);
        //设置提供业务服务的具体实例对象
        builder.setInstance(new DemoNamenodeImpl());
        
        //设置服务进程所绑定的地址信息
        builder.setBindAddress("hadoop02");
        //设置服务进程的端口
        builder.setPort(10000);
        
        //用builder来创建一个服务  (socket服务)
        Server server = builder.build();
        
        //启动服务,这样就可以被客户端调用了
        server.start();
    }
}

--------------------------------------------------------------end

Client access test: DemoClient

package clienttest;
import java.io.IOException;
import java.net.InetSocketAddress;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.ipc.RPC;
import hadoop.rpctest.DemoNamenodeProtocol;
public class DemoClient {
public static void main(String[] args) throws IOException {
    //客户端访问rpc服务器代码实现
        InetSocketAddress addr = new InetSocketAddress("hadoop02",10000);
        DemoNamenodeProtocol demoNamenodeImpl = RPC.getProxy(DemoNamenodeProtocol.class, 1L, addr, new Configuration());
        String MetaData = demoNamenodeImpl.getMetaData("获取RPC服务:hello world");
        System.out.println(MetaData);
}
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325942785&siteId=291194637
RPC
RPC