liuyangming / ByteTCC

ByteTCC is a distributed transaction manager based on the TCC(Try/Confirm/Cancel) mechanism. It’s compatible with the JTA specification. User guide: https://github.com/liuyangming/ByteTCC/wiki
https://www.bytesoft.org/
GNU Lesser General Public License v3.0
2.9k stars 911 forks source link

SpringCloudConfiguration 创建Contract 错误导致扩展的AnnotatedParameterProcessor 丢失 #18

Open hi-sb opened 7 years ago

hi-sb commented 7 years ago

原始的 是这样的@See org.bytesoft.bytetcc.supports.springcloud.SpringCloudConfiguration


    @ConditionalOnMissingBean({Contract.class})
    @Bean
    public Contract feignContract() {
        return new SpringMvcContract(annotatedParameterProcessors);
    }

我们查看 SpringMvcContract 的构造器 不难看出,实际上他是有一个可以传入List annotatedParameterProcessors 的构造器,也就是说,允许我们 扩展List annotatedParameterProcessors

    public SpringMvcContract(
            List<AnnotatedParameterProcessor> annotatedParameterProcessors) {
        this(annotatedParameterProcessors, new DefaultConversionService());
    }

在我们的项目中就扩展了一个一个AnnotatedParameterProcessor

    @Bean
    public List<AnnotatedParameterProcessor> setAnnotatedParameterProcessor() {
        List<AnnotatedParameterProcessor> annotatedArgumentResolvers = new ArrayList<>();
        annotatedArgumentResolvers.add(new PathVariableParameterProcessor());
        annotatedArgumentResolvers.add(new RequestParamParameterProcessor());
        annotatedArgumentResolvers.add(new RequestHeaderParameterProcessor());
        annotatedArgumentResolvers.add(new CookieValueParameterProcessor());
        return annotatedArgumentResolvers;
    }

feign 原生也是支持这样扩展的,但是在TCC 中没有调用这个构造器,导致扩展丢失了。建议修改为:


    @Bean
    @ConditionalOnMissingBean
    public  List<AnnotatedParameterProcessor> annotatedParameterProcessors () {
        List<AnnotatedParameterProcessor> annotatedArgumentResolvers = new ArrayList<AnnotatedParameterProcessor>();
        annotatedArgumentResolvers.add(new PathVariableParameterProcessor());
        annotatedArgumentResolvers.add(new RequestParamParameterProcessor());
        annotatedArgumentResolvers.add(new RequestHeaderParameterProcessor());
        return annotatedArgumentResolvers;
    }

    @org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean(feign.Contract.class)
    @org.springframework.context.annotation.Bean
    public feign.Contract feignContract( @Autowired List<AnnotatedParameterProcessor> annotatedParameterProcessors) {
        return new SpringMvcContract(annotatedParameterProcessors);
    }

其中public List annotatedParameterProcessors () 方法创建的其实就是 SpringMvcContract 中的getDefaultAnnotatedArgumentsProcessors(),参考代码:

    private List<AnnotatedParameterProcessor> getDefaultAnnotatedArgumentsProcessors() {

        List<AnnotatedParameterProcessor> annotatedArgumentResolvers = new ArrayList<>();

        annotatedArgumentResolvers.add(new PathVariableParameterProcessor());
        annotatedArgumentResolvers.add(new RequestParamParameterProcessor());
        annotatedArgumentResolvers.add(new RequestHeaderParameterProcessor());

        return annotatedArgumentResolvers;
    }

只不过他是private 的所以只能自己写一遍了。如果框架不做修改,那么我就只有去多创建一个 feign.Contract来覆盖你的了,但是我觉得这样不好,容易有歧义毕竟我只想扩展List ,既然feign 原生都支持这样扩展,那么TCC 也应该支持。

hi-sb commented 7 years ago

package org.springframework.cloud.netflix.feign;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.cloud.netflix.feign.support.ResponseEntityDecoder;
import org.springframework.cloud.netflix.feign.support.SpringDecoder;
import org.springframework.cloud.netflix.feign.support.SpringEncoder;
import org.springframework.cloud.netflix.feign.support.SpringMvcContract;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.core.convert.ConversionService;
import org.springframework.format.support.DefaultFormattingConversionService;
import org.springframework.format.support.FormattingConversionService;

import com.netflix.hystrix.HystrixCommand;

import feign.Contract;
import feign.Feign;
import feign.Logger;
import feign.Retryer;
import feign.codec.Decoder;
import feign.codec.Encoder;
import feign.hystrix.HystrixFeign;

/**
 * @author Dave Syer
 * @author Venil Noronha
 */
@Configuration
public class FeignClientsConfiguration {

    @Autowired
    private ObjectFactory<HttpMessageConverters> messageConverters;

    @Autowired(required = false)
    private List<AnnotatedParameterProcessor> parameterProcessors = new ArrayList<>();

    @Autowired(required = false)
    private List<FeignFormatterRegistrar> feignFormatterRegistrars = new ArrayList<>();

    @Autowired(required = false)
    private Logger logger;

    @Bean
    @ConditionalOnMissingBean
    public Decoder feignDecoder() {
        return new ResponseEntityDecoder(new SpringDecoder(this.messageConverters));
    }

    @Bean
    @ConditionalOnMissingBean
    public Encoder feignEncoder() {
        return new SpringEncoder(this.messageConverters);
    }

    @Bean
    @ConditionalOnMissingBean
    public Contract feignContract(ConversionService feignConversionService) {
        return new SpringMvcContract(this.parameterProcessors, feignConversionService);
    }
。。。。。。

这是 feign 的做法 也可以参考这个

liuyangming commented 7 years ago

@hi-sb 感谢反馈,相应修改将在0.4.0-beta3版本中发布,敬请关注!

liuyangming commented 7 years ago

该问题也可以先通过自定义一个feign.Contract来解决。若业务系统自定义了feign.Contract,SpringCloudConfiguration中的feignContract将不再生效(有ConditionalOnMissingBean注解定义)。

@org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean(feign.Contract.class)
@org.springframework.context.annotation.Bean
public feign.Contract feignContract() {
    return new SpringMvcContract();
}
hi-sb commented 7 years ago

我已经先这样解决了