博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java B2B2C Springboot电子商务平台源码-Feign设计原理
阅读量:5942 次
发布时间:2019-06-19

本文共 7633 字,大约阅读时间需要 25 分钟。

什么是Feign?

Feign 的英文表意为“假装,伪装,变形”, 是一个http请求调用的轻量级框架,可以以Java接口注解的方式调用Http请求,而不用像Java中通过封装HTTP请求报文的方式直接调用。Feign通过处理注解,将请求模板化,当实际调用的时候,传入参数,根据参数再应用到请求上,进而转化成真正的请求,这种请求相对而言比较直观。

Feign被广泛应用在Spring Cloud 的解决方案中,是学习基于Spring Cloud 微服务架构不可或缺的重要组件。

1、如何启用

启动配置上检查是否有@EnableFeignClients注解,如果有该注解,则开启包扫描,扫描被@FeignClient注解接口。扫描出该注解后,通过beanDefinition注入到IOC容器中,方便后续被调用使用。

在FeignClientsRegistrar中,registerFeignClients()完成了注册feign的操作。

public void registerFeignClients(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {        ClassPathScanningCandidateComponentProvider scanner = this.getScanner();        scanner.setResourceLoader(this.resourceLoader);        Map
attrs = metadata.getAnnotationAttributes(EnableFeignClients.class.getName()); ......//遍历该项目所需调用的服务Iterator var17 = ((Set)basePackages).iterator(); while(var17.hasNext()) { String basePackage = (String)var17.next(); Set
candidateComponents = scanner.findCandidateComponents(basePackage); Iterator var21 = candidateComponents.iterator(); while(var21.hasNext()) { BeanDefinition candidateComponent = (BeanDefinition)var21.next(); if (candidateComponent instanceof AnnotatedBeanDefinition) { AnnotatedBeanDefinition beanDefinition = (AnnotatedBeanDefinition)candidateComponent; //获取feign中的详细信息 AnnotationMetadata annotationMetadata = beanDefinition.getMetadata(); Map
attributes = annotationMetadata.getAnnotationAttributes(FeignClient.class.getCanonicalName()); String name = this.getClientName(attributes); //注册配置信息 this.registerClientConfiguration(registry, name, attributes.get("configuration")); //注册feign客户端 this.registerFeignClient(registry, annotationMetadata, attributes); } } } }

注册feign客户端,包括使用注解时配置的所有信息。

private void registerFeignClient(BeanDefinitionRegistry registry, AnnotationMetadata annotationMetadata, Map
attributes) { String className = annotationMetadata.getClassName(); BeanDefinitionBuilder definition = BeanDefinitionBuilder.genericBeanDefinition(FeignClientFactoryBean.class); this.validate(attributes); definition.addPropertyValue("url", this.getUrl(attributes)); definition.addPropertyValue("path", this.getPath(attributes)); String name = this.getName(attributes); definition.addPropertyValue("name", name); definition.addPropertyValue("type", className); definition.addPropertyValue("decode404", attributes.get("decode404")); definition.addPropertyValue("fallback", attributes.get("fallback")); definition.addPropertyValue("fallbackFactory", attributes.get("fallbackFactory")); definition.setAutowireMode(2); String alias = name + "FeignClient"; AbstractBeanDefinition beanDefinition = definition.getBeanDefinition(); boolean primary = ((Boolean)attributes.get("primary")).booleanValue(); beanDefinition.setPrimary(primary); String qualifier = this.getQualifier(attributes); if (StringUtils.hasText(qualifier)) { alias = qualifier; } BeanDefinitionHolder holder = new BeanDefinitionHolder(beanDefinition, className, new String[]{alias}); BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry); }

2、如何发起请求

ReflectiveFeign内部使用了jdk的动态代理为目标接口生成了一个动态代理类,这里会生成一个InvocationHandler(jdk动态代理原理)统一的方法拦截器,同时为接口的每个方法生成一个SynchronousMethodHandler拦截器,并解析方法上的 元数据,生成一个http请求模板。

在SynchronousMethodHandler类中生成RequestTemplate发起请求。

public Object invoke(Object[] argv) throws Throwable {    RequestTemplate template = buildTemplateFromArgs.create(argv);    Retryer retryer = this.retryer.clone();    while (true) {      try {        return executeAndDecode(template);      } catch (RetryableException e) {        retryer.continueOrPropagate(e);        if (logLevel != Logger.Level.NONE) {          logger.logRetry(metadata.configKey(), logLevel);        }        continue;      }    }  }
Object executeAndDecode(RequestTemplate template) throws Throwable {    Request request = targetRequest(template);    Response response;    long start = System.nanoTime();    try {      response = client.execute(request, options);      // ensure the request is set. TODO: remove in Feign 10      response.toBuilder().request(request).build();    } catch (IOException e) {      if (logLevel != Logger.Level.NONE) {        logger.logIOException(metadata.configKey(), logLevel, e, elapsedTime(start));      }      throw errorExecuting(request, e);    }  }

发送http请求

@Override    public Response execute(Request request, Options options) throws IOException {      HttpURLConnection connection = convertAndSend(request, options);      return convertResponse(connection).toBuilder().request(request).build();    }    HttpURLConnection convertAndSend(Request request, Options options) throws IOException {      final HttpURLConnection          connection =          (HttpURLConnection) new URL(request.url()).openConnection();      if (connection instanceof HttpsURLConnection) {        HttpsURLConnection sslCon = (HttpsURLConnection) connection;        if (sslContextFactory != null) {          sslCon.setSSLSocketFactory(sslContextFactory);        }        if (hostnameVerifier != null) {          sslCon.setHostnameVerifier(hostnameVerifier);        }      }      connection.setConnectTimeout(options.connectTimeoutMillis());      connection.setReadTimeout(options.readTimeoutMillis());      connection.setAllowUserInteraction(false);      connection.setInstanceFollowRedirects(true);      connection.setRequestMethod(request.method());      Collection
contentEncodingValues = request.headers().get(CONTENT_ENCODING); boolean gzipEncodedRequest = contentEncodingValues != null && contentEncodingValues.contains(ENCODING_GZIP); boolean deflateEncodedRequest = contentEncodingValues != null && contentEncodingValues.contains(ENCODING_DEFLATE); boolean hasAcceptHeader = false; Integer contentLength = null; for (String field : request.headers().keySet()) { if (field.equalsIgnoreCase("Accept")) { hasAcceptHeader = true; } for (String value : request.headers().get(field)) { if (field.equals(CONTENT_LENGTH)) { if (!gzipEncodedRequest && !deflateEncodedRequest) { contentLength = Integer.valueOf(value); connection.addRequestProperty(field, value); } } else { connection.addRequestProperty(field, value); } } } // Some servers choke on the default accept string. if (!hasAcceptHeader) { connection.addRequestProperty("Accept", "*/*"); } if (request.body() != null) { if (contentLength != null) { connection.setFixedLengthStreamingMode(contentLength); } else { connection.setChunkedStreamingMode(8196); } connection.setDoOutput(true); OutputStream out = connection.getOutputStream(); if (gzipEncodedRequest) { out = new GZIPOutputStream(out); } else if (deflateEncodedRequest) { out = new DeflaterOutputStream(out); } try { out.write(request.body()); } finally { try { out.close(); } catch (IOException suppressed) { // NOPMD } } } return connection; }

转载地址:http://esqtx.baihongyu.com/

你可能感兴趣的文章
Linux Namespace系列(09):利用Namespace创建一个简单可用的容器
查看>>
关于缓存命中率的几个关键问题!
查看>>
oracle中create table with as和insert into with as语句
查看>>
kafka连接异常
查看>>
11g废弃的Hint - BYPASS_UJVC
查看>>
为什么工业控制系统需要安全防护?
查看>>
Mongodb部署记录[3]-主从搭建
查看>>
hive sql操作
查看>>
tomcat 深度优化
查看>>
127 - "Accordian" Patience
查看>>
安卓完全退出程序的六种方法(欢迎新手学习,大手指导)
查看>>
elasticsearch 结构化搜索_在案例中实战基于range filter来进行范围过滤
查看>>
double free or corruption (fasttop)
查看>>
Mac 常用快捷键
查看>>
阿里云CentOS7安装Oracle11GR2
查看>>
python常用的字串格式化选项
查看>>
Lock wait timeout exceeded; try restarting......
查看>>
Servet映射规范翻译
查看>>
手机APP新“战场” 手机银行APP成了银行业的定时炸弹?
查看>>
bash变量和逻辑运算
查看>>