记得上下班打卡 | git大法好,push需谨慎

Commit 9398113b authored by anjiabin's avatar anjiabin

增加okhttp配置类

parent b4ec3b58
......@@ -19,7 +19,10 @@ public class HttpUtil {
private static final RestTemplate restTemplate;
static {
//apache httpClient
restTemplate = RestTemplateConfig.getRestTemplate();
//okhttp3 client
// restTemplate = RestTemplateOkConfig.getRestTemplate();
}
/**
......
package com.liquidnet.commons.lang.util.spring;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import java.util.concurrent.TimeUnit;
/**
* @author AnJiabin <anjiabin@zhengzai.tv>
* @version V1.0
* @Description: TODO
* @class: RestTemplateOkConfig
* @Package com.liquidnet.commons.lang.util.spring
* @Copyright: LightNet @ Copyright (c) 2022
* @date 2022/6/9 11:56
*/
public class RestTemplateOkConfig {
private static RestTemplate restTemplate;
static{
long startTime = System.currentTimeMillis();
ConnectionPool pool = new ConnectionPool(500, 5, TimeUnit.MINUTES);
OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
.connectionPool(pool)
.connectTimeout(2, TimeUnit.MINUTES)
.readTimeout(2, TimeUnit.MINUTES)
.writeTimeout(2, TimeUnit.MINUTES)
.hostnameVerifier((hostname, session) -> true)
// 设置代理
// .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 8888)))
// 拦截器
// .addInterceptor()
.build();
ClientHttpRequestFactory factory = new OkHttp3ClientHttpRequestFactory(okHttpClient);
restTemplate = new RestTemplate(factory);
System.out.println("初始化 RestTemplate,bean name :{}"+"restTemplate");
System.out.println("初始化 ok RestTemplate总耗时"+(System.currentTimeMillis()-startTime));
}
public static RestTemplate getRestTemplate(){
return restTemplate;
}
private static int getMaxCpuCore(){
int cpuCore = Runtime.getRuntime().availableProcessors();
return cpuCore;
}
}
......@@ -77,6 +77,11 @@
<artifactId>fst</artifactId>
<version>2.57</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.3</version>
</dependency>
</dependencies>
<dependencyManagement>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment