1- package io .kimmking .rpcfx .client ;
1+ package io .kimmking .rpcfx .consumer ;
22
33
4- import com .alibaba .fastjson .JSON ;
54import com .alibaba .fastjson .parser .ParserConfig ;
65import io .kimmking .rpcfx .api .*;
7- import okhttp3 .MediaType ;
8- import okhttp3 .OkHttpClient ;
9- import okhttp3 .Request ;
10- import okhttp3 .RequestBody ;
116import org .apache .curator .RetryPolicy ;
127import org .apache .curator .framework .CuratorFramework ;
138import org .apache .curator .framework .CuratorFrameworkFactory ;
149import org .apache .curator .framework .recipes .cache .TreeCache ;
1510import org .apache .curator .framework .recipes .cache .TreeCacheEvent ;
1611import org .apache .curator .framework .recipes .cache .TreeCacheListener ;
1712import org .apache .curator .retry .ExponentialBackoffRetry ;
18- import org .apache .zookeeper .CreateMode ;
1913
20- import java .io .IOException ;
21- import java .lang .reflect .InvocationHandler ;
22- import java .lang .reflect .Method ;
2314import java .lang .reflect .Proxy ;
24- import java .net .InetAddress ;
2515import java .util .ArrayList ;
2616import java .util .List ;
2717
28- public final class Rpcfx {
18+ public final class RpcfxInvoker {
2919
3020 static {
3121 ParserConfig .getGlobalInstance ().addAccept ("io.kimmking" );
3222 }
23+ CuratorFramework client ;
24+ String zkUrl = null ;
3325
34- public static <T , filters > T createFromRegistry (final Class <T > serviceClass , final String zkUrl , Router router , LoadBalancer loadBalance , Filter filter ) {
26+ public RpcfxInvoker (String zkUrl ) {
27+ this .zkUrl = zkUrl ; //"localhost:2181"
28+ this .start ();
29+ }
30+
31+ public void start () {
32+ RetryPolicy retryPolicy = new ExponentialBackoffRetry (1000 , 3 );
33+ client = CuratorFrameworkFactory .builder ().connectString (this .zkUrl ).namespace ("rpcfx" ).retryPolicy (retryPolicy ).build ();
34+ client .start ();
35+ }
36+
37+ public void stop () {
38+ client .close ();
39+ }
3540
36- // 加filte之一
41+ public < T > T createFromRegistry ( final Class < T > serviceClass , Router router , LoadBalancer loadBalance , Filter filter ) {
3742
3843 String service = serviceClass .getCanonicalName ();//"io.kimking.rpcfx.demo.api.UserService";
3944 System .out .println ("====> " +service );
4045 List <String > invokers = new ArrayList <>();
4146
42- RetryPolicy retryPolicy = new ExponentialBackoffRetry (1000 , 3 );
43- CuratorFramework client = CuratorFrameworkFactory .builder ().connectString ("localhost:2181" ).namespace ("rpcfx" ).retryPolicy (retryPolicy ).build ();
44- client .start ();
45-
4647 try {
47- // ServiceProviderDesc userServiceSesc = ServiceProviderDesc.builder()
48- // .host(InetAddress.getLocalHost().getHostAddress())
49- // .port(8082).serviceClass(service).build();
50- // String userServiceSescJson = JSON.toJSONString(userServiceSesc);
51-
5248
5349 if ( null == client .checkExists ().forPath ("/" + service )) {
5450 return null ;
@@ -69,25 +65,13 @@ public void childEvent(CuratorFramework curatorFramework, TreeCacheEvent treeCac
6965 ex .printStackTrace ();
7066 }
7167
72- //
73- //
74- // // register service
75- // // xxx "io.kimmking.rpcfx.demo.api.UserService"
76- //
77-
78-
79- // curator Provider list from zk
80-
81- // 1. 简单:从zk拿到服务提供的列表
82- // 2. 挑战:监听zk的临时节点,根据事件更新这个list(注意,需要做个全局map保持每个服务的提供者List)
83-
8468 return (T ) create (serviceClass , invokers , router , loadBalance , filter );
8569
8670 }
8771
8872
8973
90- private static void fetchInvokers (CuratorFramework client , String service , List <String > invokers ) throws Exception {
74+ private void fetchInvokers (CuratorFramework client , String service , List <String > invokers ) throws Exception {
9175 List <String > services = client .getChildren ().forPath ("/" + service );
9276 invokers .clear ();
9377 for (String svc : services ) {
@@ -97,10 +81,11 @@ private static void fetchInvokers(CuratorFramework client, String service, List<
9781 }
9882 }
9983
100- private static <T > Object create (Class <T > serviceClass , List <String > invokers , Router router , LoadBalancer loadBalance , Filter ... filters ) {
101- // 0. 替换动态代理 -> 字节码生成
102- return (T ) Proxy .newProxyInstance (Rpcfx .class .getClassLoader (), new Class []{serviceClass },
103- new RpcfxInvocationHandler (serviceClass , invokers , router , loadBalance , filters ));
84+ private <T > T create (Class <T > serviceClass , List <String > invokers , Router router , LoadBalancer loadBalance , Filter ... filters ) {
85+ RpcfxInvocationHandler invocationHandler
86+ = new RpcfxInvocationHandler (serviceClass , invokers , router , loadBalance , filters );
87+ return (T ) Proxy .newProxyInstance (RpcfxInvoker .class .getClassLoader (),
88+ new Class []{serviceClass }, invocationHandler );
10489 }
10590
10691}
0 commit comments