diff --git a/pom.xml b/pom.xml index b24fb09..e1ff747 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ 1.0-SNAPSHOT jar - test + java_test UTF-8 @@ -23,6 +23,12 @@ 6.20.3 + + org.apache.hadoop + hadoop-common + 3.3.6 + + junit junit diff --git a/src/main/java/com/zeekling/cn/rpc/HelloServer.java b/src/main/java/com/zeekling/cn/rpc/HelloServer.java new file mode 100644 index 0000000..2cf0ab3 --- /dev/null +++ b/src/main/java/com/zeekling/cn/rpc/HelloServer.java @@ -0,0 +1,28 @@ +package com.zeekling.cn.rpc; + + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.ipc.RPC; +import org.apache.hadoop.ipc.Server; + +import java.io.IOException; + +public class HelloServer implements RPCProtocol { + + @Override + public String hello(String msg) { + System.out.println("msg=" + msg); + return "hello:" + msg; + } + + public static void main(String[] args) throws IOException { + Server server = new RPC.Builder(new Configuration()) + .setBindAddress("127.0.0.1") + .setPort(8888) + .setProtocol(RPCProtocol.class) + .setInstance(new HelloServer()) + .build(); + System.out.println("服务器启动"); + server.start(); + } +} diff --git a/src/main/java/com/zeekling/cn/rpc/RPCClient.java b/src/main/java/com/zeekling/cn/rpc/RPCClient.java new file mode 100644 index 0000000..58e0533 --- /dev/null +++ b/src/main/java/com/zeekling/cn/rpc/RPCClient.java @@ -0,0 +1,23 @@ +package com.zeekling.cn.rpc; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.ipc.RPC; + +import java.io.IOException; +import java.net.InetSocketAddress; + +public class RPCClient { + + public static void main(String[] args) throws IOException { + RPCProtocol client = RPC.getProxy( + RPCProtocol.class, + RPCProtocol.versionID, + new InetSocketAddress("127.0.0.1", 8888), + new Configuration()); + System.out.println("客户端"); + String hello = client.hello("I'm Client"); + System.out.println(hello); + + } + +} diff --git a/src/main/java/com/zeekling/cn/rpc/RPCProtocol.java b/src/main/java/com/zeekling/cn/rpc/RPCProtocol.java new file mode 100644 index 0000000..0b16e7d --- /dev/null +++ b/src/main/java/com/zeekling/cn/rpc/RPCProtocol.java @@ -0,0 +1,9 @@ +package com.zeekling.cn.rpc; + +public interface RPCProtocol { + + long versionID = 666; + + String hello(String msg); + +}