feat: optimize remote http impl

This commit is contained in:
tjq 2023-01-02 12:47:28 +08:00
parent 432adeb00f
commit 4b2d9d4d74
2 changed files with 42 additions and 1 deletions

View File

@ -23,6 +23,17 @@ public class PowerJobDKey {
*/
public static final String IGNORED_NETWORK_INTERFACE_REGEX = "powerjob.network.interface.ignored";
/**
* Enables compression during data transfer, such as gzip under the HTTP protocol. default value is 'false'
* Note that enabling compression reduces network usage, but increases CPU consumption
*/
public static final String TRANSPORTER_USE_COMPRESSING = "powerjob.transporter.compression.enabled";
/**
* keep-alive connection timeout(in seconds), value <= 0 means disable keepalive. default value is 75
*/
public static final String TRANSPORTER_KEEP_ALIVE_TIMEOUT = "powerjob.transporter.keepalive.timeout";
public static final String WORKER_STATUS_CHECK_PERIOD = "powerjob.worker.status-check.normal.period";
/**
* ms

View File

@ -7,6 +7,9 @@ import io.vertx.core.http.HttpClientOptions;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerOptions;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import tech.powerjob.common.OmsConstant;
import tech.powerjob.common.PowerJobDKey;
/**
* VertxInitializer
@ -18,6 +21,13 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j
public class VertxInitializer {
/**
* 默认开启长连接 75S 超时
*/
private static final int DEFAULT_KEEP_ALIVE_TIMEOUT = 75;
private static final int CONNECTION_TIMEOUT_MS = 2000;
public static Vertx buildVertx() {
VertxOptions options = new VertxOptions();
log.info("[PowerJob-Vertx] use vertx options: {}", options);
@ -43,7 +53,27 @@ public class VertxInitializer {
}
public static HttpClient buildHttpClient(Vertx vertx) {
HttpClientOptions httpClientOptions = new HttpClientOptions();
HttpClientOptions httpClientOptions = new HttpClientOptions()
.setMetricsName(OmsConstant.PACKAGE)
.setConnectTimeout(CONNECTION_TIMEOUT_MS)
.setMaxPoolSize(Math.max(8, Runtime.getRuntime().availableProcessors()) * 2);
// 长连接
String keepaliveTimeout = System.getProperty(PowerJobDKey.TRANSPORTER_KEEP_ALIVE_TIMEOUT, String.valueOf(DEFAULT_KEEP_ALIVE_TIMEOUT));
int keepaliveTimeoutInt = Integer.parseInt(keepaliveTimeout);
if (keepaliveTimeoutInt > 0) {
httpClientOptions.setKeepAlive(true).setKeepAliveTimeout(keepaliveTimeoutInt);
} else {
httpClientOptions.setKeepAlive(false);
}
// 压缩判定
String enableCompressing = System.getProperty(PowerJobDKey.TRANSPORTER_USE_COMPRESSING);
if (StringUtils.isNotEmpty(enableCompressing)) {
httpClientOptions.setTryUseCompression(StringUtils.equalsIgnoreCase(enableCompressing, Boolean.TRUE.toString()));
}
log.info("[PowerJob-Vertx] use HttpClientOptions: {}", httpClientOptions.toJson());
return vertx.createHttpClient(httpClientOptions);
}