一、WritableRpcEngine
- Interface IHello 服务接口类
package com.harry.hadoop_eco.hadoop.examples.common.rpc.writable_; import org.apache.hadoop.ipc.ProtocolInfo; import org.apache.hadoop.ipc.VersionedProtocol; /** * Created by harry on 2/2/18. */ @ProtocolInfo(protocolVersion = IPCServer.IPC_VER, protocolName = "IHello") public interface IHello extends VersionedProtocol{ public Result sayHi(String name); }
- Service HelloImpl 服务实现类
package com.harry.hadoop_eco.hadoop.examples.common.rpc.writable_; import org.apache.hadoop.ipc.ProtocolSignature; import java.io.IOException; /** * Created by harry on 2/2/18. */ public class HelloImpl implements IHello { public Result sayHi(String name) { return new Result("hello " + name); } public long getProtocolVersion(String protocol, long clientVersion) throws IOException { return IPCServer.IPC_VER; } public ProtocolSignature getProtocolSignature(String protocol, long clientVersion, int clientMethodsHash) throws IOException { return null; } }
- Result
-
package com.harry.hadoop_eco.hadoop.examples.common.rpc.writable_; import org.apache.hadoop.io.Writable; import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; /** * Created by harry on 2/2/18. */ public class Result implements Writable { private String result; public Result(){ } public Result(String result) { this.result = result; } public void write(DataOutput out) throws IOException { out.writeUTF(result); } public void readFields(DataInput in) throws IOException { result = in.readUTF(); } @Override public String toString() { return "Result{" + "result='" + result + '\'' + '}'; } }
-
- IPCServer
package com.harry.hadoop_eco.hadoop.examples.common.rpc.writable_; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.ipc.RPC; import java.io.IOException; /** * Created by harry on 2/2/18. */ public class IPCServer { public static final int IPC_PORT = 3331; public static final long IPC_VER = 1234L; public static void main(String[] args) throws IOException { HelloImpl hello = new HelloImpl(); RPC.Server server = new RPC.Builder(new Configuration()).setProtocol(IHello.class).setInstance(hello) .setBindAddress("0.0.0.0").setPort(IPC_PORT).setVerbose(true).build(); server.start(); System.out.println("Server ready, press any key to stop"); System.in.read(); server.stop(); } }
- IPCClient
package com.harry.hadoop_eco.hadoop.examples.common.rpc.writable_; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.ipc.RPC; import java.io.IOException; import java.net.InetSocketAddress; /** * Created by harry on 2/2/18. */ public class IPCClient { public static void main(String[] args) throws IOException { InetSocketAddress addr = new InetSocketAddress("localhost", IPCServer.IPC_PORT); Configuration configuration = new Configuration(); IHello hello = RPC.getProxy(IHello.class,IPCServer.IPC_VER,addr,configuration); System.out.println(hello.sayHi("world")); RPC.stopProxy(hello); } }
二、ProtobufRpcEngine
- ihello.proto
-
option java_package = "com.harry.hadoop_eco.hadoop.examples.common.rpc.protobuf_"; option java_outer_classname = "IHelloProtos"; option java_generate_equals_and_hash = true; message EmptyRequestProto{ } message EmptyResponseProto{ } message EchoRequestProto{ required string message = 1; } message EchoResponseProto{ required string message = 1; }
-
- ihello_rpc_server.proto
-
option java_package = "com.harry.hadoop_eco.hadoop.examples.common.rpc.protobuf_"; option java_outer_classname = "IHelloRpcServiceProtos"; option java_generic_services = true; option java_generate_equals_and_hash = true; import "test.proto"; service IHelloProtobufRpcProto{ rpc ping(EmptyRequestProto) returns (EmptyResponseProto); rpc echo(EchoRequestProto) returns (EchoResponseProto); }
-
- Protoc 生成
- cd 到文件所在目录
- protoc -I ./ --java_out=${指向xxx/src/main/java,也可以是任何目录,在copy到对应的目录} ./hello.proto
- protoc -i ./ --java_out=${} ./hello_rpc_server.proto
- IHello
-
package com.harry.hadoop_eco.hadoop.examples.common.rpc.protobuf_; import org.apache.hadoop.ipc.ProtocolInfo; /** * Created by harry on 2/2/18. */ @ProtocolInfo(protocolName = "IHelloProto",protocolVersion = 1) public interface IHello extends IHelloRpcServiceProtos.IHelloProtobufRpcProto.BlockingInterface { }
-
- HelloImpl
-
package com.harry.hadoop_eco.hadoop.examples.common.rpc.protobuf_; import com.google.protobuf.RpcController; import com.google.protobuf.ServiceException; /** * Created by harry on 2/2/18. */ public class HelloImpl implements IHello { public IHelloProtos.EmptyResponseProto ping(RpcController controller, IHelloProtos.EmptyRequestProto request) throws ServiceException { return IHelloProtos.EmptyResponseProto.newBuilder().build(); } public IHelloProtos.EchoResponseProto echo(RpcController controller, IHelloProtos.EchoRequestProto request) throws ServiceException { return IHelloProtos.EchoResponseProto.newBuilder().setMessage(request.getMessage()).build(); } }
-
- IPCClient
-
package com.harry.hadoop_eco.hadoop.examples.common.rpc.protobuf_; import com.google.protobuf.ServiceException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.ipc.ProtobufRpcEngine; import org.apache.hadoop.ipc.RPC; import java.io.IOException; import java.net.InetSocketAddress; /** * Created by harry on 2/2/18. */ public class IPCClient { public static void main(String[] args) throws IOException, ServiceException { Configuration conf = new Configuration(); RPC.setProtocolEngine(conf, IHello.class, ProtobufRpcEngine.class); IHello client = RPC.getProxy(IHello.class,0,new InetSocketAddress("127.0.0.1",3331),conf); IHelloProtos.EmptyRequestProto emptyRequest = IHelloProtos.EmptyRequestProto.newBuilder().build(); IHelloProtos.EmptyResponseProto emptyResponseProto = client.ping(null,emptyRequest); System.out.println(emptyResponseProto); IHelloProtos.EchoRequestProto echoRequest = IHelloProtos.EchoRequestProto.newBuilder().setMessage("Hello World, Protobuf").build(); IHelloProtos.EchoResponseProto echoResponse = client.echo(null,echoRequest); System.out.println(echoResponse); } }
-
- IPCServer
-
package com.harry.hadoop_eco.hadoop.examples.common.rpc.protobuf_; import com.google.protobuf.BlockingService; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.ipc.ProtobufRpcEngine; import org.apache.hadoop.ipc.RPC; import java.io.IOException; /** * Created by harry on 2/2/18. */ public class IPCServer { public static void main(String[] args) throws IOException { Configuration conf = new Configuration(); RPC.setProtocolEngine(conf, IHello.class, ProtobufRpcEngine.class); HelloImpl hello = new HelloImpl(); BlockingService service = IHelloRpcServiceProtos.IHelloProtobufRpcProto.newReflectiveBlockingService(hello); RPC.Server server = new RPC.Builder(conf).setProtocol(IHello.class).setInstance(service).setBindAddress("127.0.0.1").setPort(3331).build(); server.start(); } }
-