拦截器设置流程:
-
创建registry bean @PostConstruct GlobalClientInterceptorRegistry init() 从app中获取annoaconfiguer 列表,从configure中获取拦截器列表然后添加到自己的List clientInterceptors
-
创建annoainterceptor bean,被1的@PostConstruct调用到add方法 AnnotationGlobalClientInterceptorConfigurer addClientInterceptors() this.context.getBeansWithAnnotation(GrpcGlobalClientInterceptor.class) 调用GlobalClientInterceptorRegistry 增加拦截器列表
-
GrpcClientBeanPostProcessor中调用AbstractChannelFactory createChannel 得到custominterceptors 加上定义的注解拦截器:final List interceptors = Lists.newArrayList(this.globalClientInterceptorRegistry.getClientInterceptors()); 判断有没有spring注解AnnotationAwareOrderComparator。 有的话interceptors.sort(AnnotationAwareOrderComparator.INSTANCE); 返回创建好的channel ClientInterceptors.interceptForward(channel, interceptors);
-
反射注入channel
自定义拦截器:
- annoa
- ctx.getBeansWithAnnotation
- 获取ctx中GlobalClientInterceptorRegistry, addClientInterceptors
- 在bean GrpcClientBeanPostProcessor postProcessBeforeInitialization之前设置好
Or
- annoa
- 自定义AnnotationGlobalClientInterceptorConfigurer,实现add方法
- 创建该bean,同时注意拦截器顺序