RPC入门案例(基于Hadoop中的类)
一、简介
RPC是远程过程调用(Remote Procedure Call)的缩写形式。SAP系统RPC调用的原理其实很简单,有一些类似于三层构架的C/S系统,第三方的客户程序通过接口调用SAP内部的标准或自定义函数,获得函数返回的数据进行处理后显示或打印。
二、入门案例
导入依赖
pom.xml
xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.rpc</groupId>
<artifactId>RPC-learning</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<useUniqueVersions>false</useUniqueVersions>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.rpc.server.TestServer</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>编写公共接口
javapackage com.rpc.api;
import java.rmi.Remote;
import java.rmi.RemoteException;
/**
* 公共接口
* @author BoWenWang
*/
public interface IRpc extends Remote {
/**
* 支付
* @param money
* @return
* @throws RemoteException
*/
String pay(double money) throws RemoteException;
}编写公共接口的实现类
javapackage com.rpc.api.impl;
import com.rpc.api.IRpc;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class RpcImpl extends UnicastRemoteObject implements IRpc {
private static final long serialVersionUID = 682805210518738166L;
public RpcImpl() throws RemoteException {
super();
}
public String pay(double money) throws RemoteException {
if (money > 1000) {
return "付款成功";
} else {
return "付款失败";
}
}
}编写测试服务端并启动
javapackage com.rpc.server;
import com.rpc.api.IRpc;
import com.rpc.api.impl.RpcImpl;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
/**
* 服务端
*/
public class RpcServer {
public static void main(String[] args) {
System.setProperty("java.rmi.server.hostname","127.0.0.1");
// 注册管理器
Registry registry = null;
try {
// 创建一个服务注册管理器
registry = LocateRegistry.createRegistry(30009);
} catch (RemoteException e) {
e.printStackTrace();
}
try {
// 创建一个服务
IRpc server = new RpcImpl();
// 将服务绑定命名
registry.rebind("payServer", server);
System.out.println("bind server payServer");
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
/**运行结果显示
bind server payServer
*/编写客户端进行通信
javapackage com.rpc.client;
import com.rpc.api.IRpc;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
/**
* 客户端(Web端Controller)
*/
public class RpcClient {
public static void main(String[] args) {
// 注册管理器
Registry registry = null;
try {
// 获取服务注册管理器
registry = LocateRegistry.getRegistry("127.0.0.1",30009);
// 列出所有注册的服务
String[] list = registry.list();
for(String s : list){
System.out.println(s);
}
} catch (RemoteException e) {
e.printStackTrace();
}
try {
// 根据命名获取服务
IRpc server = (IRpc) registry.lookup("payServer");
// 调用远程方法
String result = server.pay(10000);
// 输出调用结果
System.out.println("result from remote : " + result);
// 调用远程方法
String result2 = server.pay(100);
// 输出调用结果
System.out.println("result from remote2 : " + result2);
} catch (RemoteException | NotBoundException e) {
e.printStackTrace();
}
}
}
/**运行结果显示
payServer
result from remote : 付款成功
result from remote2 : 付款失败
*/