java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Feign深入分析

Java中的Feign深入分析

作者:silmeweed

这篇文章主要介绍了Java中的Feign深入分析,Feign是一个用于发送HTTP请求的工具,它的主要作用是在不同的服务之间传递Token,为了使用Feign,你需要在项目中配置一个Feign的配置类,需要的朋友可以参考下

一、使用方式:

1. java动态生成 :

使用Feign.Builder动态生成,可动态灵活生成不同的操作对象。整个Feign操作核心就是生成这样的Feign.Builder对象

1)示例:

GitHub github = Feign.builder()
           .decoder(new GsonDecoder())
           .logger(new Logger.JavaLogger().appendToFile("logs/http.log"))
           .logLevel(Logger.Level.FULL)
           .client(new OkHttpClient())
           .requestInterceptor(new ForwardedForInterceptor())
           .target(GitHub.class, https://api.github.com);

2)源代码分析:

public abstract class Feign {
//1. Feign.Builder属性。
public static class Builder {
  private final List<RequestInterceptor> requestInterceptors = new ArrayList<RequestInterceptor>();
  private Logger.Level logLevel = Logger.Level.NONE;
  private Contract contract = new Contract.Default(); //使用约解释api标注
  private Client client = new Client.Default(null, null);//网络请求客户端(okHttp/ApacheHttpClient)
  private Retryer retryer = new Retryer.Default();
  private Logger logger = new NoOpLogger();
  private Encoder encoder = new Encoder.Default();
  private Decoder decoder = new Decoder.Default();
  private QueryMapEncoder queryMapEncoder = new QueryMapEncoder.Default();
  private ErrorDecoder errorDecoder = new ErrorDecoder.Default();
  private Options options = new Options(); //
  private InvocationHandlerFactory invocationHandlerFactory = new InvocationHandlerFactory.Default();
  private boolean decode404;
  private boolean closeAfterDecode = true;
}
}
//2. 网络请求客户端的参数设置:(连接时间、读取超过)
public static class Options {
    private final int connectTimeoutMillis;
    private final int readTimeoutMillis;
    private final boolean followRedirects;
}

2. Spring Feign注解方式实现

通过注解方式提高生成Feign.Bulder的效率,简化代码。其核心最终还是生成不同的Feign.Builder实例对象。(见1. java动态生成 )在Spring cloud应用中,当我们要使用feign客户端时,一般要做以下三件事情 :

示例:

//1.使用注解@EnableFeignClients启用feign客户端。
@SpringBootApplication
@EnableFeignClients(basePackages= {"com.missuteam.onepiece.oauth.api"},
defaultConfiguration = defaultFeignConfig.class)
public class TestApplication {
    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }
}
//2.FeignClient定义
@FeignClient(name = "choppe-oauth-server",
        configuration = AuthServiceFeignConfig.class,
        fallbackFactory = AuthServiceFallbackFactory.class,
        path = "/authCenter",
        decode404 = true)
public interface AuthServiceFeignClient {
    //接口
   @RequestMapping(value = "/echo", method = RequestMethod.GET)
    TestModel echo(@RequestParam("parameter") String parameter);
}

二、源代码分析:

启动流程

1. 注册所有feign客户端的缺省配置@EnableFeignClients里指定的defaultConfiguration,生成一个FeignClientSpecification bean.

2. 注册所有@FeignClient的configuration,生成一个配置FeignClientSpecification bean.

3. 所有的配置放置到FeignContext Bean里。

public class FeignAutoConfiguration {
   @Autowired(required = false)
   private List<FeignClientSpecification> configurations = new ArrayList<>();
   @Bean
   public FeignContext feignContext() {
      FeignContext context = new FeignContext();
      context.setConfigurations(this.configurations);
      return context;
   }
}

4. 生成@FeignClient的代理Bean,(FeignClientFactoryBean)FeignClientFactoryBean包括一个Fegin。

5. 配置FeignClientFactoryBean的属性,可以从@EnableFeign里的defaultConfigure和各个@FeignClient里的configure,或application.yml配置属性获取。( feign.client.defaultToProperties = true yml里的配置(default和对应的name)将覆盖@EnableFeign和@FeignClient里的configure.)

protected void configureFeign(FeignContext context, Feign.Builder builder) {
   1.获取application.yml里的配置信息。
   FeignClientProperties properties = applicationContext.getBean(FeignClientProperties.class);
   if (properties != null) {
      2.如果设置isDefaultToProperties=true,将使用application.yml里的default配置覆盖@FeignClient或@EnableFeignClient里的defaultConfigure.
      if (properties.isDefaultToProperties()) {
         configureUsingConfiguration(context, builder);
         2.1@EnableFeignClient里的defaultConfigure.               
    configureUsingProperties(properties.getConfig().get(properties.getDefaultConfig()), builder);
        2.2@FeignClient里的Configure.
         configureUsingProperties(properties.getConfig().get(this.name), builder);
      } else {
         configureUsingProperties(properties.getConfig().get(properties.getDefaultConfig()), builder);
         configureUsingProperties(properties.getConfig().get(this.name), builder);
         configureUsingConfiguration(context, builder);
      }
   } else {
      configureUsingConfiguration(context, builder);
   }
}
protected void configureUsingConfiguration(FeignContext context, Feign.Builder builder) {
   Logger.Level level = getOptional(context, Logger.Level.class);
   if (level != null) {
      builder.logLevel(level);
   }
   Retryer retryer = getOptional(context, Retryer.class);
   if (retryer != null) {
      builder.retryer(retryer);
   }
   ErrorDecoder errorDecoder = getOptional(context, ErrorDecoder.class);
   if (errorDecoder != null) {
      builder.errorDecoder(errorDecoder);
   }
   Request.Options options = getOptional(context, Request.Options.class);
   if (options != null) {
      builder.options(options);
   }
   Map<String, RequestInterceptor> requestInterceptors = context.getInstances(
         this.name, RequestInterceptor.class);
   if (requestInterceptors != null) {
      builder.requestInterceptors(requestInterceptors.values());
   }
   if (decode404) {
      builder.decode404();
   }
}
protected void configureUsingProperties(FeignClientProperties.FeignClientConfiguration config, Feign.Builder builder) {
   if (config == null) {
      return;
   }
   if (config.getLoggerLevel() != null) {
      builder.logLevel(config.getLoggerLevel());
   }
   if (config.getConnectTimeout() != null && config.getReadTimeout() != null) {
      builder.options(new Request.Options(config.getConnectTimeout(), config.getReadTimeout()));
   }
   if (config.getRetryer() != null) {
      Retryer retryer = getOrInstantiate(config.getRetryer());
      builder.retryer(retryer);
   }
   if (config.getErrorDecoder() != null) {
      ErrorDecoder errorDecoder = getOrInstantiate(config.getErrorDecoder());
      builder.errorDecoder(errorDecoder);
   }
   if (config.getRequestInterceptors() != null && !config.getRequestInterceptors().isEmpty()) {
      // this will add request interceptor to builder, not replace existing
      for (Class<RequestInterceptor> bean : config.getRequestInterceptors()) {
         RequestInterceptor interceptor = getOrInstantiate(bean);
         builder.requestInterceptor(interceptor);
      }
   }
   if (config.getDecode404() != null) {
      if (config.getDecode404()) {
         builder.decode404();
      }
   }
   if (Objects.nonNull(config.getEncoder())) {
      builder.encoder(getOrInstantiate(config.getEncoder()));
   }
   if (Objects.nonNull(config.getDecoder())) {
      builder.decoder(getOrInstantiate(config.getDecoder()));
   }
   if (Objects.nonNull(config.getContract())) {
      builder.contract(getOrInstantiate(config.getContract()));
   }
}

FeignClientsRegistrar.class

//注册All feign 默认配置
@Override
public void registerBeanDefinitions(AnnotationMetadata metadata,
      BeanDefinitionRegistry registry) {
   registerDefaultConfiguration(metadata, registry);
   registerFeignClients(metadata, registry);
}
// 注册feign客户端的缺省配置,缺省配置信息来自注解元数据的属性 defaultConfiguration    
    private void registerDefaultConfiguration(AnnotationMetadata metadata,BeanDefinitionRegistry registry) {
        // 获取注解@EnableFeignClients的注解属性     
        Map<String, Object> defaultAttrs = metadata
                .getAnnotationAttributes(EnableFeignClients.class.getName(), true);
        if (defaultAttrs != null && defaultAttrs.containsKey("defaultConfiguration")) {
            String name;
            // 下面是对所注册的缺省配置的的命名,格式如下 :
            // default.xxx.TestApplication
            if (metadata.hasEnclosingClass()) {
                //  针对注解元数据metadata对应一个内部类或者方法返回的方法本地类的情形
                name = "default." + metadata.getEnclosingClassName();
            }
            else {        
                // name 举例 : default.xxx.TestApplication
                // 这里 xxx.TestApplication 是注解@EnableFeignClients所在配置类的长名称           
                name = "default." + metadata.getClassName();
            }
            registerClientConfiguration(registry, name,
                    defaultAttrs.get("defaultConfiguration"));
        }
    }

 FeignClientFactoryBean.java封装了,如何生成Feign

class FeignClientFactoryBean implements FactoryBean<Object>, InitializingBean,
      ApplicationContextAware {
   private Class<?> type;
   private String name;
   private String url;
   private String path;
   private boolean decode404;
   private ApplicationContext applicationContext;
   private Class<?> fallback = void.class;
   private Class<?> fallbackFactory = void.class;
   1.Feign.Builder
   protected Feign.Builder feign(FeignContext context) {
      FeignLoggerFactory loggerFactory = get(context, FeignLoggerFactory.class);
      Logger logger = loggerFactory.create(this.type);
      // @formatter:off
      Feign.Builder builder = get(context, Feign.Builder.class)
            // required values
            .logger(logger)
            .encoder(get(context, Encoder.class))
            .decoder(get(context, Decoder.class))
            .contract(get(context, Contract.class));
      // @formatter:on
     configureFeign(context, builder);
      return builder;
   }
   2. 配置Feign.Builder
   protected void configureFeign(FeignContext context, Feign.Builder builder) {
      FeignClientProperties properties = applicationContext.getBean(FeignClientProperties.class);
      if (properties != null) {
         if (properties.isDefaultToProperties()) {
            configureUsingConfiguration(context, builder);
            configureUsingProperties(properties.getConfig().get(properties.getDefaultConfig()), builder);
            configureUsingProperties(properties.getConfig().get(this.name), builder);
         } else {
            configureUsingProperties(properties.getConfig().get(properties.getDefaultConfig()), builder);
            configureUsingProperties(properties.getConfig().get(this.name), builder);
            configureUsingConfiguration(context, builder);
         }
      } else {
         configureUsingConfiguration(context, builder);
      }
   }
3.最终生成Targeter
<T> T getTarget() {
   FeignContext context = applicationContext.getBean(FeignContext.class);
   Feign.Builder builder = feign(context);
   if (!StringUtils.hasText(this.url)) {
      String url;
      if (!this.name.startsWith("http")) {
         url = "http://" + this.name;
      }
      else {
         url = this.name;
      }
      url += cleanPath();
      return (T) loadBalance(builder, context, new HardCodedTarget<>(this.type,
            this.name, url));
   }
   if (StringUtils.hasText(this.url) && !this.url.startsWith("http")) {
      this.url = "http://" + this.url;
   }
   String url = this.url + cleanPath();
   Client client = getOptional(context, Client.class);
   if (client != null) {
      if (client instanceof LoadBalancerFeignClient) {
         // not load balancing because we have a url,
         // but ribbon is on the classpath, so unwrap
         client = ((LoadBalancerFeignClient)client).getDelegate();
      }
      builder.client(client);
   }
   Targeter targeter = get(context, Targeter.class);
   return (T) targeter.target(this, builder, context, new HardCodedTarget<>(
         this.type, this.name, url));
}

EnableFeignClients类:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(FeignClientsRegistrar.class)
public @interface EnableFeignClients {
   /**
    * 指定扫描的包或类,不指定时,全部扫描.
    */
   String[] value() default {};
   String[] basePackages() default {};
   Class<?>[] basePackageClasses() default {};
   Class<?>[] clients() default {};
   /**
    * 用户定义一个默认的配置类,作用于所有FeignClient
    */
   Class<?>[] defaultConfiguration() default {};
}

到此这篇关于Java中的Feign深入分析的文章就介绍到这了,更多相关Feign深入分析内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文