77import io .kimmking .rpcfx .exception .RpcfxException ;
88import io .kimmking .rpcfx .param .RpcfxRequest ;
99import io .kimmking .rpcfx .param .RpcfxResponse ;
10+ import io .kimmking .rpcfx .utils .ClientUtils ;
11+ import io .kimmking .rpcfx .utils .NettyClientUtils ;
1012import io .kimmking .rpcfx .utils .XStreamUtils ;
1113import okhttp3 .MediaType ;
12- import okhttp3 .OkHttpClient ;
1314import okhttp3 .Request ;
1415import okhttp3 .RequestBody ;
1516import org .springframework .cglib .proxy .Enhancer ;
1920import java .io .IOException ;
2021import java .lang .reflect .InvocationHandler ;
2122import java .lang .reflect .Method ;
22- import java .lang .reflect .Proxy ;
2323
2424public final class Rpcfx {
2525
@@ -40,18 +40,36 @@ public static <T> T create(final Class<T> serviceClass, final String url) {
4040
4141 }
4242
43+ public static <T > T create (final Class <T > serviceClass , final String host , final Integer port ) {
44+ //Gglib方式
45+ Enhancer enhancer = new Enhancer ();
46+ //enhancer.setCallback(new RpcfxInvocationHandler(serviceClass, host, port));
47+ enhancer .setSuperclass (serviceClass );
48+ return (T ) enhancer .create ();
49+ // 0. 替换动态代理 -> AOP
50+ //return (T) Proxy.newProxyInstance(Rpcfx.class.getClassLoader(), new Class[]{serviceClass}, new RpcfxInvocationHandler(serviceClass, url));
51+
52+ }
53+
4354 public static class RpcfxInvocationHandler implements InvocationHandler , MethodInterceptor {
4455
4556 public static final MediaType JSONTYPE = MediaType .get ("application/json; charset=utf-8" );
4657
4758 private final Class <?> serviceClass ;
4859 private final String url ;
60+ // private final String host;
61+ // private final Integer port;
4962 private final XStream stream = XStreamUtils .createToJson ();
5063
5164 public <T > RpcfxInvocationHandler (Class <T > serviceClass , String url ) {
5265 this .serviceClass = serviceClass ;
5366 this .url = url ;
5467 }
68+ // public <T> RpcfxInvocationHandler(Class<T> serviceClass, String host, Integer port) {
69+ // this.serviceClass = serviceClass;
70+ // this.host = host;
71+ // this.port = port;
72+ // }
5573
5674 // 可以尝试,自己去写对象序列化,二进制还是文本的,,,rpcfx是xml自定义序列化、反序列化,json: code.google.com/p/rpcfx
5775 // int byte char float double long bool
@@ -62,7 +80,7 @@ public Object invoke(Object proxy, Method method, Object[] params) throws Throwa
6280 return post (method , params , url );
6381 }
6482
65- private Object post (Method method , Object [] params , String url ) throws IOException , RpcfxException {
83+ private Object post (Method method , Object [] params , String url ) throws IOException , RpcfxException , InterruptedException {
6684 RpcfxRequest rpcfxRequest = new RpcfxRequest ();
6785 rpcfxRequest .setServiceClass (this .serviceClass .getName ());
6886 rpcfxRequest .setMethod (method .getName ());
@@ -72,12 +90,14 @@ private Object post(Method method, Object[] params, String url) throws IOExcepti
7290
7391 // 1.可以复用client
7492 // 2.尝试使用httpclient或者netty client
75- OkHttpClient client = new OkHttpClient ();
93+ // OkHttpClient client = new OkHttpClient();
7694 final Request request = new Request .Builder ()
7795 .url (url )
7896 .post (RequestBody .create (JSONTYPE , reqJson ))
7997 .build ();
80- String respJson = client .newCall (request ).execute ().body ().string ();
98+ String respJson = ClientUtils .execute (request ).body ().string ();
99+ /*NettyClientUtils nettyClientUtils = new NettyClientUtils(host, port, reqJson);
100+ String respJson = nettyClientUtils.start();*/
81101 System .out .println ("resp json: " + respJson );
82102 RpcfxResponse response = JSON .parseObject (respJson , RpcfxResponse .class );
83103 // 这里判断response.status,处理异常
0 commit comments