zoukankan      html  css  js  c++  java
  • Http请求优化

    Http请求优化

    我们在做项目开发或多或少的都会使用SpringCloud,其中做远程调度的时候会将HTTP请求Http请求优化。

    HTTP请求Client存在很多种。

    • JDK原生的URLConnection 发送HTTP请求,没有连接池,是对每一个地址保持一个长连接,利用HTTP的persistence connection。这样极大的影响系统的并发性能。我们可以采用替换方案。

    实例:

    • 我们使用Feign是 默认就是使用JDK的HTTP Client;

    这时我们就可以使用别的Client进行替换:

    • 比如Apache的HttpClient 来替换原始的HttpClient。可以通过设置连接池,以及访问超时时间等服务之间的调用进行优化。
    • OKHTTP: okHttp也是一个很不错的HttpClient,具有很多优秀的特性:
      • 支持SPDY,可以合并多个到同一主机的请求;
      • 使用连接池技术减少请求的延迟;
      • 使用GZIP压缩减少传输的数据量;
      • 缓存响应避免重复的网络请求。 
      •  1 <dependency>
         2             <groupId>com.netflix.feign</groupId>
         3             <artifactId>feign-httpclient</artifactId>
         4             <version>8.17.0</version>
         5         </dependency>
         6 
         7         <dependency>
         8             <groupId>io.github.openfeign</groupId>
         9             <artifactId>feign-okhttp</artifactId>
        10         </dependency>
        11     </dependencies>
        1 feign:
        2   httpclient:
        3     enabled: false
        4   okhttp:
        5     enabled: true
      •  1 @Configuration
         2 @ConditionalOnClass(Feign.class)
         3 @AutoConfigureBefore(FeignAutoConfiguration.class)
         4 public class FeignOkHttpConfig {
         5     @Bean
         6     public okhttp3.OkHttpClient okHttpClient(){
         7         return new okhttp3.OkHttpClient.Builder()
         8                  //设置连接超时
         9                 .connectTimeout(60, TimeUnit.SECONDS)
        10                 //设置读超时
        11                 .readTimeout(60, TimeUnit.SECONDS)
        12                 //设置写超时
        13                 .writeTimeout(60,TimeUnit.SECONDS)
        14                 //是否自动重连
        15                 .retryOnConnectionFailure(true)
        16                 .connectionPool(new ConnectionPool())
        17                 //构建OkHttpClient对象
        18                 .build();
        19     }
        20 
        21 }
  • 相关阅读:
    php ReflectionObject类
    is_callable
    Zend Framework: Accessing Request, Response & Router Object from anywhere
    (转)zend异常处理
    call_user_func
    zend framework基础教程6—视图
    php func_get_arg
    zend framework基础教程3—创建index.php
    【算法02】3种方法求解斐波那契数列
    【算法01】寻找丑数
  • 原文地址:https://www.cnblogs.com/Mao-admin/p/10114603.html
Copyright © 2011-2022 走看看