主要通过JAVA 原生的序列化,Socket通信 动态代理 反射机制,实现最简单的RPC框架。
由三部分组成
1)服务提供者, 运行在服务端,负责提供服务接口定义和服务实现类
2)服务发布者 运行在服务端,负责将本地服务发布成远程服务,供其他消费者调用
3) 本地服务代理 运行在RPC客户端,通过代理调用远程服务提供者,然后将结果进行封装返回给本地消费者
package com.rpc.server;
public interface EchoService {
String echo(String ping);
}
package com.rpc.server;
public class EchoServiceImpl implements EchoService{
@Override
public String echo(String ping) {
return ping == null?"I am ok":ping+"Iam ok";
}
}
package com.rpc.server;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Method;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
public class RpcExporter {
static Executor exe = Executors.newFixedThreadPool(
Runtime.getRuntime().availableProcessors());
public static void export(String hostname
,int port){
try {
ServerSocket serverSocket = new ServerSocket();
serverSocket.bind(new InetSocketAddress(hostname,port));
try{
while(true){
exe.execute(new ExportTask(serverSocket.accept()));
}
}finally{
serverSocket.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static class ExportTask implements Runnable{
Socket client = null;
public ExportTask (Socket c){
this.client = c;
}
@Override
public void run() {
ObjectInputStream input = null;
ObjectOutputStream out = null;
try {
input =new ObjectInputStream(client.getInputStream());
String interfaceName = input.readUTF();
Class<?> service = Class.forName(interfaceName);
String methodName = input.readUTF();
Class[] pType=(Class[])input.readObject();
Object[] arguments =(Object []) input.readObject();
Method method = service.getMethod(methodName, pType);
Object result = method.invoke(service.newInstance(), arguments);
out = new ObjectOutputStream(client.getOutputStream());
out.writeObject(result);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if(out != null){
out.close();
}
if(input != null){
input.close();
}
if(client != null){
client.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
package com.rpc.client;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.net.InetSocketAddress;
import java.net.Socket;
public class RpcImporter <S> {
public S importer(final Class<?> serviceClass, final
InetSocketAddress addr){
return (S)Proxy.newProxyInstance(serviceClass.getClassLoader(),
new Class[]{serviceClass.getInterfaces()[0]},
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Socket socket = null;
ObjectOutputStream output = null
;
ObjectInputStream input = null;
try{
socket = new Socket();
socket.connect(addr);
output = new ObjectOutputStream(socket.getOutputStream());
output.writeUTF(serviceClass.getName());
output.writeUTF(method.getName());
output.writeObject(method.getParameterTypes())
;
output.writeObject(args);
input = new ObjectInputStream(socket.getInputStream());
return input.readObject();
}finally{
if(socket != null){
socket.close();
}
if(output != null){
output.close();
}
if(input != null){
input.close();
}
}
}
});
}
}
package com.rpc.client;
import java.net.InetSocketAddress;
import com.rpc.server.EchoService;
import com.rpc.server.EchoServiceImpl;
import com.rpc.server.RpcExporter;
public class RpcTest {
public static void main(String[] args) {
new Thread(
new Runnable(){
@Override
public void run() {
RpcExporter.export("localhost", 8089);
}
}).start();
RpcImporter<EchoService> rpcImporter = new RpcImporter<EchoService>();
EchoService importer = rpcImporter.importer(EchoServiceImpl.class,
new InetSocketAddress("localhost",8089));
System.out.println(importer.echo("Are you ok?"));
}
}