1+ package io .kimmking .rpcfx .client ;
2+
3+ import com .alibaba .fastjson .JSON ;
4+ import io .kimmking .rpcfx .api .*;
5+ import okhttp3 .MediaType ;
6+ import okhttp3 .OkHttpClient ;
7+ import okhttp3 .Request ;
8+ import okhttp3 .RequestBody ;
9+
10+ import java .io .IOException ;
11+ import java .lang .reflect .InvocationHandler ;
12+ import java .lang .reflect .Method ;
13+ import java .util .List ;
14+
15+ public class RpcfxInvocationHandler implements InvocationHandler {
16+
17+ public static final MediaType JSONTYPE = MediaType .get ("application/json; charset=utf-8" );
18+
19+ private final Class <?> serviceClass ;
20+ private final List <String > invokers ;
21+ private final Router router ;
22+ private final LoadBalancer loadBalance ;
23+ private final Filter [] filters ;
24+
25+ public <T > RpcfxInvocationHandler (Class <T > serviceClass , List <String > invokers , Router router , LoadBalancer loadBalance , Filter ... filters ){
26+ this .serviceClass = serviceClass ;
27+ this .invokers = invokers ;
28+ this .router = router ;
29+ this .loadBalance = loadBalance ;
30+ this .filters = filters ;
31+ }
32+
33+ // 可以尝试,自己去写对象序列化,二进制还是文本的,,,rpcfx是xml自定义序列化、反序列化,json: code.google.com/p/rpcfx
34+ // int byte char float double long bool
35+ // [], data class
36+
37+ @ Override
38+ public Object invoke (Object proxy , Method method , Object [] params ) throws Throwable {
39+
40+ List <String > urls = router .route (invokers );
41+ // System.out.println("router.route => ");
42+ // urls.forEach(System.out::println);
43+ String url = loadBalance .select (urls ); // router, loadbalance
44+ // System.out.println("loadBalance.select => ");
45+ // System.out.println("final => " + url);
46+
47+ if (url == null ){
48+ throw new RuntimeException ("No available providers from registry center." );
49+ }
50+
51+ // 加filter地方之二
52+ // mock == true, new Student("hubao");
53+
54+ RpcfxRequest request = new RpcfxRequest ();
55+ request .setServiceClass (this .serviceClass .getName ());
56+ request .setMethod (method .getName ());
57+ request .setParams (params );
58+
59+ if (null !=filters ){
60+ for (Filter filter : filters ){
61+ if (!filter .filter (request )){
62+ return null ;
63+ }
64+ }
65+ }
66+
67+ RpcfxResponse response = post (request , url );
68+
69+ // 加filter地方之三
70+ // Student.setTeacher("cuijing");
71+
72+ // 这里判断response.status,处理异常
73+ // 考虑封装一个全局的RpcfxException
74+
75+ return JSON .parse (response .getResult ().toString ());
76+ }
77+
78+ OkHttpClient client = new OkHttpClient ();
79+
80+ private RpcfxResponse post (RpcfxRequest req , String url ) throws IOException {
81+ String reqJson = JSON .toJSONString (req );
82+ // System.out.println("req json: "+reqJson);
83+
84+ // 1.可以复用client
85+ // 2.尝试使用httpclient或者netty client
86+
87+ final Request request = new Request .Builder ()
88+ .url (url )
89+ .post (RequestBody .create (JSONTYPE , reqJson ))
90+ .build ();
91+ String respJson = client .newCall (request ).execute ().body ().string ();
92+ // System.out.println("resp json: "+respJson);
93+ return JSON .parseObject (respJson , RpcfxResponse .class );
94+ }
95+ }
0 commit comments