当前位置:   article > 正文

RPC简单实例_rpc实例

rpc实例

什么是RPC?

远程过程调用(Remote Procedure Call,RPC)是一个计算机通信协议。该协议允许运行于一台计算机的程序调用另一台计算机的子程序,同时将网络的通信细节隐藏起来,而程序员无需额外的为这个交互作用编程。RPC采用客户机/服务器模型。
请求过程如图:
在这里插入图片描述

客户端程序->客户端Stub程序 ->通信模块 -> 远程请求 -> 通信模块 -> 调度程序 -> 服务器端Stub程序 -> 服务程序
**

实例代码:

**

package org.example.RpcTest;

import org.apache.hadoop.ipc.VersionedProtocol;

import java.io.IOException;

public interface MethodProtocol extends VersionedProtocol {
    public static final  long versionID=1L;
    int calculate(int v1,int v2) throws IOException;
}

package org.example.RpcTest;

import org.apache.hadoop.ipc.ProtocolSignature;

import java.io.IOException;

public class MethodProtocolImpl implements MethodProtocol {

    public long getProtocolVersion(String protocol,long clientVersion){
        return MethodProtocol.versionID;
    }

    public ProtocolSignature getProtocolSignature(String protocol, long clientVersion, int hashcode){
        return new ProtocolSignature(MethodProtocol.versionID,null);
    }
    @Override
    public int calculate(int v1, int v2) throws IOException {
        return ((v1+v2)/2);
    }
}

package org.example.RpcTest;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.ipc.RPC;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Arrays;

public class RpcClient {
    public static void main(String[] args) throws IOException {
        MethodProtocol proxy;
        InetSocketAddress addr = new InetSocketAddress("localhost",8866);
        Configuration conf = new Configuration();
        proxy = (MethodProtocol) RPC.getProxy(MethodProtocol.class,MethodProtocol.versionID,addr,conf);
        int result = proxy.calculate(7,7);
        System.out.println(result);
    }
}


package org.example.RpcTest;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.ipc.RPC;
import org.apache.hadoop.ipc.Server;

import java.io.IOException;
import java.util.Arrays;

public class RpcServer {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        Server server = null;
        try {
            server = new RPC.Builder(conf).setProtocol(MethodProtocol.class).setInstance(new MethodProtocolImpl()).setBindAddress("localhost")
                    .setPort(8866).setNumHandlers(5).build();
        } catch (IOException e) {
            e.printStackTrace();
        }
        server.start();
        System.out.println("server begin .....");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/876660
推荐阅读
相关标签
  

闽ICP备14008679号