SpringCloud Feign使用ApacheHttpClient代替默认client方式

目录
  • 使用ApacheHttpClient代替默认client
    • ApacheHttpClient和默认实现的比较
    • ApacheHttpClient使用
  • apache的HttpClient默认重试机制
    • maven
    • 异常重试log
    • RetryExec
    • DefaultHttpRequestRetryHandler

使用ApacheHttpClient代替默认client

ApacheHttpClient和默认实现的比较

  • Feign在默认情况下使用的是JDK原生的URLConnection发送HTTP请求,没有连接池,但是对每个地址会保持一个长连接,即利用HTTP的persistence connection。
  • ApacheHttpClient实现了连接池,同时它封装了访问http的请求头,参数,内容体,响应等等,使客户端发送 HTTP 请求变得容易。

ApacheHttpClient 使用

maven 依赖

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.7</version>
    </dependency>
    <dependency>
        <groupId>io.github.openfeign</groupId>
        <artifactId>feign-httpclient</artifactId>
        <version>10.1.0</version>
    </dependency>

配置文件的修改

feign:
  httpclient:
    enabled: true

创建ApacheHttpClient客户端

import javax.net.ssl.SSLContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.SSLContexts;
import org.springframework.util.ResourceUtils;
import feign.httpclient.ApacheHttpClient;
@Slf4j
public class FeignClientBuilder {
  private boolean enabled;
  private String keyPassword;
  private String keyStore;
  private String keyStorePassword;
  private String trustStore;
  private String trustStorePassword;
  private int maxConnTotal = 2048;
  private int maxConnPerRoute = 512;
  public FeignClientBuilder(boolean enabled, String keyPassword, String keyStore, String keyStorePassword, String trustStore, String trustStorePassword, int maxConnTotal, int maxConnPerRoute) {
    this.enabled = enabled;
    this.keyPassword = keyPassword;
    this.keyStore = keyStore;
    this.keyStorePassword = keyStorePassword;
    this.trustStore = trustStore;
    this.trustStorePassword = trustStorePassword;
    /**
     * maxConnTotal是同时间正在使用的最多的连接数
     */
    this.maxConnTotal = maxConnTotal;
    /**
     * maxConnPerRoute是针对一个域名同时间正在使用的最多的连接数
     */
    this.maxConnPerRoute = maxConnPerRoute;
  }
  public ApacheHttpClient apacheHttpClient() {
    CloseableHttpClient defaultHttpClient = HttpClients.custom()
            .setMaxConnTotal(maxConnTotal)
            .setMaxConnPerRoute(maxConnPerRoute)
            .build();
    ApacheHttpClient defaultApacheHttpClient = new ApacheHttpClient(defaultHttpClient);
    if (!enabled) {
      return defaultApacheHttpClient;
    }
    SSLContextBuilder sslContextBuilder = SSLContexts.custom();
    // 如果 服务端启用了 TLS 客户端验证,则需要指定 keyStore
    if (keyStore == null || keyStore.isEmpty()) {
      return new ApacheHttpClient();
    } else {
      try {
        sslContextBuilder
                .loadKeyMaterial(
                        ResourceUtils.getFile(keyStore),
                        keyStorePassword.toCharArray(),
                        keyPassword.toCharArray());
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    // 如果 https 使用自签名证书,则需要指定 trustStore
    if (trustStore == null || trustStore.isEmpty()) {
    } else {
      try {
        sslContextBuilder
//        .loadTrustMaterial(TrustAllStrategy.INSTANCE)
                .loadTrustMaterial(
                        ResourceUtils.getFile(trustStore),
                        trustStorePassword.toCharArray()
                );
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    try {
      SSLContext sslContext = sslContextBuilder.build();
      SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
              sslContext,
              SSLConnectionSocketFactory.getDefaultHostnameVerifier());
      CloseableHttpClient httpClient = HttpClients.custom()
              .setMaxConnTotal(maxConnTotal)
              .setMaxConnPerRoute(maxConnPerRoute)
              .setSSLSocketFactory(sslsf)
              .build();
      ApacheHttpClient apacheHttpClient = new ApacheHttpClient(httpClient);
      log.info("feign Client load with ssl.");
      return apacheHttpClient;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return defaultApacheHttpClient;
  }
  public static FeignClientBuilderBuilder builder() {
    return new FeignClientBuilderBuilder();
  }
  public static class FeignClientBuilderBuilder {
    private boolean enabled;
    private String keyPassword;
    private String keyStore;
    private String keyStorePassword;
    private String trustStore;
    private String trustStorePassword;
    private int maxConnTotal = 2048;
    private int maxConnPerRoute = 512;
    public FeignClientBuilderBuilder enabled(boolean enabled) {
      this.enabled = enabled;
      return this;
    }
    public FeignClientBuilderBuilder keyPassword(String keyPassword) {
      this.keyPassword = keyPassword;
      return this;
    }
    public FeignClientBuilderBuilder keyStore(String keyStore) {
      this.keyStore = keyStore;
      return this;
    }
    public FeignClientBuilderBuilder keyStorePassword(String keyStorePassword) {
      this.keyStorePassword = keyStorePassword;
      return this;
    }
    public FeignClientBuilderBuilder trustStore(String trustStore) {
      this.trustStore = trustStore;
      return this;
    }
    public FeignClientBuilderBuilder trustStorePassword(String trustStorePassword) {
      this.trustStorePassword = trustStorePassword;
      return this;
    }
    public FeignClientBuilderBuilder maxConnTotal(int maxConnTotal) {
      this.maxConnTotal = maxConnTotal;
      return this;
    }
    public FeignClientBuilderBuilder maxConnPerRoute(int maxConnPerRoute) {
      this.maxConnPerRoute = maxConnPerRoute;
      return this;
    }
    public FeignClientBuilder build() {
      return new FeignClientBuilder(
              this.enabled,
              this.keyPassword,
              this.keyStore,
              this.keyStorePassword,
              this.trustStore,
              this.trustStorePassword,
              this.maxConnTotal,
              this.maxConnPerRoute
      );
    }
  }
}

使用时可以直接使用builder来创建ApacheHttpClient。

apache的HttpClient默认重试机制

maven

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version>
        </dependency>

异常重试log

2017-01-31 19:31:39.057  INFO 3873 --- [askScheduler-13] o.apache.http.impl.execchain.RetryExec   : I/O exception (org.apache.http.NoHttpResponseException) caught when processing request to {}->http://192.168.99.100:8080: The target server failed to respond
2017-01-31 19:31:39.058  INFO 3873 --- [askScheduler-13] o.apache.http.impl.execchain.RetryExec   : Retrying request to {}->http://192.168.99.100:8080

RetryExec

org/apache/http/impl/execchain/RetryExec.java

/**
 * Request executor in the request execution chain that is responsible
 * for making a decision whether a request failed due to an I/O error
 * should be re-executed.
 * <p>
 * Further responsibilities such as communication with the opposite
 * endpoint is delegated to the next executor in the request execution
 * chain.
 * </p>
 *
 * @since 4.3
 */
@Immutable
public class RetryExec implements ClientExecChain {
    private final Log log = LogFactory.getLog(getClass());
    private final ClientExecChain requestExecutor;
    private final HttpRequestRetryHandler retryHandler;
    public RetryExec(
            final ClientExecChain requestExecutor,
            final HttpRequestRetryHandler retryHandler) {
        Args.notNull(requestExecutor, "HTTP request executor");
        Args.notNull(retryHandler, "HTTP request retry handler");
        this.requestExecutor = requestExecutor;
        this.retryHandler = retryHandler;
    }
    @Override
    public CloseableHttpResponse execute(
            final HttpRoute route,
            final HttpRequestWrapper request,
            final HttpClientContext context,
            final HttpExecutionAware execAware) throws IOException, HttpException {
        Args.notNull(route, "HTTP route");
        Args.notNull(request, "HTTP request");
        Args.notNull(context, "HTTP context");
        final Header[] origheaders = request.getAllHeaders();
        for (int execCount = 1;; execCount++) {
            try {
                return this.requestExecutor.execute(route, request, context, execAware);
            } catch (final IOException ex) {
                if (execAware != null && execAware.isAborted()) {
                    this.log.debug("Request has been aborted");
                    throw ex;
                }
                if (retryHandler.retryRequest(ex, execCount, context)) {
                    if (this.log.isInfoEnabled()) {
                        this.log.info("I/O exception ("+ ex.getClass().getName() +
                                ") caught when processing request to "
                                + route +
                                ": "
                                + ex.getMessage());
                    }
                    if (this.log.isDebugEnabled()) {
                        this.log.debug(ex.getMessage(), ex);
                    }
                    if (!RequestEntityProxy.isRepeatable(request)) {
                        this.log.debug("Cannot retry non-repeatable request");
                        throw new NonRepeatableRequestException("Cannot retry request " +
                                "with a non-repeatable request entity", ex);
                    }
                    request.setHeaders(origheaders);
                    if (this.log.isInfoEnabled()) {
                        this.log.info("Retrying request to " + route);
                    }
                } else {
                    if (ex instanceof NoHttpResponseException) {
                        final NoHttpResponseException updatedex = new NoHttpResponseException(
                                route.getTargetHost().toHostString() + " failed to respond");
                        updatedex.setStackTrace(ex.getStackTrace());
                        throw updatedex;
                    } else {
                        throw ex;
                    }
                }
            }
        }
    }
}

DefaultHttpRequestRetryHandler

org/apache/http/impl/client/DefaultHttpRequestRetryHandler.java

/**
 * The default {@link HttpRequestRetryHandler} used by request executors.
 *
 * @since 4.0
 */
@Immutable
public class DefaultHttpRequestRetryHandler implements HttpRequestRetryHandler {
    public static final DefaultHttpRequestRetryHandler INSTANCE = new DefaultHttpRequestRetryHandler();
    /** the number of times a method will be retried */
    private final int retryCount;
    /** Whether or not methods that have successfully sent their request will be retried */
    private final boolean requestSentRetryEnabled;
    private final Set<Class<? extends IOException>> nonRetriableClasses;
    /**
     * Create the request retry handler using the specified IOException classes
     *
     * @param retryCount how many times to retry; 0 means no retries
     * @param requestSentRetryEnabled true if it's OK to retry requests that have been sent
     * @param clazzes the IOException types that should not be retried
     * @since 4.3
     */
    protected DefaultHttpRequestRetryHandler(
            final int retryCount,
            final boolean requestSentRetryEnabled,
            final Collection<Class<? extends IOException>> clazzes) {
        super();
        this.retryCount = retryCount;
        this.requestSentRetryEnabled = requestSentRetryEnabled;
        this.nonRetriableClasses = new HashSet<Class<? extends IOException>>();
        for (final Class<? extends IOException> clazz: clazzes) {
            this.nonRetriableClasses.add(clazz);
        }
    }
    /**
     * Create the request retry handler using the following list of
     * non-retriable IOException classes: <br>
     * <ul>
     * <li>InterruptedIOException</li>
     * <li>UnknownHostException</li>
     * <li>ConnectException</li>
     * <li>SSLException</li>
     * </ul>
     * @param retryCount how many times to retry; 0 means no retries
     * @param requestSentRetryEnabled true if it's OK to retry non-idempotent requests that have been sent
     */
    @SuppressWarnings("unchecked")
    public DefaultHttpRequestRetryHandler(final int retryCount, final boolean requestSentRetryEnabled) {
        this(retryCount, requestSentRetryEnabled, Arrays.asList(
                InterruptedIOException.class,
                UnknownHostException.class,
                ConnectException.class,
                SSLException.class));
    }
    /**
     * Create the request retry handler with a retry count of 3, requestSentRetryEnabled false
     * and using the following list of non-retriable IOException classes: <br>
     * <ul>
     * <li>InterruptedIOException</li>
     * <li>UnknownHostException</li>
     * <li>ConnectException</li>
     * <li>SSLException</li>
     * </ul>
     */
    public DefaultHttpRequestRetryHandler() {
        this(3, false);
    }
    /**
     * Used {@code retryCount} and {@code requestSentRetryEnabled} to determine
     * if the given method should be retried.
     */
    @Override
    public boolean retryRequest(
            final IOException exception,
            final int executionCount,
            final HttpContext context) {
        Args.notNull(exception, "Exception parameter");
        Args.notNull(context, "HTTP context");
        if (executionCount > this.retryCount) {
            // Do not retry if over max retry count
            return false;
        }
        if (this.nonRetriableClasses.contains(exception.getClass())) {
            return false;
        } else {
            for (final Class<? extends IOException> rejectException : this.nonRetriableClasses) {
                if (rejectException.isInstance(exception)) {
                    return false;
                }
            }
        }
        final HttpClientContext clientContext = HttpClientContext.adapt(context);
        final HttpRequest request = clientContext.getRequest();
        if(requestIsAborted(request)){
            return false;
        }
        if (handleAsIdempotent(request)) {
            // Retry if the request is considered idempotent
            return true;
        }
        if (!clientContext.isRequestSent() || this.requestSentRetryEnabled) {
            // Retry if the request has not been sent fully or
            // if it's OK to retry methods that have been sent
            return true;
        }
        // otherwise do not retry
        return false;
    }
    /**
     * @return {@code true} if this handler will retry methods that have
     * successfully sent their request, {@code false} otherwise
     */
    public boolean isRequestSentRetryEnabled() {
        return requestSentRetryEnabled;
    }
    /**
     * @return the maximum number of times a method will be retried
     */
    public int getRetryCount() {
        return retryCount;
    }
    /**
     * @since 4.2
     */
    protected boolean handleAsIdempotent(final HttpRequest request) {
        return !(request instanceof HttpEntityEnclosingRequest);
    }
    /**
     * @since 4.2
     *
     * @deprecated (4.3)
     */
    @Deprecated
    protected boolean requestIsAborted(final HttpRequest request) {
        HttpRequest req = request;
        if (request instanceof RequestWrapper) { // does not forward request to original
            req = ((RequestWrapper) request).getOriginal();
        }
        return (req instanceof HttpUriRequest && ((HttpUriRequest)req).isAborted());
    }
}

默认重试3次,三次都失败则抛出NoHttpResponseException或其他异常

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • spring cloud 之 Feign 使用HTTP请求远程服务的实现方法

    一.Feign 简介 在spring Cloud Netflix栈中,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使用HTTP客户端.我们可以使用JDK原生的URLConnection.Apache的Http Client.Netty的异步HTTP Client, Spring的RestTemplate.但是,用起来最方便.最优雅的还是要属Feign了. Feign是一种声明式.模板化的HTTP客户端.在Spring Cloud中使用Feign, 我们可以做到使用

  • 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务的方法(推荐)

    在Spring Cloud Netflix栈中,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使用HTTP客户端.我们可以使用JDK原生的URLConnection.Apache的Http Client.Netty的异步HTTP Client, Spring的RestTemplate.但是,用起来最方便.最优雅的还是要属Feign了. Feign简介 Feign是一种声明式.模板化的HTTP客户端.在Spring Cloud中使用Feign, 我们可以做到使用HTT

  • 完美解决SpringCloud-OpenFeign使用okhttp替换不生效问题

    事发地 原默认的Feign是使用URLConnector进行通信的,当换为okhttp时,直接引入包及配置以下内容根本不生效,还是走原生的. feign: okhttp: enable: true 事件还原 创建项目并引入pom相关的依赖如下: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xml

  • 使用okhttp替换Feign默认Client的操作

    一 关键pom <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Cloud OpenFeign的Starter的依赖 --> <dependency> &l

  • SpringCloud Feign使用ApacheHttpClient代替默认client方式

    目录 使用ApacheHttpClient代替默认client ApacheHttpClient和默认实现的比较 ApacheHttpClient使用 apache的HttpClient的默认重试机制 maven 异常重试log RetryExec DefaultHttpRequestRetryHandler 使用ApacheHttpClient代替默认client ApacheHttpClient和默认实现的比较 Feign在默认情况下使用的是JDK原生的URLConnection发送HTTP

  • SpringCloud Feign使用ApacheHttpClient代替默认client方式

    目录 使用ApacheHttpClient代替默认client ApacheHttpClient和默认实现的比较 ApacheHttpClient使用 apache的HttpClient默认重试机制 maven 异常重试log RetryExec DefaultHttpRequestRetryHandler 使用ApacheHttpClient代替默认client ApacheHttpClient和默认实现的比较 Feign在默认情况下使用的是JDK原生的URLConnection发送HTTP请

  • springcloud feign 接口指定接口服务ip方式

    目录 feign接口指定接口服务ip 场景 调用Feign接口时指定ip 只指定服务名 指定ip feign接口指定接口服务ip 场景 现在有2个服务,在eureka注册的服务名称一样,但是对外的接口不一样.其中有一方不允许合并代码,只能把另一个调用指定一下具体的服务地址 @FeignClient(name = "服务名称",url = "${url}",fallback = ServiceHystrix.class) public interface Servic

  • springcloud feign 接口指定接口服务ip方式

    目录 feign接口指定接口服务ip 场景 调用feign接口时指定ip 只指定服务名 指定ip feign接口指定接口服务ip 场景 现在有2个服务,在eureka注册的服务名称一样,但是对外的接口不一样.其中有一方不允许合并代码,只能把另一个调用指定一下具体的服务地址 @FeignClient(name = "服务名称",url = "${url}",fallback = ServiceHystrix.class) public interface Servic

  • 浅谈SpringCloud feign的http请求组件优化方案

    1 描述 如果我们直接使用SpringCloud Feign进行服务间调用的时候,http组件使用的是JDK的HttpURLConnection,每次请求都会新建一个连接,没有使用线程池复用.具体的可以从源码进行分析 2 源码分析 我们在分析源码很难找到入口,不知道从何开始入手,我们在分析SpringCloud feign的时候可用在配置文件下面我讲一下个人的思路. 1 首先我点击@EnableFeignClients 看一下这个注解在哪个资源路径下 如下图所示: 2 找到服务启动加载的配置文件

  • Java之Springcloud Feign组件详解

    一.Feign是什么? OpenFeign是Spring Cloud提供的一个声明式的伪Hltp客户端,它使得调用远程服务就像调用本地服务一样简单,只需要创建一个接口并添加一个注解即可,Nacos很好的兼容了OpenFeign,OpenFeign默认集成了Ribbon, 所以在Nacos下使用OpenFeign默认就实现了负载均衡的效果. 二.使用步骤 1.消费方导入依赖 ···c org.springframework.cloud spring-cloud-starter-openfeign

  • SpringCloud Feign超详细讲解

    目录 一.什么是Feign 二.Feign能干什么 三.Feign的使用步骤 1.新建一个module 2.配置Pom.xml 3.配置applicatin.yaml 4.配置configBean 5.配置Controller类 6.配置启动类 7.改动API 1)引入Feign依赖 2)配置Service 3)注意 四.结果 一.什么是Feign Feign是声明式Web Service客户端,它让微服务之间的调用变得更简单,类似controller调用service.SpringCloud集

  • SpringCloud Feign远程调用实现详解

    目录 1. Feign远程调用 1.1.Feign替代RestTemplate 1.2.自定义配置 1.2.1.配置文件方式 1.2.2.Java代码方式 2.Feign使用优化 3. 最佳实践 3.1.继承方式 3.2.抽取方式 3.3.实现基于抽取的最佳实践 先来看我们以前利用RestTemplate发起远程调用的代码: 存在下面的问题: 代码可读性差,编程体验不统一 参数复杂URL难以维护 1. Feign远程调用 Feign是一个声明式的http客户端,官方地址:https://gith

  • SpringCloud Feign客户端使用流程

    目录 一.HTTP客户端Feign 1.1RestTemplate方式调用存在的问题 1.2Feign的介绍 1.3Feign的使用 1.4自定义Feign的配置 1.5Feign性能优化 1.6Feign的最佳实践 一.HTTP客户端Feign 1.1RestTemplate方式调用存在的问题 以前我用使用RestTemplate发起远程调用的代码: String url = "http://userservice/user/" + order.getUserId(); User u

  • 解决SpringCloud Feign传对象参数调用失败的问题

    SpringCloud Feign传对象参数调用失败 不支持GET请求方式 使用Apache HttpClient替换Feign原生httpclient @RequestBody接收json参数 bootstrap-local.yml feign: httpclient: enabled: true pom.xml <!-- 使用Apache HttpClient替换Feign原生httpclient --> <dependency> <groupId>com.netf

随机推荐