添加rpc调用样例 #3
8
pom.xml
8
pom.xml
@ -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>
|
||||
|
28
src/main/java/com/zeekling/cn/rpc/HelloServer.java
Normal file
28
src/main/java/com/zeekling/cn/rpc/HelloServer.java
Normal 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();
|
||||
}
|
||||
}
|
23
src/main/java/com/zeekling/cn/rpc/RPCClient.java
Normal file
23
src/main/java/com/zeekling/cn/rpc/RPCClient.java
Normal 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);
|
||||
|
||||
}
|
||||
|
||||
}
|
9
src/main/java/com/zeekling/cn/rpc/RPCProtocol.java
Normal file
9
src/main/java/com/zeekling/cn/rpc/RPCProtocol.java
Normal file
@ -0,0 +1,9 @@
|
||||
package com.zeekling.cn.rpc;
|
||||
|
||||
public interface RPCProtocol {
|
||||
|
||||
long versionID = 666;
|
||||
|
||||
String hello(String msg);
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user