iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >SpringCloud Feign中怎么使用ApacheHttpClient代替默认client方式
  • 324
分享到

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

2023-06-29 10:06:31 324人浏览 安东尼
摘要

这篇文章主要讲解了“SpringCloud Feign中怎么使用ApacheHttpClient代替默认client方式”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“springC

这篇文章主要讲解了“SpringCloud Feign中怎么使用ApacheHttpClient代替默认client方式”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“springCloud Feign中怎么使用ApacheHttpClient代替默认client方式”吧!

使用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;@Slf4jpublic 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;        this.maxConnTotal = maxConnTotal;        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

@Immutablepublic 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

@Immutablepublic class DefaultHttpRequestRetryHandler implements HttpRequestRetryHandler {    public static final DefaultHttpRequestRetryHandler INSTANCE = new DefaultHttpRequestRetryHandler();        private final int retryCount;        private final boolean requestSentRetryEnabled;    private final Set<Class<? extends IOException>> nonRetriableClasses;        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);        }    }        @SuppressWarnings("unchecked")    public DefaultHttpRequestRetryHandler(final int retryCount, final boolean requestSentRetryEnabled) {        this(retryCount, requestSentRetryEnabled, Arrays.asList(                InterruptedIOException.class,                UnknownHostException.class,                ConnectException.class,                SSLException.class));    }        public DefaultHttpRequestRetryHandler() {        this(3, false);    }        @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;    }        public boolean isRequestSentRetryEnabled() {        return requestSentRetryEnabled;    }        public int getRetryCount() {        return retryCount;    }        protected boolean handleAsIdempotent(final HttpRequest request) {        return !(request instanceof HttpEntityEnclosingRequest);    }        @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或其他异常

感谢各位的阅读,以上就是“SprinGCloud Feign中怎么使用ApacheHttpClient代替默认client方式”的内容了,经过本文的学习后,相信大家对SpringCloud Feign中怎么使用ApacheHttpClient代替默认client方式这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!

--结束END--

本文标题: SpringCloud Feign中怎么使用ApacheHttpClient代替默认client方式

本文链接: https://www.lsjlt.com/news/324305.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • SpringCloud Feign使用ApacheHttpClient代替默认client方式
    目录使用ApacheHttpClient代替默认clientApacheHttpClient和默认实现的比较ApacheHttpClient使用apache的HttpClient默认...
    99+
    2024-04-02
  • SpringCloud Feign中怎么使用ApacheHttpClient代替默认client方式
    这篇文章主要讲解了“SpringCloud Feign中怎么使用ApacheHttpClient代替默认client方式”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“SpringC...
    99+
    2023-06-29
  • 怎么在java接口中使用默认方法
    本篇文章为大家展示了怎么在java接口中使用默认方法,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。java基本数据类型有哪些Java的基本数据类型分为:1、整数类型,用来表示整数的数据类型。2、浮点...
    99+
    2023-06-14
  • java接口怎么使用默认方法
    本篇内容介绍了“java接口怎么使用默认方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!1、接口中默认方法的格式:public ...
    99+
    2023-06-30
  • 怎么在python中使用参数默认值
    本篇文章给大家分享的是有关怎么在python中使用参数默认值,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。python的数据类型有哪些python的数据类型:1. 数字类型,包...
    99+
    2023-06-14
  • vue3项目怎么使用样式穿透修改elementUI默认样式
    .hello-world-box[data-v-e17ea971] { color: red; }这也是style标签scoped属性实现样式模块化的原理。当一个style标签拥有scoped属性时,它的CSS样式就只能作用于当前的...
    99+
    2023-05-14
    Vue3 elementui
  • C++中怎么使用=default生成默认构造函数
    今天就跟大家聊聊有关C++中怎么使用=default生成默认构造函数,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。从C++基本知识说起struct Point{   &nb...
    99+
    2023-06-19
  • 怎么在R语言中使用ggplot2 修改默认颜色
    这期内容当中小编将会给大家带来有关怎么在R语言中使用ggplot2 修改默认颜色,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。1个元素:2个元素(颜色分配顺序为先从左到右,后从上到下):3个元素:4个元素...
    99+
    2023-06-14
  • vue中怎么使用xlsx插件下载内容默认居中的excel
    这篇文章主要介绍“vue中怎么使用xlsx插件下载内容默认居中的excel”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“vue中怎么使用xlsx插件下载内容默认居中...
    99+
    2024-04-02
  • MySQL中怎么使用Generated Columns + index代替函数索引
    这篇文章主要讲解了“MySQL中怎么使用Generated Columns + index代替函数索引”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“MySQ...
    99+
    2024-04-02
  • vue3中怎么使用props和emits并指定其类型与默认值
    defineProps 的使用defineProps在使用的时候无需引入,默认是全局方法。在 js 开发的 vue3 项目中使用const props = defineProps({ attr1: { type: String,...
    99+
    2023-05-19
    Vue3 props emits
  • Mysql中默认自动事务autocommit关闭和开启方式、rollback回滚恢复数据的使用方法
    文章目录 autocommit自动提交事物一、查看autocommit状态二、修改autocommit 状态的方式1、第一种方式2、修改mysql配置文件my.cnf 二、rollbac...
    99+
    2023-10-18
    mysql 数据库
  • php7+中怎么使用openssl替代mcrypt进行AES加密解密
    这篇文章主要介绍php7+中怎么使用openssl替代mcrypt进行AES加密解密,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!mcrypt十年过去,现在php7+中已经开始淘汰。官方给出掉提示:mcrypt_ge...
    99+
    2023-06-15
  • 怎么在JavaScript中使用replace()方法替换当前页面
    怎么在JavaScript中使用replace()方法替换当前页面?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。JavaScript的特点1.JavaScript主要用来向HT...
    99+
    2023-06-14
  • C#程序中怎么使用泛型集合代替非泛型集合
    C#程序中怎么使用泛型集合代替非泛型集合,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。软件开发过程中,不可避免会用到集合,C#中的集合表现为数...
    99+
    2024-04-02
  • 怎么使用JavaScript中的迭代方法
    这篇文章主要介绍“怎么使用JavaScript中的迭代方法”,在日常操作中,相信很多人在怎么使用JavaScript中的迭代方法问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”...
    99+
    2024-04-02
  • 怎么在JavaScript中是使用replace()方法替换指定位置
    怎么在JavaScript中是使用replace()方法替换指定位置?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。JavaScript的作用是什么1、能够嵌入动态文本于HTML...
    99+
    2023-06-14
  • 怎么使用正则表达式替换JavaScript文本中的换行符
    这篇文章主要介绍“怎么使用正则表达式替换JavaScript文本中的换行符”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“怎么使用正则表达式替换JavaScript文本中的换行符”文章能帮助大家解决问...
    99+
    2023-07-06
  • 怎么使用正则替换的方式实现清除字符串中所有的HTML标签
    这篇文章将为大家详细讲解有关怎么使用正则替换的方式实现清除字符串中所有的HTML标签,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。如何编写一个函数,使用正则替换的方式能够实现清除字符串中所有的HTML标签...
    99+
    2023-06-15
  • linux中怎么使用yum方式安装mysql
    今天就跟大家聊聊有关linux中怎么使用yum方式安装mysql,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。linux下使用yum安装mysql&...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作