通过BeanFactotyPostProcessor动态修改@FeignClient的path

最近项目有个需求,要在启动后,动态修改@FeignClient的请求路径,网上找到的基本都是在@FeignClient里使用${…},通过配置文件来定义Feign的接口路径,这并不能满足我们的需求

由于某些特殊原因,我们的每个接口都有一个interfacePath,定义在接口上的自定义注解中
也就是说@FeignClient定义的接口继承自其他模块,而其他模块的接口上有个自定义注解,描述了该接口的interfacePath,如下:

@FeignClient(value = "x-module")
public interface XXXService extends XApi{
}
@XXXMapping("/member")
public interface XApi {

所以我们需要在每个@FeignClient中将这个@XXXMapping的值添加到path属性,作为跨服务调用的前缀,如果要手动处理每个@FeignClient前缀,未免太不友好,我们希望这个能由程序自动处理

首先看下@FeignClient扫描的过程,看看有没有合适的时机来处理这个问题

使用Feign的项目,一般会在启动类添加注解@EnableFeignClients,先点进这个注解看下

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Import({FeignClientsRegistrar.class})
public @interface EnableFeignClients {
    String[] value() default {};

    String[] basePackages() default {};

    Class<?>[] basePackageClasses() default {};

    Class<?>[] defaultConfiguration() default {};

    Class<?>[] clients() default {};
}

它通过@Import注解导入了一个类FeignClientsRegistrar

class FeignClientsRegistrar implements ImportBeanDefinitionRegistrar, ResourceLoaderAware, EnvironmentAware {

这个类实现了ImportBeanDefinitionRegistrar接口,这个接口用于在Spring容器初始化过程中,向容器注册一些BeanDefinition,这属于Spring源码的范畴这里就不再赘述,直接看它的registerBeanDefinitions方法实现

    public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
        this.registerDefaultConfiguration(metadata, registry);
        this.registerFeignClients(metadata, registry);
    }

只有两行代码,看名字第二行是注册FeignClient,那么我们的@FeignClient基本可以确定是这行代码在处理了,点进去

这个方法比较长,这里就只贴些关键代码

	LinkedHashSet<BeanDefinition> candidateComponents = new LinkedHashSet();
    Map<String, Object> attrs = metadata.getAnnotationAttributes(EnableFeignClients.class.getName());
    ......
    ......
    ClassPathScanningCandidateComponentProvider scanner = this.getScanner();
    scanner.setResourceLoader(this.resourceLoader);
    scanner.addIncludeFilter(new AnnotationTypeFilter(FeignClient.class));
    Set<String> basePackages = this.getBasePackages(metadata);
    Iterator var8 = basePackages.iterator();
    while(var8.hasNext()) {
         String basePackage = (String)var8.next();
         candidateComponents.addAll(scanner.findCandidateComponents(basePackage));
    }
    
    Iterator var13 = candidateComponents.iterator();

    while(var13.hasNext()) {
        BeanDefinition candidateComponent = (BeanDefinition)var13.next();
        if (candidateComponent instanceof AnnotatedBeanDefinition) {
            AnnotatedBeanDefinition beanDefinition = (AnnotatedBeanDefinition)candidateComponent;
            AnnotationMetadata annotationMetadata = beanDefinition.getMetadata();
            Assert.isTrue(annotationMetadata.isInterface(), "@FeignClient can only be specified on an interface");
            Map<String, Object> attributes = annotationMetadata.getAnnotationAttributes(FeignClient.class.getCanonicalName());
            String name = this.getClientName(attributes);
            this.registerClientConfiguration(registry, name, attributes.get("configuration"));
            this.registerFeignClient(registry, annotationMetadata, attributes);
        }
    }
    ......
    ......

先定义了一个扫描器,通过@FeignClient过滤出一组BeanDefinition,也就是上面的candidateComponents,然后遍历,其中有一行代码

Map<String, Object> attributes = annotationMetadata.getAnnotationAttributes(FeignClient.class.getCanonicalName());

这里就获取了@FeignClient里定义的各种属性,比如value 、path 、contextId等等
然后调用registerFeignClient方法完成注册,进入这个方法

    private void registerFeignClient(BeanDefinitionRegistry registry, AnnotationMetadata annotationMetadata, Map<String, Object> attributes) {
        String className = annotationMetadata.getClassName();
        Class clazz = ClassUtils.resolveClassName(className, (ClassLoader)null);
        ConfigurableBeanFactory beanFactory = registry instanceof ConfigurableBeanFactory ? (ConfigurableBeanFactory)registry : null;
        String contextId = this.getContextId(beanFactory, attributes);
        String name = this.getName(attributes);
        FeignClientFactoryBean factoryBean = new FeignClientFactoryBean();
        factoryBean.setBeanFactory(beanFactory);
        factoryBean.setName(name);
        factoryBean.setContextId(contextId);
        factoryBean.setType(clazz);
        factoryBean.setRefreshableClient(this.isClientRefreshEnabled());
        BeanDefinitionBuilder definition = BeanDefinitionBuilder.genericBeanDefinition(clazz, () -> {
            factoryBean.setUrl(this.getUrl(beanFactory, attributes));
            factoryBean.setPath(this.getPath(beanFactory, attributes));
            factoryBean.setDecode404(Boolean.parseBoolean(String.valueOf(attributes.get("decode404"))));
            Object fallback = attributes.get("fallback");
            if (fallback != null) {
                factoryBean.setFallback(fallback instanceof Class ? (Class)fallback : ClassUtils.resolveClassName(fallback.toString(), (ClassLoader)null));
            }

            Object fallbackFactory = attributes.get("fallbackFactory");
            if (fallbackFactory != null) {
                factoryBean.setFallbackFactory(fallbackFactory instanceof Class ? (Class)fallbackFactory : ClassUtils.resolveClassName(fallbackFactory.toString(), (ClassLoader)null));
            }

            return factoryBean.getObject();
        });
        definition.setAutowireMode(2);
        definition.setLazyInit(true);
        this.validate(attributes);
        AbstractBeanDefinition beanDefinition = definition.getBeanDefinition();
        beanDefinition.setAttribute("factoryBeanObjectType", className);
        beanDefinition.setAttribute("feignClientsRegistrarFactoryBean", factoryBean);
        boolean primary = (Boolean)attributes.get("primary");
        beanDefinition.setPrimary(primary);
        String[] qualifiers = this.getQualifiers(attributes);
        if (ObjectUtils.isEmpty(qualifiers)) {
            qualifiers = new String[]{contextId + "FeignClient"};
        }

        BeanDefinitionHolder holder = new BeanDefinitionHolder(beanDefinition, className, qualifiers);
        BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry);
        this.registerOptionsBeanDefinition(registry, contextId);
    }

这个方法虽然长但很清晰,先定义了一个FeignClientFactoryBean,然后生成一个BeanDefinitionBuilder,通过lambda传入了一个InstanceSupplier,其持有了FactoryBean,在InstanceSupplier中,通过设置FactoryBean的url、path属性,确定了@FeignClient请求的路径

我们通过debug观察一下最终生成的BeanDefinition长什么样子,进入registerBeanDefinition方法,先获取了beanName,这个名字就是我们自己接口的全路径名
第二行代码真正地往容器注册了BeanDefinition,在这行打个断点,并设置断点的条件,方便定位到我们的@FeignClient类
通过BeanFactotyPostProcessor动态修改@FeignClient的path_第1张图片
通过BeanFactotyPostProcessor动态修改@FeignClient的path_第2张图片

可以看到生成的BeanDefinition有个instanceSupplier属性

通过BeanFactotyPostProcessor动态修改@FeignClient的path_第3张图片

其内部的AnnotationAttributes就是从@FeignClient注解中解析到的配置,包括value、path等,是 一个Map结构

通过BeanFactotyPostProcessor动态修改@FeignClient的path_第4张图片

看到这里,便已经有了大致思路了,这里的InstanceSupplier,持有了从@FeignClinent中解析到的各种属性,并在将来实例化的时候,将这些属性处理为FeignClient的请求路径

那么我们只要在这步之后,实例化之前,将InstanceSupplier持有的属性修改掉,就可以实现动态修改@FeignClient的请求path了

ImportBeanDefinitionRegistrar的处理发生在BeanFactoryPostProcessor的处理流程中,那么我们可以自定义一个BeanFactoryPostProcessor,来获取Feign处理后的BeanDefinition,取其InstanceSupplier,反射修改其属性

自定义一个BeanFactoryPostProcessor

public class FeignClientProcessor implements BeanFactoryPostProcessor, ResourceLoaderAware, EnvironmentAware {
    private String feignClientPackage;
    private ResourceLoader resourceLoader;
    private Environment environment;
    ......
    ......

实现ResourceLoaderAware和EnvironmentAware接口 是为了扫描得到加了@FeignClient注解的类,因为Feign注册的BeanDefinition的名字就是我们接口的全路径名,所以可以扫描后到容器里根据类名取,上面看到有Feign扫描的过程,就直接copy过来用了

其中在EnvironmentAware的回调中,设置了一个Feign的扫描路径,因为此时还在Spring容器刷新的早期阶段,通过@Value注解是取不到配置的

	@Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
        this.feignClientPackage = environment.getProperty("feign.client.package");
    }

扫描的代码基本上就是Feign的源码,略微修改,扫描的路径是自定义的,而不是从根路径扫,因为我们自己的项目,Feign接口是在指定位置的,然后扫描到的BeanDefinition,转化为类名,这样就得到了所有@FeignClient标注的类名列表

	private List<String> scanFeignClient() {
        ClassPathScanningCandidateComponentProvider scanner = this.getScanner();
        scanner.setResourceLoader(this.resourceLoader);
        scanner.addIncludeFilter(new AnnotationTypeFilter(FeignClient.class));
        Set<BeanDefinition> candidateComponents = scanner.findCandidateComponents(feignClientPackage);
        return candidateComponents.stream().map(BeanDefinition::getBeanClassName).collect(Collectors.toList());
    }

    private ClassPathScanningCandidateComponentProvider getScanner() {
        return new ClassPathScanningCandidateComponentProvider(false, this.environment) {
            protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
                boolean isCandidate = false;
                if (beanDefinition.getMetadata().isIndependent() && !beanDefinition.getMetadata().isAnnotation()) {
                    isCandidate = true;
                }

                return isCandidate;
            }
        };
    }

然后针对每个类进行处理
先通过类实现的interface获取自定义注解的接口路径,然后通过类名从容器中获取到Feign处理过的BeanDefinition,取其InstanceSupplier,反射找到存储@FeignClient属性的map,拼接请求前缀作为path添加到map中

	List<String> feignClientList = scanFeignClient();
    feignClientList.forEach(item -> {
        GenericBeanDefinition beanDefinition = (GenericBeanDefinition)configurableListableBeanFactory.getBeanDefinition(item);
        Class<?> clazz = beanDefinition.getBeanClass();
        Class<?> apiInterface = Arrays.stream(clazz.getInterfaces()).filter(i -> i.getName().startsWith("com.xxx") && i.getName().endsWith("Api")).findAny().orElseThrow(() -> new RuntimeException("基础路径未定义"));
        XXXMapping annotation = apiInterface.getAnnotation(XXXMapping.class);
        String interfacePath = annotation.value();
        Supplier<?> instanceSupplier = beanDefinition.getInstanceSupplier();
        try {
            Field[] declaredFields = instanceSupplier.getClass().getDeclaredFields();
            for (Field field : declaredFields) {
                if (field.getType().isAssignableFrom(Map.class)) {
                    field.setAccessible(true);
                    Map<String, String> map = (Map)field.get(instanceSupplier);
                    String basePath = map.get("value");
                    map.put("path", basePath + interfacePath);
                }
            }
        } catch (Exception e) {
            log.error("初始化FeignClient失败:", e);
        }
    });

完整代码

@Component
@Log4j2
public class FeignClientProcessor implements BeanFactoryPostProcessor, ResourceLoaderAware, EnvironmentAware {

    private String feignClientPackage;

    private ResourceLoader resourceLoader;

    private Environment environment;

    @Override
    @SuppressWarnings("unchecked")
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        List<String> feignClientList = scanFeignClient();
        feignClientList.forEach(item -> {
            GenericBeanDefinition beanDefinition = (GenericBeanDefinition)configurableListableBeanFactory.getBeanDefinition(item);
            Class<?> clazz = beanDefinition.getBeanClass();
            Class<?> apiInterface = Arrays.stream(clazz.getInterfaces()).filter(i -> i.getName().startsWith("com.aic") && i.getName().endsWith("Api")).findAny().orElseThrow(() -> new RuntimeException("基础路径未定义"));
            XXXMapping annotation = apiInterface.getAnnotation(XXXMapping.class);
            String interfacePath = annotation.value();
            Supplier<?> instanceSupplier = beanDefinition.getInstanceSupplier();
            try {
                Field[] declaredFields = instanceSupplier.getClass().getDeclaredFields();
                for (Field field : declaredFields) {
                    Class<?> type = field.getType();
                    if (type.isAssignableFrom(Map.class)) {
                        field.setAccessible(true);
                        Map<String, String> map = (Map)field.get(instanceSupplier);
                        String basePath = map.get("value");
                        map.put("path", basePath + interfacePath);
                    }
                }
            } catch (Exception e) {
                log.error("初始化FeignClient失败:", e);
            }
        });
    }

    private List<String> scanFeignClient() {
        ClassPathScanningCandidateComponentProvider scanner = this.getScanner();
        scanner.setResourceLoader(this.resourceLoader);
        scanner.addIncludeFilter(new AnnotationTypeFilter(FeignClient.class));
        Set<BeanDefinition> candidateComponents = scanner.findCandidateComponents(feignClientPackage);
        return candidateComponents.stream().map(BeanDefinition::getBeanClassName).collect(Collectors.toList());
    }

    private ClassPathScanningCandidateComponentProvider getScanner() {
        return new ClassPathScanningCandidateComponentProvider(false, this.environment) {
            protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
                boolean isCandidate = false;
                if (beanDefinition.getMetadata().isIndependent() && !beanDefinition.getMetadata().isAnnotation()) {
                    isCandidate = true;
                }

                return isCandidate;
            }
        };
    }

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
        this.feignClientPackage = environment.getProperty("feign.client.package");
    }
}

你可能感兴趣的:(java,springboot源码,spring,boot,spring,cloud)