版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013278314/article/details/82292207
首先结构图:
rpc_api:
api里面实现的rpc调用(RpcFramework):
package com.th.rpc.framework;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.net.ServerSocket;
import java.net.Socket;
public class RpcFramework {
public static void export(final Object service, int port) throws IOException {
if (service == null)
throw new IllegalArgumentException("service instance == null");
if (port <= 0 || port > 65535) {
throw new IllegalArgumentException("Invalid port " + port);
}
System.out.println("Export service " + service.getClass().getName() + " on port " + port);
ServerSocket server = new ServerSocket(port);
for (;;) {
try {
final Socket socket = server.accept();
new Thread(new Runnable() {
@Override
public void run() {
try {
ObjectInputStream input = new ObjectInputStream(socket.getInputStream());
String methodName = input.readUTF();
System.out.println("methodName " + methodName);
Class<?>[] parameterTypes = (Class<?>[]) input.readObject();
Object[] arguments = (Object[]) input.readObject();
ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());
Method method = service.getClass().getMethod(methodName, parameterTypes);
Object result = method.invoke(service, arguments);
output.writeObject(result);
output.close();
input.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}).start();
} catch (Exception e) {
}
}
}
public static <T> T refer(final Class<T> interfaceClass, final String host, final int port) {
if (interfaceClass == null)
throw new IllegalArgumentException("Interface class == null");
if (!interfaceClass.isInterface())
throw new IllegalArgumentException("The " + interfaceClass.getName() + " must be interface class!");
if (host == null || host.length() == 0)
throw new IllegalArgumentException("Host == null!");
if (port <= 0 || port > 65535)
throw new IllegalArgumentException("Invalid port " + port);
T proxy = (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class<?>[] { interfaceClass }, new RpcReferHandler(host, 1234));
return proxy;
}
}
RpcReferHandler:
package com.th.rpc.framework;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.net.Socket;
public class RpcReferHandler implements InvocationHandler {
private String host;
private int port;
public RpcReferHandler(String host, int port) {
this.host = host;
this.port = port;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Socket socket = new Socket(host, port);
ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream input = null;
try {
output.writeUTF(method.getName());
System.out.println("methodName : " + method.getName());
output.writeObject(method.getParameterTypes());
output.writeObject(args);
input = new ObjectInputStream(socket.getInputStream());
Object result = input.readObject();
if (result instanceof Throwable)
throw (Throwable) result;
System.out.println(result);
return result;
} catch (Exception e) {
e.printStackTrace();
} finally {
input.close();
output.close();
socket.close();
}
return null;
}
}
公共抽象类 HelloService:
provider:
package com.th.rpc.provider;
import java.io.IOException;
import com.th.rpc.framework.RpcFramework;
import com.th.rpc.service.HelloService;
import com.th.rpc.service.impl.HelloServiceImpl;
public class RpcProvicer {
public static void main(String[] args) throws IOException {
HelloService service = new HelloServiceImpl();
RpcFramework.export(service, 1234);
}
}
HelloService实现类:
package com.th.rpc.service.impl;
import com.th.rpc.service.HelloService;
public class HelloServiceImpl implements HelloService {
@Override
public String hello(String name) {
return "Hello " + name;
}
@Override
public String hi(String msg) {
return "Hi " + msg;
}
}
consumer:
package com.th.rpc.consumer;
import com.th.rpc.framework.RpcFramework;
import com.th.rpc.service.HelloService;
public class RpcConsumer {
public static void main(String[] args) {
HelloService service = RpcFramework.refer(HelloService.class, "127.0.0.1", 1234);
String result = service.hello("World");
String hi = service.hi("小明");
System.out.println("client: " + result);
System.out.println("client: " + hi);
}
}
测试:
首先启动provider,然后启动consumer,测试结果如下: