From 620ef3862a99eec511891c7910da3b0d1f304dc3 Mon Sep 17 00:00:00 2001 From: zeekling Date: Sat, 28 Oct 2023 23:26:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0rpc=E8=B0=83=E7=94=A8?= =?UTF-8?q?=E6=A0=B7=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 8 +++++- .../java/com/zeekling/cn/rpc/HelloServer.java | 28 +++++++++++++++++++ .../java/com/zeekling/cn/rpc/RPCClient.java | 23 +++++++++++++++ .../java/com/zeekling/cn/rpc/RPCProtocol.java | 9 ++++++ 4 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/zeekling/cn/rpc/HelloServer.java create mode 100644 src/main/java/com/zeekling/cn/rpc/RPCClient.java create mode 100644 src/main/java/com/zeekling/cn/rpc/RPCProtocol.java 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); + +} -- 2.45.2