diff --git a/07rpc/rpc01/rpcfx-core/pom.xml b/07rpc/rpc01/rpcfx-core/pom.xml index 8f11dc5d..b211dd7f 100644 --- a/07rpc/rpc01/rpcfx-core/pom.xml +++ b/07rpc/rpc01/rpcfx-core/pom.xml @@ -24,6 +24,28 @@ 1.2.70 + + + com.thoughtworks.xstream + xstream + 1.4.14 + + + + + org.codehaus.jettison + jettison + 1.4.0 + + + + + io.netty + netty-all + 4.1.56.Final + + + com.squareup.okhttp3 okhttp diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/RpcfxReflectionResolver.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/RpcfxReflectionResolver.java new file mode 100644 index 00000000..ea884ad2 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/RpcfxReflectionResolver.java @@ -0,0 +1,7 @@ +package io.kimmking.rpcfx.api; + +public interface RpcfxReflectionResolver { + + T resolve(String serviceClass); + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/client/NettyClient.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/client/NettyClient.java new file mode 100644 index 00000000..a33b1000 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/client/NettyClient.java @@ -0,0 +1,57 @@ +package io.kimmking.rpcfx.client; + +import io.kimmking.rpcfx.handler.ClientHandler; +import io.netty.bootstrap.Bootstrap; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.nio.NioSocketChannel; +import io.netty.util.CharsetUtil; + +public class NettyClient { + + private final String host; + + private final Integer port; + + private final String req; + + + public NettyClient(String host, Integer port, String req) { + this.host = host; + this.port = port; + this.req = req; + } + + public String start() throws InterruptedException { + EventLoopGroup eventLoopGroup = new NioEventLoopGroup(); + String resp; + try { + Bootstrap bootstrap = new Bootstrap(); + bootstrap. + group(eventLoopGroup). + channel(NioSocketChannel.class). + remoteAddress(host, port). + handler(new ChannelInitializer() { + @Override + protected void initChannel(SocketChannel socketChannel) throws Exception { + socketChannel.pipeline().addLast(new ClientHandler(req)); + } + }); + ChannelFuture future = bootstrap.connect().sync(); + resp = future.channel().alloc().buffer().toString(CharsetUtil.UTF_8); + System.out.println(resp); + future.channel().closeFuture().sync(); + } finally { + eventLoopGroup.shutdownGracefully(); + } + return resp; + } + + public static void main(String[] args) throws InterruptedException { + new NettyClient("127.0.0.1", 8888, "hello netty").start(); + } + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/client/Rpcfx.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/client/Rpcfx.java index 3d7b3788..c1d0fdd1 100644 --- a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/client/Rpcfx.java +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/client/Rpcfx.java @@ -3,41 +3,72 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.parser.ParserConfig; -import io.kimmking.rpcfx.api.RpcfxRequest; -import io.kimmking.rpcfx.api.RpcfxResponse; +import com.thoughtworks.xstream.XStream; +import io.kimmking.rpcfx.exception.RpcfxException; +import io.kimmking.rpcfx.param.RpcfxRequest; +import io.kimmking.rpcfx.param.RpcfxResponse; +import io.kimmking.rpcfx.utils.ClientUtils; +import io.kimmking.rpcfx.utils.XStreamUtils; import okhttp3.MediaType; -import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; +import org.springframework.cglib.proxy.Enhancer; +import org.springframework.cglib.proxy.MethodInterceptor; +import org.springframework.cglib.proxy.MethodProxy; import java.io.IOException; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; -import java.lang.reflect.Proxy; public final class Rpcfx { static { - ParserConfig.getGlobalInstance().addAccept("io.kimmking"); + ParserConfig parserConfig = ParserConfig.getGlobalInstance(); + parserConfig.addAccept("io.kimmking"); + parserConfig.setAutoTypeSupport(true); } public static T create(final Class serviceClass, final String url) { + //Gglib方式 + Enhancer enhancer = new Enhancer(); + enhancer.setCallback(new RpcfxInvocationHandler(serviceClass, url)); + enhancer.setSuperclass(serviceClass); + return (T) enhancer.create(); + // 0. 替换动态代理 -> AOP + //return (T) Proxy.newProxyInstance(Rpcfx.class.getClassLoader(), new Class[]{serviceClass}, new RpcfxInvocationHandler(serviceClass, url)); + + } + public static T create(final Class serviceClass, final String host, final Integer port) { + //Gglib方式 + Enhancer enhancer = new Enhancer(); + //enhancer.setCallback(new RpcfxInvocationHandler(serviceClass, host, port)); + enhancer.setSuperclass(serviceClass); + return (T) enhancer.create(); // 0. 替换动态代理 -> AOP - return (T) Proxy.newProxyInstance(Rpcfx.class.getClassLoader(), new Class[]{serviceClass}, new RpcfxInvocationHandler(serviceClass, url)); + //return (T) Proxy.newProxyInstance(Rpcfx.class.getClassLoader(), new Class[]{serviceClass}, new RpcfxInvocationHandler(serviceClass, url)); } - public static class RpcfxInvocationHandler implements InvocationHandler { + public static class RpcfxInvocationHandler implements InvocationHandler, MethodInterceptor { public static final MediaType JSONTYPE = MediaType.get("application/json; charset=utf-8"); private final Class serviceClass; private final String url; +// private final String host; +// private final Integer port; + private final XStream stream = XStreamUtils.createToJson(); + public RpcfxInvocationHandler(Class serviceClass, String url) { this.serviceClass = serviceClass; this.url = url; } +// public RpcfxInvocationHandler(Class serviceClass, String host, Integer port) { +// this.serviceClass = serviceClass; +// this.host = host; +// this.port = port; +// } // 可以尝试,自己去写对象序列化,二进制还是文本的,,,rpcfx是xml自定义序列化、反序列化,json: code.google.com/p/rpcfx // int byte char float double long bool @@ -45,33 +76,40 @@ public RpcfxInvocationHandler(Class serviceClass, String url) { @Override public Object invoke(Object proxy, Method method, Object[] params) throws Throwable { - RpcfxRequest request = new RpcfxRequest(); - request.setServiceClass(this.serviceClass.getName()); - request.setMethod(method.getName()); - request.setParams(params); - - RpcfxResponse response = post(request, url); - - // 这里判断response.status,处理异常 - // 考虑封装一个全局的RpcfxException - - return JSON.parse(response.getResult().toString()); + return post(method, params, url); } - private RpcfxResponse post(RpcfxRequest req, String url) throws IOException { - String reqJson = JSON.toJSONString(req); - System.out.println("req json: "+reqJson); + private Object post(Method method, Object[] params, String url) throws IOException, RpcfxException, InterruptedException { + RpcfxRequest rpcfxRequest = new RpcfxRequest(); + rpcfxRequest.setServiceClass(this.serviceClass.getName()); + rpcfxRequest.setMethod(method.getName()); + rpcfxRequest.setParams(params); + String reqJson = JSON.toJSONString(rpcfxRequest); + System.out.println("req json: " + reqJson); // 1.可以复用client // 2.尝试使用httpclient或者netty client - OkHttpClient client = new OkHttpClient(); + //OkHttpClient client = new OkHttpClient(); final Request request = new Request.Builder() .url(url) .post(RequestBody.create(JSONTYPE, reqJson)) .build(); - String respJson = client.newCall(request).execute().body().string(); - System.out.println("resp json: "+respJson); - return JSON.parseObject(respJson, RpcfxResponse.class); + String respJson = ClientUtils.execute(request).body().string(); + /*NettyClientUtils nettyClientUtils = new NettyClientUtils(host, port, reqJson); + String respJson = nettyClientUtils.start();*/ + System.out.println("resp json: " + respJson); + RpcfxResponse response = JSON.parseObject(respJson, RpcfxResponse.class); + // 这里判断response.status,处理异常 + // 考虑封装一个全局的RpcfxException + if (!response.isStatus()) { + throw new RpcfxException("invoke error", response.getException()); + } + return XStreamUtils.fromBean(stream, response.getResult().toString()); + } + + @Override + public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { + return post(method, objects, url); } } } diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/exception/RpcfxException.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/exception/RpcfxException.java new file mode 100644 index 00000000..b17f35fd --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/exception/RpcfxException.java @@ -0,0 +1,21 @@ +package io.kimmking.rpcfx.exception; + +public class RpcfxException extends Exception { + + public RpcfxException() { + super(); + } + + public RpcfxException(Throwable throwable) { + super(throwable); + } + + public RpcfxException(String message) { + super(message); + } + + public RpcfxException(String message, Throwable throwable) { + super(message, throwable); + } + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/handler/ClientHandler.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/handler/ClientHandler.java new file mode 100644 index 00000000..bc504dba --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/handler/ClientHandler.java @@ -0,0 +1,36 @@ +package io.kimmking.rpcfx.handler; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.SimpleChannelInboundHandler; +import io.netty.util.CharsetUtil; + +@ChannelHandler.Sharable +public class ClientHandler extends SimpleChannelInboundHandler { + + private final String req; + + public ClientHandler(String req) { + this.req = req; + } + + @Override + protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) throws Exception { + + System.out.println("Client receiver:" + byteBuf.toString(CharsetUtil.UTF_8)); + + } + + @Override + public void channelActive(ChannelHandlerContext ctx) throws Exception { + ctx.writeAndFlush(Unpooled.copiedBuffer(req, CharsetUtil.UTF_8)); + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + cause.printStackTrace(); + ctx.close(); + } +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/handler/ServerHandler.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/handler/ServerHandler.java new file mode 100644 index 00000000..b8928c1b --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/handler/ServerHandler.java @@ -0,0 +1,35 @@ +package io.kimmking.rpcfx.handler; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.channel.*; +import io.netty.util.CharsetUtil; + +@ChannelHandler.Sharable +public class ServerHandler extends ChannelInboundHandlerAdapter { + + /** + * 读取消息,写到回给发送者 + * @param ctx + * @param msg + * @throws Exception + */ + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + ByteBuf buf = (ByteBuf) msg; + System.out.println("Server receiver:" + buf.toString(CharsetUtil.UTF_8)); + ctx.write(buf); + } + + + @Override + public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { + ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + cause.printStackTrace(); + ctx.close(); + } +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/RpcfxRequest.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/param/RpcfxRequest.java similarity index 94% rename from 07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/RpcfxRequest.java rename to 07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/param/RpcfxRequest.java index 3a4de089..5e8f12a3 100644 --- a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/RpcfxRequest.java +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/param/RpcfxRequest.java @@ -1,4 +1,4 @@ -package io.kimmking.rpcfx.api; +package io.kimmking.rpcfx.param; public class RpcfxRequest { diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/RpcfxResponse.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/param/RpcfxResponse.java similarity index 94% rename from 07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/RpcfxResponse.java rename to 07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/param/RpcfxResponse.java index c7d82a18..ffd0dbcd 100644 --- a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/RpcfxResponse.java +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/param/RpcfxResponse.java @@ -1,4 +1,4 @@ -package io.kimmking.rpcfx.api; +package io.kimmking.rpcfx.param; public class RpcfxResponse { diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/server/NettyServer.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/server/NettyServer.java new file mode 100644 index 00000000..6bc6a9b3 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/server/NettyServer.java @@ -0,0 +1,49 @@ +package io.kimmking.rpcfx.server; + +import io.kimmking.rpcfx.handler.ServerHandler; +import io.netty.bootstrap.ServerBootstrap; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.nio.NioServerSocketChannel; + +public class NettyServer { + + private final Integer port; + + public NettyServer(Integer port) { + this.port = port; + } + + public void start() { + + EventLoopGroup group = new NioEventLoopGroup(); + try { + ServerBootstrap bootstrap = new ServerBootstrap(); + bootstrap.group(group). + channel(NioServerSocketChannel.class). + localAddress(port). + childHandler(new ChannelInitializer() { + @Override + protected void initChannel(SocketChannel socketChannel) throws Exception { + socketChannel.pipeline().addLast(new ServerHandler()); + } + }); + ChannelFuture future = bootstrap.bind().sync(); + future.channel().closeFuture().sync(); + } catch (InterruptedException e) { + e.printStackTrace(); + } finally { + group.shutdownGracefully(); + } + + + } + + public static void main(String[] args) { + new NettyServer(8888).start(); + } + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/server/RpcfxInvoker.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/server/RpcfxInvoker.java index a6f77dac..ec759ba9 100644 --- a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/server/RpcfxInvoker.java +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/server/RpcfxInvoker.java @@ -2,9 +2,14 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.SerializerFeature; -import io.kimmking.rpcfx.api.RpcfxRequest; +import com.thoughtworks.xstream.XStream; +import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver; +import io.kimmking.rpcfx.api.RpcfxReflectionResolver; +import io.kimmking.rpcfx.exception.RpcfxException; +import io.kimmking.rpcfx.param.RpcfxRequest; import io.kimmking.rpcfx.api.RpcfxResolver; -import io.kimmking.rpcfx.api.RpcfxResponse; +import io.kimmking.rpcfx.param.RpcfxResponse; +import io.kimmking.rpcfx.utils.XStreamUtils; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -12,10 +17,15 @@ public class RpcfxInvoker { - private RpcfxResolver resolver; +// private RpcfxResolver resolver; +// +// public RpcfxInvoker(RpcfxResolver resolver){ +// this.resolver = resolver; +// } + private RpcfxReflectionResolver reflectionResolver; - public RpcfxInvoker(RpcfxResolver resolver){ - this.resolver = resolver; + public RpcfxInvoker(RpcfxReflectionResolver reflectionResolver) { + this.reflectionResolver = reflectionResolver; } public RpcfxResponse invoke(RpcfxRequest request) { @@ -23,13 +33,16 @@ public RpcfxResponse invoke(RpcfxRequest request) { String serviceClass = request.getServiceClass(); // 作业1:改成泛型和反射 - Object service = resolver.resolve(serviceClass);//this.applicationContext.getBean(serviceClass); + //Object service = resolver.resolve(serviceClass);//this.applicationContext.getBean(serviceClass); + Object service = reflectionResolver.resolve(serviceClass); try { Method method = resolveMethodFromClass(service.getClass(), request.getMethod()); Object result = method.invoke(service, request.getParams()); // dubbo, fastjson, // 两次json序列化能否合并成一个 - response.setResult(JSON.toJSONString(result, SerializerFeature.WriteClassName)); + //response.setResult(JSON.toJSONString(result, SerializerFeature.WriteClassName)); + XStream stream = XStreamUtils.createToJson(); + response.setResult(XStreamUtils.to(stream, result)); response.setStatus(true); return response; } catch ( IllegalAccessException | InvocationTargetException e) { @@ -39,7 +52,7 @@ public RpcfxResponse invoke(RpcfxRequest request) { // 2.封装一个统一的RpcfxException // 客户端也需要判断异常 e.printStackTrace(); - response.setException(e); + response.setException(new RpcfxException(e)); response.setStatus(false); return response; } diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/utils/ClientUtils.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/utils/ClientUtils.java new file mode 100644 index 00000000..8b3e451a --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/utils/ClientUtils.java @@ -0,0 +1,17 @@ +package io.kimmking.rpcfx.utils; + +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; + +import java.io.IOException; + +public class ClientUtils { + + private static final OkHttpClient client = new OkHttpClient(); + + public static Response execute(Request request) throws IOException { + return client.newCall(request).execute(); + } + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/utils/XStreamUtils.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/utils/XStreamUtils.java new file mode 100644 index 00000000..4473e2a7 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/utils/XStreamUtils.java @@ -0,0 +1,36 @@ +package io.kimmking.rpcfx.utils; + +import com.thoughtworks.xstream.XStream; +import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver; + +public class XStreamUtils { + + private static XStream stream; + + public static XStream createToJson() { + stream = new XStream(new JettisonMappedXmlDriver()); + stream.setMode(XStream.NO_REFERENCES); + return stream; + } + + public static T fromBean(XStream stream, String str) { + if (null != stream) { + return (T) stream.fromXML(str); + } + return null; + } + + public static String to(XStream stream, Object obj) { + if (null != stream) { + return stream.toXML(obj); + } + return null; + } + + + public static XStream createToXml() { + stream = new XStream(); + return stream; + } + +} diff --git a/07rpc/rpc01/rpcfx-demo-consumer/src/main/java/io/kimmking/rpcfx/demo/consumer/RpcfxClientApplication.java b/07rpc/rpc01/rpcfx-demo-consumer/src/main/java/io/kimmking/rpcfx/demo/consumer/RpcfxClientApplication.java index 17537bb9..c641cc55 100644 --- a/07rpc/rpc01/rpcfx-demo-consumer/src/main/java/io/kimmking/rpcfx/demo/consumer/RpcfxClientApplication.java +++ b/07rpc/rpc01/rpcfx-demo-consumer/src/main/java/io/kimmking/rpcfx/demo/consumer/RpcfxClientApplication.java @@ -21,10 +21,12 @@ public static void main(String[] args) { // service.findById UserService userService = Rpcfx.create(UserService.class, "http://localhost:8080/"); + //UserService userService = Rpcfx.create(UserService.class, "127.0.0.1", 8080); User user = userService.findById(1); System.out.println("find user id=1 from server: " + user.getName()); OrderService orderService = Rpcfx.create(OrderService.class, "http://localhost:8080/"); + //OrderService orderService = Rpcfx.create(OrderService.class, "127.0.0.1", 8080); Order order = orderService.findOrderById(1992129); System.out.println(String.format("find order name=%s, amount=%f",order.getName(),order.getAmount())); diff --git a/07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/OrderServiceImpl.java b/07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/OrderServiceImpl.java index 39821952..d0c48277 100644 --- a/07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/OrderServiceImpl.java +++ b/07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/OrderServiceImpl.java @@ -3,6 +3,7 @@ import io.kimmking.rpcfx.demo.api.Order; import io.kimmking.rpcfx.demo.api.OrderService; + public class OrderServiceImpl implements OrderService { @Override diff --git a/07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/ReflectionResolver.java b/07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/ReflectionResolver.java new file mode 100644 index 00000000..3dab158c --- /dev/null +++ b/07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/ReflectionResolver.java @@ -0,0 +1,30 @@ +package io.kimmking.rpcfx.demo.provider; + +import io.kimmking.rpcfx.api.RpcfxReflectionResolver; +import org.springframework.beans.BeansException; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; + +import java.lang.reflect.InvocationTargetException; + +public class ReflectionResolver implements RpcfxReflectionResolver, ApplicationContextAware { + + private ApplicationContext applicationContext; + + @Override + public T resolve(String serviceClass) { + T t = null; + try { + Class klass = Class.forName(serviceClass); + t = (T) applicationContext.getBean(klass); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + return t; + } + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + this.applicationContext = applicationContext; + } +} diff --git a/07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/RpcfxServerApplication.java b/07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/RpcfxServerApplication.java index 266618de..f5d64e50 100644 --- a/07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/RpcfxServerApplication.java +++ b/07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/RpcfxServerApplication.java @@ -1,8 +1,9 @@ package io.kimmking.rpcfx.demo.provider; -import io.kimmking.rpcfx.api.RpcfxRequest; +import io.kimmking.rpcfx.api.RpcfxReflectionResolver; +import io.kimmking.rpcfx.param.RpcfxRequest; import io.kimmking.rpcfx.api.RpcfxResolver; -import io.kimmking.rpcfx.api.RpcfxResponse; +import io.kimmking.rpcfx.param.RpcfxResponse; import io.kimmking.rpcfx.demo.api.OrderService; import io.kimmking.rpcfx.demo.api.UserService; import io.kimmking.rpcfx.server.RpcfxInvoker; @@ -31,15 +32,20 @@ public RpcfxResponse invoke(@RequestBody RpcfxRequest request) { } @Bean - public RpcfxInvoker createInvoker(@Autowired RpcfxResolver resolver){ + public RpcfxInvoker createInvoker(@Autowired RpcfxReflectionResolver resolver){ return new RpcfxInvoker(resolver); } @Bean - public RpcfxResolver createResolver(){ - return new DemoResolver(); + public RpcfxReflectionResolver createReflectionResolver() { + return new ReflectionResolver(); } +// @Bean +// public RpcfxResolver createResolver(){ +// return new DemoResolver(); +// } + // 能否去掉name // @Bean(name = "io.kimmking.rpcfx.demo.api.UserService")