33
44import com .alibaba .fastjson .JSON ;
55import com .alibaba .fastjson .parser .ParserConfig ;
6+ import io .kimmking .rpcfx .utils .HttpUtils ;
7+ import io .kimmking .rpcfx .exception .RpcException ;
68import io .kimmking .rpcfx .api .RpcfxRequest ;
79import io .kimmking .rpcfx .api .RpcfxResponse ;
10+ import net .bytebuddy .ByteBuddy ;
11+ import net .bytebuddy .implementation .InvocationHandlerAdapter ;
12+ import net .bytebuddy .matcher .ElementMatchers ;
813import okhttp3 .MediaType ;
9- import okhttp3 .OkHttpClient ;
10- import okhttp3 .Request ;
11- import okhttp3 .RequestBody ;
14+ import okhttp3 .internal .http2 .ErrorCode ;
1215
1316import java .io .IOException ;
1417import java .lang .reflect .InvocationHandler ;
18+ import java .lang .reflect .InvocationTargetException ;
1519import java .lang .reflect .Method ;
1620import java .lang .reflect .Proxy ;
21+ import java .util .HashMap ;
22+ import java .util .Map ;
1723
1824public final class Rpcfx {
1925
2026 static {
2127 ParserConfig .getGlobalInstance ().addAccept ("io.kimmking" );
2228 }
2329
24- public static <T > T create (final Class <T > serviceClass , final String url ) {
25-
26- // 0. 替换动态代理 -> AOP
27- return (T ) Proxy .newProxyInstance (Rpcfx .class .getClassLoader (), new Class []{serviceClass }, new RpcfxInvocationHandler (serviceClass , url ));
30+ public static <T > T create (final Class <T > serviceClass , final String url )
31+ throws NoSuchMethodException , IllegalAccessException , InvocationTargetException , InstantiationException {
2832
33+ // 0. 替换动态代理 -> 字节码增强
34+ return serviceClass .cast (getByteBuddyProxy (serviceClass , url ));
35+ }
36+ private static <T > Object getByteBuddyProxy (Class <T > serviceClass , String url )
37+ throws InstantiationException , IllegalAccessException , InvocationTargetException , NoSuchMethodException {
38+ return new ByteBuddy ()
39+ .subclass (Object .class )
40+ .name (serviceClass .getCanonicalName () + "$ByteBuddyProxy" )
41+ .implement (serviceClass )
42+ .method (ElementMatchers .any ())
43+ .intercept (InvocationHandlerAdapter .of (new RpcfxInvocationHandler (serviceClass , url )))
44+ .make ()
45+ .load (Rpcfx .class .getClassLoader ())
46+ .getLoaded ()
47+ .getDeclaredConstructor ()
48+ .newInstance ();
2949 }
3050
3151 public static class RpcfxInvocationHandler implements InvocationHandler {
@@ -39,10 +59,9 @@ public <T> RpcfxInvocationHandler(Class<T> serviceClass, String url) {
3959 this .url = url ;
4060 }
4161
42- // 可以尝试,自己去写对象序列化,二进制还是文本的,,,rpcfx是xml自定义序列化、反序列化,json: code.google.com/p/rpcfx
43- // int byte char float double long bool
62+ // TODO: 2020/12/17 可以尝试,自己去写对象序列化,二进制还是文本的,,,rpcfx是xml自定义序列化、反序列化,json: code.google.com/p/rpcfx
63+ // int byte char float double long bool
4464 // [], data class
45-
4665 @ Override
4766 public Object invoke (Object proxy , Method method , Object [] params ) throws Throwable {
4867 RpcfxRequest request = new RpcfxRequest ();
@@ -51,10 +70,12 @@ public Object invoke(Object proxy, Method method, Object[] params) throws Throwa
5170 request .setParams (params );
5271
5372 RpcfxResponse response = post (request , url );
54-
5573 // 这里判断response.status,处理异常
5674 // 考虑封装一个全局的RpcfxException
57-
75+ if (!response .isStatus ()){
76+ System .out .println ("error" + response );
77+ throw new RpcException (ErrorCode .CONNECT_ERROR );
78+ }
5879 return JSON .parse (response .getResult ().toString ());
5980 }
6081
@@ -64,12 +85,7 @@ private RpcfxResponse post(RpcfxRequest req, String url) throws IOException {
6485
6586 // 1.可以复用client
6687 // 2.尝试使用httpclient或者netty client
67- OkHttpClient client = new OkHttpClient ();
68- final Request request = new Request .Builder ()
69- .url (url )
70- .post (RequestBody .create (JSONTYPE , reqJson ))
71- .build ();
72- String respJson = client .newCall (request ).execute ().body ().string ();
88+ String respJson = HttpUtils .httpPostJson (reqJson ,url );
7389 System .out .println ("resp json: " +respJson );
7490 return JSON .parseObject (respJson , RpcfxResponse .class );
7591 }
0 commit comments