hadoop;RPC;调用接口;cmd的jps查看java进程;有main方法的类才华产生进程

hadoop;RPC;调用接口;cmd的jps查看java进程;有main方法的类才能产生进程

RPC(remote procedure call)不同java进程间的对象方法调用,一方称作服务端,一方称作客户端;被调用的对象的方法执行发生在server端

首先应该编写服务端MyServer,客户端MyClient,操作对象类MyBiz(根据服务端方法参数推测的),操作对象接口MyBizable(根据客户端方法参数推测的)

通过查看源码,一步步向里查看,直到没有return该方法出现

package com.kane.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 MyServer {
public static final String SERVER_ADDRESS="localhost";
public static final int SERVER_PORT=12345;
public static void main(String[] args) throws IOException {
 /** Construct an RPC server.
    * @param instance the instance whose methods will be called实例化中方法会被调用
    * @param conf the configuration to use
    * @param bindAddress the address to bind on to listen for connection
    * @param port the port to listen for connections on
    * @param numHandlers the number of method handler threads to run
    * @param verbose whether each call should be logged
    */
Server server = RPC.getServer(new MyBiz(), SERVER_ADDRESS, SERVER_PORT, new Configuration());
server.start();
}
}


package com.kane.rpc;
import java.net.InetSocketAddress;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.ipc.RPC;
import org.apache.hadoop.ipc.VersionedProtocol;
public class MyClient {
public static void main(String[] args) throws Exception {
 /** Construct a client-side proxy object that implements the named protocol,
  * talking to a server at the named address.
  * 参数依次:实现协议的业务类(这要求我们的mybiz类实现该协议)版本号,服务端地址,配置 */

//final VersionedProtocol waitForProxy = RPC.waitForProxy(MyBiz.class, 1, new InetSocketAddress(MyServer.SERVER_ADDRESS, MyServer.SERVER_PORT), new Configuration());

//后来测试时报异常说业务类不是一个接口,所以采用一个接口 MyBizable继承 VersionedProtocol ,MyBiz实现 MyBizable
final MyBizable proxy =(MyBizable)RPC.waitForProxy(MyBizable.class,MyBizable.VERSION, new InetSocketAddress(MyServer.SERVER_ADDRESS, MyServer.SERVER_PORT), new Configuration());
final String str = proxy.hello("kane");
System.out.println(str);
RPC.stopProxy(proxy);
}
}

hadoop;RPC;调用接口;cmd的jps查看java进程;有main方法的类才华产生进程

hadoop;RPC;调用接口;cmd的jps查看java进程;有main方法的类才华产生进程

package com.kane.rpc;


import org.apache.hadoop.ipc.VersionedProtocol;


public interface MyBizable extends  VersionedProtocol{
public static long VERSION=1;//这里写的版本和client接收的 版本一样
public abstract String hello(String name);
}

package com.kane.rpc;


import java.io.IOException;


import org.apache.hadoop.ipc.VersionedProtocol;






public class MyBiz implements MyBizable{
/* (non-Javadoc)
* @see com.kane.rpc.MyBizable#hello(java.lang.String)
*/
@Override
public String hello(String name) {
return "hello"+name;
}


@Override
public long getProtocolVersion(String protocol, long clientVersion)
throws IOException {
// 这里返回 的版本和client写的版本一样
return MyBizable.VERSION;
}
}

先启动myserver,然后再运行myclient

hadoop;RPC;调用接口;cmd的jps查看java进程;有main方法的类才华产生进程