一个最简单的 RPC 程序

一个最简单的 RPC 程序.

[b]Service 接口[/b]

public interface Service {
String sayHello(String name);

String sayJoke();
}


[b]Service 实现类[/b]

public class ServiceImpl implements Service {
@Override
public String sayHello(String name) {
return "Hello " + name;
}

@Override
public String sayJoke() {
return "一姐姐半夜打黑车回家,开了后门后,想还是记下车牌吧,于是关门绕到车位看了一眼,结果车开跑了。姐姐心想司机心虚了,还好没坐这车。于是继续等,十分钟后,那黑车又绕回来停在姐姐身边摇下车窗说:你怎么没上来啊,我听关车门以为你上来了,问你去哪儿,一回头没人,这大半夜的吓死我了...(转)";
}
}


[b]Server 端[/b]

@SuppressWarnings("rawtypes")
public class Server {

private final Logger logger = LoggerFactory.getLogger(Server.class);

private final Map<Class, Object> serviceMap = new ConcurrentHashMap<>();

public void start(int port) {
ServerRunnable pr = new ServerRunnable(port);
pr.start();
logger.info("Publisher start!");
}

public <T> void publishService(Class<T> service, T impl) {
serviceMap.put(service, impl);
}

private class ServerRunnable extends Thread {

private int port;
private ServerSocket ss = null;

public ServerRunnable(int port) {
this.port = port;
}

@Override
public void run() {

try {
ss = new ServerSocket(port);
} catch (IOException e) {
logger.error("", e);
}

while (true) {
Socket s = null;
try {
logger.info("accept");
s = ss.accept();
} catch (IOException e) {
logger.error("", e);
}

ServerHandler handler = new ServerHandler(s, serviceMap);
handler.start();
}
}
}

private class ServerHandler extends Thread {

private Socket s;

public ServerHandler(Socket s, Map<Class, Object> serviceMap) {
this.s = s;
}

@Override
public void run() {

ObjectInputStream ois = null;
ObjectOutputStream oos = null;
try {
// 主要的逻辑在这了,
ois = new ObjectInputStream(s.getInputStream());
Class service = (Class) ois.readObject();
Object serviceImpl = serviceMap.get(service);
if (serviceImpl != null) {
String methodName = ois.readUTF();
Class[] parameterTypes = (Class[]) ois.readObject();
Object[] args = (Object[]) ois.readObject();

Method method = serviceImpl.getClass().getMethod(methodName, parameterTypes);
Object result = method.invoke(serviceImpl, args);

oos = new ObjectOutputStream(s.getOutputStream());
oos.writeObject(result);
oos.flush();
logger.info("RPC handle finish");
} else {
logger.warn("Service implement not found!");
}

} catch (Exception e) {
logger.error("", e);
} finally {
if (ois != null) {
try {
ois.close();
} catch (IOException e) {
}
}

if (oos != null) {
try {
oos.close();
} catch (IOException e) {
}
}

if (s != null) {
try {
s.close();
} catch (IOException e) {
}
}
}
}
}

public static void main(String[] args) {
Server server = new Server();
server.start(10001);
server.publishService(Service.class, new ServiceImpl());
}
}



[b]Client 端[/b]


public class Client {

private static final Logger logger = LoggerFactory.getLogger(Client.class);

@SuppressWarnings("unchecked")
public <T> T getProxy(final String host, final int port, Class<T> service) {

InvocationHandlerImpl<T> handler = new InvocationHandlerImpl<T>(service, host, port);
Object proxy = Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[] { Service.class }, handler);

return (T) proxy;
}

private class InvocationHandlerImpl<T> implements InvocationHandler {

private Class<T> service;
private String host;
private int port;

public InvocationHandlerImpl(Class<T> service, String host, int port) {
super();
this.service = service;
this.host = host;
this.port = port;
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Socket s = null;
ObjectInputStream ois = null;
ObjectOutputStream oos = null;
try {
s = new Socket(host, port);
oos = new ObjectOutputStream(s.getOutputStream());
oos.writeObject(service);
oos.writeUTF(method.getName());
oos.writeObject(method.getParameterTypes());
oos.writeObject(args);
oos.flush();

ois = new ObjectInputStream(s.getInputStream());
return ois.readObject();
} catch (Exception e) {
logger.error("", e);
return null;
} finally {
if (ois != null) {
try {
ois.close();
} catch (IOException e) {
}
}
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
}
}
if (s != null) {
try {
s.close();
} catch (IOException e) {
}
}
}
}
}

public static void main(String[] args) {
Client c = new Client();
Service service = c.getProxy("localhost", 10001, Service.class);

System.out.println( service.sayHello("World"));
System.out.println( service.sayJoke());

}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值