From 35a12acd05c4972d9a41bbed18886648273ddeba Mon Sep 17 00:00:00 2001 From: zenglinhui Date: Wed, 16 Dec 2020 01:29:51 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=88=90=E5=8F=8D?= =?UTF-8?q?=E5=B0=84=E5=92=8C=E6=B3=9B=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../rpcfx/api/RpcfxReflectionResolver.java | 7 +++++ .../java/io/kimmking/rpcfx/client/Rpcfx.java | 4 +-- .../rpcfx/{api => param}/RpcfxRequest.java | 2 +- .../rpcfx/{api => param}/RpcfxResponse.java | 2 +- .../kimmking/rpcfx/server/RpcfxInvoker.java | 15 +++++++--- .../rpcfx/demo/provider/OrderServiceImpl.java | 1 + .../demo/provider/ReflectionResolver.java | 28 +++++++++++++++++++ .../demo/provider/RpcfxServerApplication.java | 16 +++++++---- 8 files changed, 62 insertions(+), 13 deletions(-) create mode 100644 07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/RpcfxReflectionResolver.java rename 07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/{api => param}/RpcfxRequest.java (94%) rename 07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/{api => param}/RpcfxResponse.java (94%) create mode 100644 07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/ReflectionResolver.java 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/Rpcfx.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/client/Rpcfx.java index 3d7b3788..6d1d8d42 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,8 +3,8 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.parser.ParserConfig; -import io.kimmking.rpcfx.api.RpcfxRequest; -import io.kimmking.rpcfx.api.RpcfxResponse; +import io.kimmking.rpcfx.param.RpcfxRequest; +import io.kimmking.rpcfx.param.RpcfxResponse; import okhttp3.MediaType; import okhttp3.OkHttpClient; import okhttp3.Request; 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/RpcfxInvoker.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/server/RpcfxInvoker.java index a6f77dac..faa168fd 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,10 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.SerializerFeature; -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 java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -12,10 +13,15 @@ public class RpcfxInvoker { - private RpcfxResolver resolver; +/* private RpcfxResolver resolver; public RpcfxInvoker(RpcfxResolver resolver){ this.resolver = resolver; + }*/ + private RpcfxReflectionResolver reflectionResolver; + + public RpcfxInvoker(RpcfxReflectionResolver reflectionResolver) { + this.reflectionResolver = reflectionResolver; } public RpcfxResponse invoke(RpcfxRequest request) { @@ -23,7 +29,8 @@ 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()); 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..4483a980 --- /dev/null +++ b/07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/ReflectionResolver.java @@ -0,0 +1,28 @@ +package io.kimmking.rpcfx.demo.provider; + +import io.kimmking.rpcfx.api.RpcfxReflectionResolver; + +import java.lang.reflect.InvocationTargetException; + +public class ReflectionResolver implements RpcfxReflectionResolver { + @Override + public T resolve(String serviceClass) { + T t = null; + try { + Class klass = Class.forName(serviceClass); + t = (T) klass.getConstructor().newInstance(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + + return t; + } +} 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") From 56cca62679f43bf76a98e0bbef6e8417534b194a Mon Sep 17 00:00:00 2001 From: zenglinhui Date: Wed, 16 Dec 2020 12:48:20 +0800 Subject: [PATCH 2/7] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/kimmking/rpcfx/client/Rpcfx.java | 16 ++++++++++++---- .../rpcfx/exception/RpcfxException.java | 17 +++++++++++++++++ .../kimmking/rpcfx/server/RpcfxInvoker.java | 19 ++++++++++--------- .../demo/provider/RpcfxServerApplication.java | 16 ++++++++-------- 4 files changed, 47 insertions(+), 21 deletions(-) create mode 100644 07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/exception/RpcfxException.java 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 6d1d8d42..e82c2b86 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,6 +3,7 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.parser.ParserConfig; +import io.kimmking.rpcfx.exception.RpcfxException; import io.kimmking.rpcfx.param.RpcfxRequest; import io.kimmking.rpcfx.param.RpcfxResponse; import okhttp3.MediaType; @@ -18,7 +19,11 @@ public final class Rpcfx { static { - ParserConfig.getGlobalInstance().addAccept("io.kimmking"); + ParserConfig config = ParserConfig.getGlobalInstance(); + config.addAccept("io.kimmking"); + config.setAutoTypeSupport(true); + + //ParserConfig.getGlobalInstance().addAccept("io.kimmking"); } public static T create(final Class serviceClass, final String url) { @@ -51,11 +56,14 @@ public Object invoke(Object proxy, Method method, Object[] params) throws Throwa request.setParams(params); RpcfxResponse response = post(request, url); - + if (response.isStatus()) { + return response.getResult(); + } else { + RpcfxException e = (RpcfxException) response.getException(); + return null; + } // 这里判断response.status,处理异常 // 考虑封装一个全局的RpcfxException - - return JSON.parse(response.getResult().toString()); } private RpcfxResponse post(RpcfxRequest req, String url) throws IOException { 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..25ad0456 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/exception/RpcfxException.java @@ -0,0 +1,17 @@ +package io.kimmking.rpcfx.exception; + +public class RpcfxException extends Exception { + + public RpcfxException() { + super(); + } + + public RpcfxException(Throwable throwable) { + super(throwable); + } + + public RpcfxException(String exception) { + super(exception); + } + +} 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 faa168fd..58fefef0 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 @@ -3,6 +3,7 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.SerializerFeature; 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.param.RpcfxResponse; @@ -13,24 +14,24 @@ public class RpcfxInvoker { -/* private RpcfxResolver resolver; + private RpcfxResolver resolver; public RpcfxInvoker(RpcfxResolver resolver){ this.resolver = resolver; - }*/ - private RpcfxReflectionResolver reflectionResolver; - - public RpcfxInvoker(RpcfxReflectionResolver reflectionResolver) { - this.reflectionResolver = reflectionResolver; } + //private RpcfxReflectionResolver reflectionResolver; + +// public RpcfxInvoker(RpcfxReflectionResolver reflectionResolver) { +// this.reflectionResolver = reflectionResolver; +// } public RpcfxResponse invoke(RpcfxRequest request) { RpcfxResponse response = new RpcfxResponse(); String serviceClass = request.getServiceClass(); // 作业1:改成泛型和反射 - //Object service = resolver.resolve(serviceClass);//this.applicationContext.getBean(serviceClass); - Object service = reflectionResolver.resolve(serviceClass); + Object service = resolver.resolve(serviceClass);//this.applicationContext.getBean(serviceClass); + //Object service = reflectionResolver.resolve(serviceClass); try { Method method = resolveMethodFromClass(service.getClass(), request.getMethod()); @@ -46,7 +47,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-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 f5d64e50..1034cdce 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 @@ -32,20 +32,20 @@ public RpcfxResponse invoke(@RequestBody RpcfxRequest request) { } @Bean - public RpcfxInvoker createInvoker(@Autowired RpcfxReflectionResolver resolver){ + public RpcfxInvoker createInvoker(@Autowired RpcfxResolver resolver){ return new RpcfxInvoker(resolver); } - @Bean - public RpcfxReflectionResolver createReflectionResolver() { - return new ReflectionResolver(); - } - // @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") From 393dd9ed49aae0fcd3b93c9c5e1305eec742b3a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=BE=E6=9E=97=E8=BE=89?= Date: Wed, 16 Dec 2020 18:12:34 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=E5=91=A8=E5=85=AD=E7=AC=AC3=E9=A2=98:?= =?UTF-8?q?=E6=94=B9=E6=88=90=E6=B3=9B=E5=9E=8B=E5=92=8C=E5=8F=8D=E5=B0=84?= =?UTF-8?q?=EF=BC=8C=E5=A2=9E=E5=8A=A0=E5=85=A8=E5=B1=80=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E7=B1=BB=EF=BC=8C=E5=A4=84=E7=90=86=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/kimmking/rpcfx/client/Rpcfx.java | 21 +++++++--------- .../rpcfx/exception/RpcfxException.java | 8 +++++-- .../kimmking/rpcfx/server/RpcfxInvoker.java | 20 ++++++++-------- .../demo/provider/ReflectionResolver.java | 24 ++++++++++--------- .../demo/provider/RpcfxServerApplication.java | 16 ++++++------- 5 files changed, 45 insertions(+), 44 deletions(-) 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 e82c2b86..70c84ee2 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 @@ -19,11 +19,7 @@ public final class Rpcfx { static { - ParserConfig config = ParserConfig.getGlobalInstance(); - config.addAccept("io.kimmking"); - config.setAutoTypeSupport(true); - - //ParserConfig.getGlobalInstance().addAccept("io.kimmking"); + ParserConfig.getGlobalInstance().addAccept("io.kimmking"); } public static T create(final Class serviceClass, final String url) { @@ -39,6 +35,7 @@ public static class RpcfxInvocationHandler implements InvocationHandler { private final Class serviceClass; private final String url; + public RpcfxInvocationHandler(Class serviceClass, String url) { this.serviceClass = serviceClass; this.url = url; @@ -56,19 +53,17 @@ public Object invoke(Object proxy, Method method, Object[] params) throws Throwa request.setParams(params); RpcfxResponse response = post(request, url); - if (response.isStatus()) { - return response.getResult(); - } else { - RpcfxException e = (RpcfxException) response.getException(); - return null; - } // 这里判断response.status,处理异常 // 考虑封装一个全局的RpcfxException + if (!response.isStatus()) { + throw new RpcfxException("invoke error", response.getException()); + } + return JSON.parse(response.getResult().toString()); } private RpcfxResponse post(RpcfxRequest req, String url) throws IOException { String reqJson = JSON.toJSONString(req); - System.out.println("req json: "+reqJson); + System.out.println("req json: " + reqJson); // 1.可以复用client // 2.尝试使用httpclient或者netty client @@ -78,7 +73,7 @@ private RpcfxResponse post(RpcfxRequest req, String url) throws IOException { .post(RequestBody.create(JSONTYPE, reqJson)) .build(); String respJson = client.newCall(request).execute().body().string(); - System.out.println("resp json: "+respJson); + System.out.println("resp json: " + respJson); return JSON.parseObject(respJson, RpcfxResponse.class); } } 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 index 25ad0456..b17f35fd 100644 --- 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 @@ -10,8 +10,12 @@ public RpcfxException(Throwable throwable) { super(throwable); } - public RpcfxException(String exception) { - super(exception); + 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/server/RpcfxInvoker.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/server/RpcfxInvoker.java index 58fefef0..8efcf796 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 @@ -14,24 +14,24 @@ 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; } - //private RpcfxReflectionResolver reflectionResolver; - -// public RpcfxInvoker(RpcfxReflectionResolver reflectionResolver) { -// this.reflectionResolver = reflectionResolver; -// } public RpcfxResponse invoke(RpcfxRequest request) { RpcfxResponse response = new RpcfxResponse(); String serviceClass = request.getServiceClass(); // 作业1:改成泛型和反射 - Object service = resolver.resolve(serviceClass);//this.applicationContext.getBean(serviceClass); - //Object service = reflectionResolver.resolve(serviceClass); + //Object service = resolver.resolve(serviceClass);//this.applicationContext.getBean(serviceClass); + Object service = reflectionResolver.resolve(serviceClass); try { Method method = resolveMethodFromClass(service.getClass(), request.getMethod()); 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 index 4483a980..3dab158c 100644 --- 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 @@ -1,28 +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 { +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) klass.getConstructor().newInstance(); + t = (T) applicationContext.getBean(klass); } catch (ClassNotFoundException e) { e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (InstantiationException e) { - e.printStackTrace(); - } catch (NoSuchMethodException e) { - e.printStackTrace(); - } catch (InvocationTargetException 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 1034cdce..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 @@ -32,20 +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 RpcfxReflectionResolver createReflectionResolver() { -// return new ReflectionResolver(); -// } - @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") From 1fa8b77e0630b705118192f0c70e0b9fdd86ecdc Mon Sep 17 00:00:00 2001 From: zenglinhui Date: Wed, 16 Dec 2020 22:25:35 +0800 Subject: [PATCH 4/7] =?UTF-8?q?=E7=94=A8XStream=E4=BB=A3=E6=9B=BFFastJson?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 07rpc/rpc01/rpcfx-core/pom.xml | 15 ++++++++ .../java/io/kimmking/rpcfx/client/Rpcfx.java | 10 ++++-- .../kimmking/rpcfx/server/RpcfxInvoker.java | 7 +++- .../io/kimmking/rpcfx/utils/XStreamUtils.java | 36 +++++++++++++++++++ 4 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/utils/XStreamUtils.java diff --git a/07rpc/rpc01/rpcfx-core/pom.xml b/07rpc/rpc01/rpcfx-core/pom.xml index 8f11dc5d..3fdff076 100644 --- a/07rpc/rpc01/rpcfx-core/pom.xml +++ b/07rpc/rpc01/rpcfx-core/pom.xml @@ -24,6 +24,21 @@ 1.2.70 + + + com.thoughtworks.xstream + xstream + 1.4.14 + + + + + org.codehaus.jettison + jettison + 1.4.0 + + + com.squareup.okhttp3 okhttp 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 70c84ee2..5c6e5ba0 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,9 +3,11 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.parser.ParserConfig; +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.XStreamUtils; import okhttp3.MediaType; import okhttp3.OkHttpClient; import okhttp3.Request; @@ -19,7 +21,9 @@ 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) { @@ -35,6 +39,7 @@ public static class RpcfxInvocationHandler implements InvocationHandler { private final Class serviceClass; private final String url; + private final XStream stream = XStreamUtils.createToJson(); public RpcfxInvocationHandler(Class serviceClass, String url) { this.serviceClass = serviceClass; @@ -47,6 +52,7 @@ 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()); @@ -58,7 +64,7 @@ public Object invoke(Object proxy, Method method, Object[] params) throws Throwa if (!response.isStatus()) { throw new RpcfxException("invoke error", response.getException()); } - return JSON.parse(response.getResult().toString()); + return XStreamUtils.fromBean(stream, response.getResult().toString()); } private RpcfxResponse post(RpcfxRequest req, String url) throws IOException { 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 8efcf796..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,11 +2,14 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.SerializerFeature; +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.param.RpcfxResponse; +import io.kimmking.rpcfx.utils.XStreamUtils; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -37,7 +40,9 @@ public RpcfxResponse invoke(RpcfxRequest request) { 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) { 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; + } + +} From 31970a419bf3e33d47bb23834e6850942f757ed4 Mon Sep 17 00:00:00 2001 From: zenglinhui Date: Wed, 16 Dec 2020 23:10:47 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=E7=94=A8Cglib=E6=96=B9=E5=BC=8F=E4=BB=A3?= =?UTF-8?q?=E6=9B=BFproxy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/kimmking/rpcfx/client/Rpcfx.java | 48 +++++++++++-------- 1 file changed, 29 insertions(+), 19 deletions(-) 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 5c6e5ba0..25f4e61f 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 @@ -12,6 +12,9 @@ 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; @@ -27,13 +30,17 @@ public final class Rpcfx { } 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)); + //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"); @@ -52,23 +59,15 @@ 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 - if (!response.isStatus()) { - throw new RpcfxException("invoke error", response.getException()); - } - return XStreamUtils.fromBean(stream, response.getResult().toString()); + return post(method, params, url); } - private RpcfxResponse post(RpcfxRequest req, String url) throws IOException { - String reqJson = JSON.toJSONString(req); + private Object post(Method method, Object[] params, String url) throws IOException, RpcfxException { + 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 @@ -80,7 +79,18 @@ private RpcfxResponse post(RpcfxRequest req, String url) throws IOException { .build(); String respJson = client.newCall(request).execute().body().string(); System.out.println("resp json: " + respJson); - return JSON.parseObject(respJson, RpcfxResponse.class); + 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); } } } From 42a4f1a570b6eeb913d5efbb7ffa9e5d85ae99c7 Mon Sep 17 00:00:00 2001 From: zenglinhui Date: Mon, 28 Dec 2020 00:01:02 +0800 Subject: [PATCH 6/7] =?UTF-8?q?=E6=B7=BB=E5=8A=A0netty=E6=94=AF=E6=8C=81?= =?UTF-8?q?=EF=BC=8C=E6=B5=8B=E8=AF=95=E6=9C=AA=E9=80=9A=E8=BF=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 07rpc/rpc01/rpcfx-core/pom.xml | 7 +++ .../java/io/kimmking/rpcfx/client/Rpcfx.java | 30 +++++++++-- .../kimmking/rpcfx/handler/ClientHandler.java | 36 +++++++++++++ .../io/kimmking/rpcfx/utils/ClientUtils.java | 17 ++++++ .../rpcfx/utils/NettyClientUtils.java | 53 +++++++++++++++++++ .../demo/consumer/RpcfxClientApplication.java | 2 + 6 files changed, 140 insertions(+), 5 deletions(-) create mode 100644 07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/handler/ClientHandler.java create mode 100644 07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/utils/ClientUtils.java create mode 100644 07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/utils/NettyClientUtils.java diff --git a/07rpc/rpc01/rpcfx-core/pom.xml b/07rpc/rpc01/rpcfx-core/pom.xml index 3fdff076..b211dd7f 100644 --- a/07rpc/rpc01/rpcfx-core/pom.xml +++ b/07rpc/rpc01/rpcfx-core/pom.xml @@ -38,6 +38,13 @@ 1.4.0 + + + io.netty + netty-all + 4.1.56.Final + + com.squareup.okhttp3 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 25f4e61f..86106937 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 @@ -7,9 +7,10 @@ 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.NettyClientUtils; import io.kimmking.rpcfx.utils.XStreamUtils; import okhttp3.MediaType; -import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import org.springframework.cglib.proxy.Enhancer; @@ -19,7 +20,6 @@ import java.io.IOException; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; -import java.lang.reflect.Proxy; public final class Rpcfx { @@ -40,18 +40,36 @@ public static T create(final Class serviceClass, final String 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)); + + } + 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 @@ -62,7 +80,7 @@ public Object invoke(Object proxy, Method method, Object[] params) throws Throwa return post(method, params, url); } - private Object post(Method method, Object[] params, String url) throws IOException, RpcfxException { + 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()); @@ -72,12 +90,14 @@ private Object post(Method method, Object[] params, String url) throws IOExcepti // 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(); + 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,处理异常 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..8e703a19 --- /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(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/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/NettyClientUtils.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/utils/NettyClientUtils.java new file mode 100644 index 00000000..ed35c89f --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/utils/NettyClientUtils.java @@ -0,0 +1,53 @@ +package io.kimmking.rpcfx.utils; + +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 NettyClientUtils { + + private final String host; + + private final Integer port; + + private final String req; + + + public NettyClientUtils(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; + } + +} 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())); From bd91507479027d3632acfc02852c24f30440969a Mon Sep 17 00:00:00 2001 From: zenglinhui Date: Mon, 28 Dec 2020 00:31:14 +0800 Subject: [PATCH 7/7] =?UTF-8?q?=E6=B7=BB=E5=8A=A0netty=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NettyClient.java} | 10 ++-- .../java/io/kimmking/rpcfx/client/Rpcfx.java | 1 - .../kimmking/rpcfx/handler/ClientHandler.java | 2 +- .../kimmking/rpcfx/handler/ServerHandler.java | 35 +++++++++++++ .../io/kimmking/rpcfx/server/NettyServer.java | 49 +++++++++++++++++++ 5 files changed, 92 insertions(+), 5 deletions(-) rename 07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/{utils/NettyClientUtils.java => client/NettyClient.java} (85%) create mode 100644 07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/handler/ServerHandler.java create mode 100644 07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/server/NettyServer.java diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/utils/NettyClientUtils.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/client/NettyClient.java similarity index 85% rename from 07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/utils/NettyClientUtils.java rename to 07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/client/NettyClient.java index ed35c89f..a33b1000 100644 --- a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/utils/NettyClientUtils.java +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/client/NettyClient.java @@ -1,4 +1,4 @@ -package io.kimmking.rpcfx.utils; +package io.kimmking.rpcfx.client; import io.kimmking.rpcfx.handler.ClientHandler; import io.netty.bootstrap.Bootstrap; @@ -10,7 +10,7 @@ import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.util.CharsetUtil; -public class NettyClientUtils { +public class NettyClient { private final String host; @@ -19,7 +19,7 @@ public class NettyClientUtils { private final String req; - public NettyClientUtils(String host, Integer port, String req) { + public NettyClient(String host, Integer port, String req) { this.host = host; this.port = port; this.req = req; @@ -50,4 +50,8 @@ protected void initChannel(SocketChannel socketChannel) throws Exception { 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 86106937..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 @@ -8,7 +8,6 @@ import io.kimmking.rpcfx.param.RpcfxRequest; import io.kimmking.rpcfx.param.RpcfxResponse; import io.kimmking.rpcfx.utils.ClientUtils; -import io.kimmking.rpcfx.utils.NettyClientUtils; import io.kimmking.rpcfx.utils.XStreamUtils; import okhttp3.MediaType; import okhttp3.Request; 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 index 8e703a19..bc504dba 100644 --- 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 @@ -19,7 +19,7 @@ public ClientHandler(String req) { @Override protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) throws Exception { - System.out.println(byteBuf.toString(CharsetUtil.UTF_8)); + System.out.println("Client receiver:" + byteBuf.toString(CharsetUtil.UTF_8)); } 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/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(); + } + +}