自定义rpc
一、服务端暴露的接口
package remote.procedure.call.server;
public interface HelloService {
public String sayHi(String name) ;
}
二、服务端功能接口实现类
package remote.procedure.call.server;
public class HelloServiceImpl implements HelloService {
@Override
public String sayHi(String name) {
System.out.println("你是傻逼!!!!!"+name);
return "hi,"+name ;
}
}
三、 服务端注册中心
(一)接口
package remote.procedure.call.server;
public interface Server {
public void start() ;
public void stop();
public void register(Class service,Class serviceImpl);
}
(二)实现类
input.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
四、客户端
package remote.procedure.call.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 Client {
@SuppressWarnings("unchecked")
public static <T> T getRemoteProxyObj(Class serviceInterface,
InetSocketAddress addr) {
return (T)Proxy.newProxyInstance(serviceInterface.getClassLoader(),
new Class<?>[] {
serviceInterface} , new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args)