一個RPC的demo(good)
從下面的例子中可以看到,Consumer(client)的程式碼中引用了Provider部分的class,本例中是
com.provider.EchoServiceImpl和com.provider.EchoService
即這些class在Consumer(client)和Provider(server)都需要部署
Provider:
package com.provider; public interface EchoService { String echo(String msg); }
package com.provider; public class EchoServiceImpl implements EchoService { @Override public String echo(String msg) { return msg != null ? msg + "-->I am OK." : " pass null."; } }
package com.provider; 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 RpcProvider { private static Executor executor = Executors.newFixedThreadPool(20); public static void provide(String host, int port) throws IOException { ServerSocket serverSocket = new ServerSocket(); serverSocket.bind(new InetSocketAddress(host, port)); try { while (true) { System.out.println("one input coming"); executor.execute(new ProviderTask(serverSocket.accept())); System.out.println("execute one input"); } } finally { serverSocket.close(); } } private static class ProviderTask implements Runnable { private Socket socket; public ProviderTask(Socket socket) { this.socket = socket; } @Override public void run() { ObjectInputStream inputStream = null; ObjectOutputStream outputStream = null; try { inputStream = new ObjectInputStream(socket.getInputStream()); String className = inputStream.readUTF(); Class<?> service = Class.forName(className); String methodName = inputStream.readUTF(); Class<?>[] parameterTypes = (Class<?>[]) inputStream.readObject(); Object[] arguments = (Object[]) inputStream.readObject(); Method method = service.getMethod(methodName, parameterTypes); Object result = method.invoke(service.newInstance(), arguments); outputStream = new ObjectOutputStream(socket.getOutputStream()); outputStream.writeObject(result); } catch (Exception e) { e.printStackTrace(); } finally { close(inputStream); close(outputStream); close(socket); } } } public static void close(AutoCloseable closeable) { if (closeable != null) { try { closeable.close(); } catch (Exception e) { e.printStackTrace(); } } } }
package com.provider; import java.io.IOException; public class ProviderMain { public static void main(String[] args) { try { RpcProvider.provide("localhost", 8088); } catch (IOException e) { e.printStackTrace(); } } }
Consumer:
package com.consumer; import com.provider.RpcProvider; 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 RpcConsumer { public Object consume(Class<?> echoServiceClass, InetSocketAddress socketAddress) { return Proxy.newProxyInstance(echoServiceClass.getClassLoader(), new Class<?>[]{echoServiceClass.getInterfaces()[0]}, new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Socket socket = null; ObjectOutputStream outputStream = null; ObjectInputStream inputStream = null; try { socket = new Socket(); socket.connect(socketAddress); outputStream = new ObjectOutputStream(socket.getOutputStream()); outputStream.writeUTF(echoServiceClass.getName()); outputStream.writeUTF(method.getName()); outputStream.writeObject(method.getParameterTypes()); outputStream.writeObject(args); inputStream = new ObjectInputStream(socket.getInputStream()); return inputStream.readObject(); } finally { RpcProvider.close(outputStream); RpcProvider.close(inputStream); RpcProvider.close(socket); } } }); } }
package com.consumer; import com.provider.EchoService; import com.provider.EchoServiceImpl; import java.net.InetSocketAddress; public class ConsumerMain { public static void main(String[] args) { RpcConsumer rpcConsumer = new RpcConsumer(); EchoService echoService = (EchoService) rpcConsumer.consume(EchoServiceImpl.class, new InetSocketAddress("localhost", 8088)); System.out.println(echoService.echo(ConsumerMain.class.getCanonicalName())); } }
相關文章
- 一個clean框架的demo框架
- 關於Sqlite的一個demoSQLite
- 一個spark清洗資料的demoSpark
- 一個vue的電影資訊demoVue
- 寫一個RPC服務RPC
- 徒手擼一個簡單的RPC框架RPC框架
- 一個輕量級RPC的實現RPC
- 實現一個迷你版的RPCRPC
- 【JavaScript】第一個Demo和一個問題JavaScript
- 一個極簡版本的 VUE SSR demoVue
- 寫一個簡單的demo理解vuexVue
- Android中Service的一個Demo例子Android
- 以上程式碼使用good分隔字串生成一個陣列。Go字串陣列
- Flutter 從一個demo開始Flutter
- rails跑通第一個demoAI
- 分享自己做的一個JAVA RPC 框架 longioJavaRPC框架
- 一個基於Android的MVP框架DemoAndroidMVP框架
- 一個最簡單的WebSocket hello world demoWeb
- 關於SSM框架的一個簡單DemoSSM框架
- 一個簡單的介面測試框架 demo框架
- 如何設計一個RPC系統?RPC
- 設計一個分散式RPC框架分散式RPC框架
- 如何設計一個 RPC 系統RPC
- 仿照dubbo手寫一個RPC框架RPC框架
- 如何設計一個RPC系統RPC
- 實現一個requirejs原型demoUIJS原型
- GOODGo
- 遊戲開發者怎麼做出一個成功的demo?遊戲開發
- 我的第一個Flutter 應用Demo AireportFlutterAI
- 從零開始實現一個RPC框架(一)RPC框架
- 如何實現一個分散式RPC框架分散式RPC框架
- Twirp:一個很酷的基於Go的新RPC框架GoRPC框架
- 自己用 Netty 實現一個簡單的 RPCNettyRPC
- 原生Js的三個demoJS
- 開源一個最近寫的spring與mongodb結合的demo(spring-mongodb-demo)SpringMongoDB
- demo演示如何寫一個無配置格式統一的日誌
- 從一個小Demo看React的diff演算法React演算法
- 基於ThinkPHP5.0+GatewayWorker寫的一個聊天DEMOPHPGateway