PRC 不同java进程间的对象方法调用(server +client),是
Hadoop框架运行的基础(服务端执行客户端调用)
1.Server ,提供对象,供客户端调用,被调用的对象的方法执行发生在服务端
2.Client ,
、、、、、、、、、、、、、、代码
package com.rpc;
import org.apache.hadoop.ipc.VersionedProtocol;
public interface UserInterface extends VersionedProtocol {
public static final long version=00000;
public abstract String hellow(String demo);
}
、、、、、、、、
package com.rpc;
import java.io.IOException;
public class User implements UserInterface {
/* (non-Javadoc)
* @see com.rpc.UserInterface#hellow(java.lang.String)
*/
@Override
public String hellow(String demo){
return "hellow"+demo;
}
@Override
public long getProtocolVersion(String arg0, long arg1) throws IOException {
// TODO Auto-generated method stub
return UserInterface.version;
}
}
、、、、、、、、、、、、、、、、、
package com.rpc;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.ipc.RPC;
import org.apache.hadoop.ipc.RPC.Server;
public class IServer {
/** Construct an RPC server.
* @param instance 方法将调用的实例
* @param conf the configuration to use
* @param bindAddress 地址绑定监听连接
* @param port 连接端口
* @param numHandlers the number of method handler threads to run
* @param verbose whether each call should be logged
*/
public static final String Server_Address="localhost";
public static final int Server_port=1234;
public static void main(String[] args) throws IOException {
final Server server = RPC.getServer(new User(), Server_Address, Server_port, new Configuration());
server.start();//启动服务
}
}
、、、、、、、、、、、、、、、、、、
package com.rpc;
import java.net.InetSocketAddress;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.ipc.RPC;
public class IClient {
public static void main(String[] args) throws Exception {
/** 构建一个客户端代理对象实现指定的协议
,
* talking to a server at the named address. */
final UserInterface Proxy = (UserInterface) RPC.waitForProxy(UserInterface.class,
UserInterface.version,
new InetSocketAddress(IServer.Server_Address,IServer.Server_port),
new Configuration() );
final String hellow = Proxy.hellow("word");
System.out.println("客户端调用结果:"+hellow);
RPC.stopProxy(Proxy);//关闭代理对象
}
}