1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
| public class RpcConsumer { public static void main(String [] args){ IRpcHelloService rpcHello = RpcProxy.create(IRpcHelloService.class); System.out.println(rpcHello.hello("Tom老师"));
IRpcService service = RpcProxy.create(IRpcService.class); System.out.println("8 + 2 = " + service.add(8, 2)); System.out.println("8 - 2 = " + service.sub(8, 2)); System.out.println("8 * 2 = " + service.mult(8, 2)); System.out.println("8 / 2 = " + service.div(8, 2)); } }
public class RpcProxy { public static <T> T create(Class<?> clazz){ MethodProxy proxy = new MethodProxy(clazz); Class<?> [] interfaces = clazz.isInterface() ? new Class[]{clazz} : clazz.getInterfaces(); T result = (T) Proxy.newProxyInstance(clazz.getClassLoader(),interfaces,proxy); return result; }
private static class MethodProxy implements InvocationHandler { private Class<?> clazz; public MethodProxy(Class<?> clazz){ this.clazz = clazz; }
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (Object.class.equals(method.getDeclaringClass())) { try { return method.invoke(this, args); } catch (Throwable t) { t.printStackTrace(); } } else { return rpcInvoke(proxy,method, args); } return null; }
public Object rpcInvoke(Object proxy,Method method,Object[] args){
InvokerProtocol msg = new InvokerProtocol(); msg.setClassName(this.clazz.getName()); msg.setMethodName(method.getName()); msg.setValues(args); msg.setParames(method.getParameterTypes());
final RpcProxyHandler consumerHandler = new RpcProxyHandler(); EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group) .channel(NioSocketChannel.class) .option(ChannelOption.TCP_NODELAY, true) .handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4)); pipeline.addLast("frameEncoder", new LengthFieldPrepender(4)); pipeline.addLast("encoder", new ObjectEncoder()); pipeline.addLast("decoder", new ObjectDecoder(Integer.MAX_VALUE, ClassResolvers.cacheDisabled(null))); pipeline.addLast("handler",consumerHandler); } });
ChannelFuture future = b.connect("localhost", 8080).sync(); future.channel().writeAndFlush(msg).sync(); future.channel().closeFuture().sync(); } catch(Exception e){ e.printStackTrace(); }finally { group.shutdownGracefully(); } return consumerHandler.getResponse(); }
} }
public class RpcProxyHandler extends ChannelInboundHandlerAdapter { private Object response; public Object getResponse() { return response; } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { response = msg; } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { System.out.println("client exception is general"); } }
|