手写简单版本的RPC
程序员文章站
2022-07-12 16:43:43
...
一、实现思路
基于socket与serverSocket网络编程
二、实现方案
1、rpc-server:
(1)、rpc-server-api
a.创建 ISayHellow
public interface ISayHellow {
void sayHellow();
}
b.创建 IStudyHard
public interface IStudyHard {
void studyHard();
}
c.创建 RpcRequest
public class RpcRequest implements Serializable{
private String className;
private String methodName;
private Object[] params;
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public String getMethodName() {
return methodName;
}
public void setMethodName(String methodName) {
this.methodName = methodName;
}
public Object[] getParams() {
return params;
}
public void setParams(Object[] params) {
this.params = params;
}
}
(2)、rpc-server-provider
a.创建SayHellowImpl
public class SayHellowImpl implements ISayHellow {
@Override
public void sayHellow() {
System.out.println("[rpc调用]:gpp,hellow!");
}
}
b.创建StudyHardImpl
public class StudyHardImpl implements IStudyHard {
@Override
public void studyHard() {
System.out.println("[rpc调用]:study study up!");
}
}
c.创建 RpcHandler
public class RpcHandler {
void doRpcRequest() throws IOException {
System.out.println("rpc调用开始-------");
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(80);
} catch (IOException e) {
e.printStackTrace();
}
while(true){
Socket socket = null;
try {
socket = serverSocket.accept();
} catch (IOException e) {
e.printStackTrace();
}
ObjectInputStream objectInputStream = null;
ObjectOutputStream objectOutputStream = null;
try {
objectInputStream = new ObjectInputStream(socket.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
RpcRequest rpcRequest = null;
try {
rpcRequest = (RpcRequest) objectInputStream.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
String className = rpcRequest.getClassName();
String methodName = rpcRequest.getMethodName();
Object[] params = rpcRequest.getParams();
Class <?> aClass = null;
try {
aClass = Class.forName(className);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Class[] types = new Class[params.length];
for (int i = 0; i <params.length ; i++) {
types[i] = params[i].getClass();
}
Method method = null;
try {
method = aClass.getMethod(methodName, types);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
try {
Object result = null;
try {
result = method.invoke(SayHellowImpl.class.newInstance(), params);
} catch (InstantiationException e) {
e.printStackTrace();
}
objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
objectOutputStream.writeObject(result);
objectOutputStream.flush();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}finally {
if(objectInputStream != null){
objectInputStream.close();
}
if(objectOutputStream!= null){
objectOutputStream.close();
}
System.out.println("rpc调用结束------");
}
}
}
}
d.创建服务端启动类 ServerApp
public class ServerApp
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
RpcHandler rpcHandler = new RpcHandler();
try {
rpcHandler.doRpcRequest();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2、rpc-client:
a. 创建 代理类 RpcProxy
public class RpcProxy implements InvocationHandler {
@Override
public Object invoke(Object proxy, Method method, Object[] args) {
RpcRequest rpcRequest = new RpcRequest();
rpcRequest.setClassName(method.getDeclaringClass().getName());
rpcRequest.setMethodName(method.getName());
rpcRequest.setParams(args);
Socket socket = null;
Object result=null;
try {
while(true){
socket = new Socket("localhost",8089);
if(null != socket) break;
}
} catch (IOException e) {
e.printStackTrace();
}
ObjectOutputStream objectOutputStream = null;
ObjectInputStream objectInputStream = null;
try {
objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
try {
objectOutputStream.writeObject(rpcRequest);
} catch (IOException e) {
e.printStackTrace();
}
try {
objectOutputStream.writeObject(rpcRequest);
objectOutputStream.flush();
objectInputStream = new ObjectInputStream(socket.getInputStream());
try {
result = objectInputStream.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(null != objectInputStream){
try {
objectInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null != objectOutputStream){
try {
objectOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
}
b.创建 访问代理客户端 RpcProxyClient
public class RpcProxyClient {
public <T> T clientProxy2(final Class<T> interfaceCls){
return (T)Proxy.newProxyInstance(interfaceCls.getClassLoader(),new Class<?>[]{interfaceCls},new RpcProxy());
}
}
c.创建客户端启动类 ClientApp
public class ClientApp
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
ISayHellow iSayHellow = new RpcProxyClient().clientProxy2(ISayHellow.class);
iSayHellow.sayHellow();
}
}
三.工程代码结构图