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
  • 相关阅读:
    typescript学习笔记(一)----基础类型
    iOS----------常用三方库
    iOS----------拨打电话的3种方式
    iOS----------随机色
    iOS----------常见经典错误
    iOS----------使用cocoapods遇到的问题
    iOS ----------NSDate 、CFAbsoluteTimeGetCurrent、CACurrentMediaTime 的区别
    iOS----------计算一段代码执行时间
    iOS----------The Apple Developer Program License Agreement has been updated.
    iOS-UIView指定圆角设置
  • 原文地址:https://www.cnblogs.com/dulixiaoqiao/p/6978588.html
Copyright © 2011-2022 走看看