一、簡介java
OkHttp是一個高效的HTTP客戶端,容許全部同一個主機地址的請求共享同一個socket鏈接;鏈接池減小請求延時;透明的GZIP壓縮減小響應數據的大小;緩存響應內容,避免一些徹底重複的請求web
當網絡出現問題的時候OkHttp依然堅守本身的職責,它會自動恢復通常的鏈接問題,若是你的服務有多個IP地址,當第一個IP請求失敗時,OkHttp會交替嘗試你配置的其餘IP,OkHttp使用現代TLS技術(SNI, ALPN)初始化新的鏈接,當握手失敗時會回退到TLS 1.0。spring
1)使用:它的請求/響應 API 使用構造器模式builders來設計,它支持阻塞式的同步請求和帶回調的異步請求。緩存
<dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>3.10.0</version> </dependency>
import okhttp3.ConnectionPool; import okhttp3.OkHttpClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.http.client.OkHttp3ClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.concurrent.TimeUnit; @Configuration public class OkHttpRestTemplateConfig { @Bean("okHttpRestTemplate") public RestTemplate okHttpRestTemplate() { OkHttpClient httpClient = okHttpClient(); ClientHttpRequestFactory factory = new OkHttp3ClientHttpRequestFactory(httpClient); RestTemplate restTemplate = new RestTemplate(factory); // 能夠添加消息轉換 //restTemplate.setMessageConverters(...); // 能夠增長攔截器 //restTemplate.setInterceptors(...); return restTemplate; } @Bean public OkHttpClient okHttpClient() { return new OkHttpClient.Builder() //.sslSocketFactory(sslSocketFactory(), x509TrustManager()) .retryOnConnectionFailure(false) .connectionPool(pool()) .connectTimeout(30, TimeUnit.SECONDS) .readTimeout(30, TimeUnit.SECONDS) .writeTimeout(30,TimeUnit.SECONDS) .build(); } @Bean public X509TrustManager x509TrustManager() { return new X509TrustManager() { @Override public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { } @Override public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { } @Override public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } }; } @Bean public SSLSocketFactory sslSocketFactory() { try { //信任任何連接 SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, new TrustManager[]{x509TrustManager()}, new SecureRandom()); return sslContext.getSocketFactory(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (KeyManagementException e) { e.printStackTrace(); } return null; } /** * Create a new connection pool with tuning parameters appropriate for a single-user application. * The tuning parameters in this pool are subject to change in future OkHttp releases. Currently */ @Bean public ConnectionPool pool() { return new ConnectionPool(200, 5, TimeUnit.MINUTES); } }