传智---Haoop--(7)--RPC

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);//关闭代理对象
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值