嵌入式http服务
简单使用
导入包
// https://mvnrepository.com/artifact/io.undertow/undertow-core
compile group: 'io.undertow', name: 'undertow-core', version: '2.0.23.Final'
public void start() {
if (httpPort == null && httpsPort == null) httpPort = 8080; // by default start http
var watch = new StopWatch();
try {
Undertow.Builder builder = Undertow.builder();
if (httpPort != null) builder.addHttpListener(httpPort, "0.0.0.0");
if (httpsPort != null) builder.addHttpsListener(httpsPort, "0.0.0.0", new SSLContextBuilder().build());
builder.setHandler(handler())
// jdk 11 uses TLSv1.3 as default, which causes various of intermittent handshake or close channel issues under multi threading condition (both JDK httpclient and okHTTP), will check whether it improves on openjdk 11.0.2/12
.setSocketOption(Options.SSL_ENABLED_PROTOCOLS, Sequence.of("TLSv1.2"))
// set tcp back log larger, also requires to update kernel, e.g. sysctl -w net.core.somaxconn=1024 && sysctl -w net.ipv4.tcp_max_syn_backlog=4096
.setSocketOption(Options.BACKLOG, 4096)
.setServerOption(UndertowOptions.DECODE_URL, Boolean.FALSE)
.setServerOption(UndertowOptions.ENABLE_HTTP2, Boolean.TRUE)
.setServerOption(UndertowOptions.ENABLE_RFC6265_COOKIE_VALIDATION, Boolean.TRUE)
// since we don't use Expires or Last- Modified header, so it's not necessary to set Date header, for cache, prefer cache-control/max-age
// refer to https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.18.1
.setServerOption(UndertowOptions.ALWAYS_SET_DATE, Boolean.FALSE)
.setServerOption(UndertowOptions.ALWAYS_SET_KEEP_ALIVE, Boolean.FALSE)
// set tcp idle timeout to 620s, by default AWS ALB uses 60s, GCloud LB uses 600s, since it is always deployed with LB, longer timeout doesn't hurt
// refer to https://cloud.google.com/load-balancing/docs/https/#timeouts_and_retries
// refer to https://docs.aws.amazon.com/elasticloadbalancing/latest/application/application-load-balancers.html#connection-idle-timeout
.setServerOption(UndertowOptions.NO_REQUEST_TIMEOUT, 620 * 1000) // 620s
.setServerOption(UndertowOptions.SHUTDOWN_TIMEOUT, 10 * 1000) // 10s
.setServerOption(UndertowOptions.MAX_ENTITY_SIZE, 10L * 1024 * 1024); // max post body is 10M
server = builder.build();
server.start();
} finally {
logger.info("http server started, httpPort={}, httpsPort={}, gzip={}, elapsed={}", httpPort, httpsPort, gzip, watch.elapsed());
}
}
网友评论