添加rpc调用样例 #3

Merged
zeekling merged 1 commits from rpcTest into main 2023-10-28 15:28:47 +00:00
4 changed files with 67 additions and 1 deletions
Showing only changes of commit 620ef3862a - Show all commits

View File

@ -7,7 +7,7 @@
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>test</name>
<name>java_test</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@ -23,6 +23,12 @@
<version>6.20.3</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>3.3.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>

View File

@ -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();
}
}

View File

@ -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);
}
}

View File

@ -0,0 +1,9 @@
package com.zeekling.cn.rpc;
public interface RPCProtocol {
long versionID = 666;
String hello(String msg);
}