call添加连接池
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
package xtools.cloud.call.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* <p>Title : CallConfig</p>
|
||||
* <p>Description : CallConfig</p>
|
||||
* <p>DevelopTools : Idea_x64_v2026.1</p>
|
||||
* <p>DevelopSystem : macOS Sequoia 15.7.5</p>
|
||||
* <p>Company : org.xujun</p>
|
||||
*
|
||||
* @author : XuJun
|
||||
* @version : 1.0.0
|
||||
* @date : 2026/5/14 11:20
|
||||
*/
|
||||
@Data
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "call")
|
||||
public class CallConfig {
|
||||
|
||||
/**
|
||||
* 客户端配置
|
||||
*/
|
||||
private Client httpClient = new Client();
|
||||
|
||||
/**
|
||||
* 客户端配置
|
||||
*/
|
||||
@Data
|
||||
public static class Client {
|
||||
|
||||
/**
|
||||
* 连接池大小
|
||||
*/
|
||||
private int connectionPoolSize = 500;
|
||||
|
||||
/**
|
||||
* 连接超时时间(秒)
|
||||
*/
|
||||
private int connectTimeout = 10;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,10 +1,15 @@
|
||||
package xtools.cloud.call.config;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.boot.restclient.RestClientCustomizer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.client.JdkClientHttpRequestFactory;
|
||||
import xtools.cloud.call.interceptor.ClientHttpInterceptor;
|
||||
|
||||
import java.net.http.HttpClient;
|
||||
import java.time.Duration;
|
||||
|
||||
/**
|
||||
* <p>Title : RestClientConfig</p>
|
||||
* <p>Description : RestClientConfig</p>
|
||||
@@ -19,6 +24,9 @@ import xtools.cloud.call.interceptor.ClientHttpInterceptor;
|
||||
@Configuration
|
||||
public class RestClientConfig {
|
||||
|
||||
@Resource
|
||||
private CallConfig callConfig;
|
||||
|
||||
/**
|
||||
* RestClient自定义配置
|
||||
*
|
||||
@@ -26,7 +34,17 @@ public class RestClientConfig {
|
||||
*/
|
||||
@Bean
|
||||
public RestClientCustomizer restClientCustomizer() {
|
||||
CallConfig.Client clientConfig = callConfig.getHttpClient();
|
||||
return restClientBuilder -> {
|
||||
// 设置连接池大小
|
||||
System.setProperty("jdk.httpclient.connectionPoolSize", String.valueOf(clientConfig.getConnectionPoolSize()));
|
||||
// 使用JDK内置HttpClient,非阻塞 I/O + 自带连接池,兼容虚拟线程
|
||||
HttpClient httpClient = HttpClient.newBuilder()
|
||||
.connectTimeout(Duration.ofSeconds(clientConfig.getConnectTimeout()))
|
||||
.version(HttpClient.Version.HTTP_2)
|
||||
.build();
|
||||
restClientBuilder.requestFactory(new JdkClientHttpRequestFactory(httpClient));
|
||||
|
||||
// 配置拦截器
|
||||
restClientBuilder.requestInterceptor(clientHttpInterceptor());
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user