22
33import io .kimmking .rpcfx .annotation .RpcfxReference ;
44import io .kimmking .rpcfx .api .RpcContext ;
5+ import io .kimmking .rpcfx .meta .ServiceMeta ;
6+ import io .kimmking .rpcfx .registry .RegistryCenter ;
7+ import io .kimmking .rpcfx .registry .RegistryConfiguration ;
58import io .kimmking .rpcfx .stub .StubSkeletonHelper ;
69import lombok .extern .slf4j .Slf4j ;
710import org .springframework .beans .BeansException ;
811import org .springframework .beans .PropertyValues ;
12+ import org .springframework .beans .factory .annotation .Autowired ;
13+ import org .springframework .beans .factory .annotation .Value ;
914import org .springframework .beans .factory .config .InstantiationAwareBeanPostProcessor ;
15+ import org .springframework .context .annotation .Import ;
1016import org .springframework .stereotype .Component ;
1117
18+ import javax .annotation .PostConstruct ;
1219import java .io .Closeable ;
1320import java .io .IOException ;
1421import java .lang .reflect .Field ;
22+ import java .util .ArrayList ;
1523import java .util .Arrays ;
1624import java .util .List ;
1725import java .util .stream .Collectors ;
2432 */
2533@ Slf4j
2634@ Component
35+ @ Import ({RegistryConfiguration .class })
2736public class ConsumerBootstrap implements Closeable , InstantiationAwareBeanPostProcessor {
2837
29- private RpcContext rpcContext = new RpcContext ();
38+ private RpcContext context = new RpcContext ();
3039
3140 private String scanPackage = "io.kimmking" ;
3241
42+ @ Value ("${app.id:app1}" )
43+ public String app ;
44+ @ Value ("${app.namespace:public}" )
45+ public String ns ;
46+ @ Value ("${app.env:dev}" )
47+ public String env ;
48+ @ Value ("${app.mock:false}" )
49+ public boolean mock ;
50+ @ Value ("${app.cache:false}" )
51+ public boolean cache ;
52+ @ Value ("${app.retry:1}" )
53+ public int retry ;
54+
55+ @ Autowired
56+ RegistryCenter rc ;
57+
58+ @ PostConstruct
59+ public void init () {
60+ this .context .getParameters ().put ("app.id" , app );
61+ this .context .getParameters ().put ("app.namespace" , ns );
62+ this .context .getParameters ().put ("app.env" , env );
63+ this .context .getParameters ().put ("app.mock" , String .valueOf (mock ));
64+ this .context .getParameters ().put ("app.cache" , String .valueOf (cache ));
65+ this .context .getParameters ().put ("app.retry" , String .valueOf (retry ));
66+ }
67+
3368 @ Override
3469 public void close () throws IOException {
3570
@@ -38,14 +73,17 @@ public void close() throws IOException {
3873 @ Override
3974 public PropertyValues postProcessProperties (PropertyValues pvs , Object bean , String beanName ) throws BeansException {
4075 if (bean .getClass ().getPackage ().getName ().startsWith (scanPackage )) {
41- Field [] declaredFields = bean .getClass ().getDeclaredFields ();
42- List <Field > consumers = Arrays .stream (declaredFields ).filter (field -> field .isAnnotationPresent (RpcfxReference .class )).collect (Collectors .toList ());
76+ Field [] declaredFields = resolveAllField (bean .getClass ()); // 解决父类里的注解扫描不到的问题
4377
44- consumers .stream ().forEach (consumer -> {
45- Object consumer1 = createConsumer (consumer .getType ());
78+ List <Field > consumers = Arrays .stream (declaredFields )
79+ .filter (field -> field .isAnnotationPresent (RpcfxReference .class ))
80+ .collect (Collectors .toList ());
81+
82+ consumers .stream ().forEach (field -> {
83+ Object consumer = createConsumer (field .getType ());
4684 try {
47- consumer .setAccessible (true );
48- consumer .set (bean , consumer1 );
85+ field .setAccessible (true );
86+ field .set (bean , consumer );
4987 } catch (IllegalAccessException e ) {
5088 log .error (e .getMessage (), e );
5189 }
@@ -54,7 +92,19 @@ public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, Str
5492 return null ;
5593 }
5694
95+ private Field [] resolveAllField (Class <?> aClass ) {
96+ List <Field > res = new ArrayList <>(20 );
97+ while ( !Object .class .equals (aClass ) ) {
98+ Field [] fields = aClass .getDeclaredFields ();
99+ res .addAll (Arrays .asList (fields ));
100+ aClass = aClass .getSuperclass ();
101+ }
102+ return res .toArray (new Field [0 ]);
103+ }
104+
57105 private <T > T createConsumer (Class <T > clazz ) {
58- return StubSkeletonHelper .createConsumer (clazz , rpcContext );
106+ ServiceMeta sm = ServiceMeta .builder ().name (clazz .getCanonicalName ())
107+ .app (app ).namespace (ns ).env (env ).build ();
108+ return (T ) StubSkeletonHelper .createConsumer (sm , context , rc );
59109 }
60110}
0 commit comments