zoukankan      html  css  js  c++  java
  • Hadoop RPC通信机制

    客户端与服务端都要实现同一个接口Bizable,客户端得到服务端实例代码对象的方法。
    服务端需要绑定相关的IP地址、端口。
     
    1.在这里,我们使用Hadoop提供的工具类RPC.Builder,下面就是服务端相关代码
    public class RPCServer implements Bizable{
          //服务端实现sayHi接口,为客户端提供接口
          public String sayHi(String name)
          {
                return "Hi~" + name;
          }
          
          public static void main(String[] args) throws HadoopIllegalArgumentException, IOException {
                //启动服务器
                Configuration conf = new Configuration();
                Server server = new RPC.Builder(conf).setProtocol(Bizable.class).setInstance(new RPCServer()).setBindAddress("192.168.8.100").setPort(9527).build();
                server.start();
          }
    }
     
     
    2.接口代码如下:
    public interface Bizable {
          public static final long versionID = 10010;
          public String sayHi(String name);
    }
     
    3.客户端,需要得到服务端代理对象,通过RPC.getProxy可以得到服务器代码对象
    public class RPCClient {
          public static void main(String[] args) throws IOException {
              //通过RPC得到一个代理对象
              Bizable proxyBizable = RPC.getProxy(Bizable.class, 10010, new InetSocketAddress("192.168.8.100", 9527), new Configuration());
             
              String result = proxyBizable.sayHi("Tomcat");
              System.out.println(result);
              RPC.stopProxy(proxyBizable);
          }
    }
     
    4.我们先启动服务端,再启动客户端,就可以看到Hi~ Tomcat
  • 相关阅读:
    Codeforces Round #131 (Div. 2) E. Relay Race dp
    Codeforces Round #130 (Div. 2) C
    Codeforces Round #130 (Div. 2) A. Dubstep
    UVA 11858 Frosh Week 逆序对统计
    UVA 11149 Power of Matrix 快速幂
    UVA 12382 Grid of Lamps 贪心
    POJ 3614 Sunscreen 贪心
    Codeforces Round #238 (Div. 2) D. Toy Sum 暴搜
    Codeforces Round #227 (Div. 2) E. George and Cards 线段树+set
    Codeforces Round #327 (Div. 2) E. Three States
  • 原文地址:https://www.cnblogs.com/dulixiaoqiao/p/6978588.html
Copyright © 2011-2022 走看看