前言
spring boot 默认内嵌是tomcat
如果只是想最简单提升一下性能 可以把tomcat 换成 undertow
至于哪里强 可以看看一些web服务器对比 undertow还是很能打的
https://undertow.io/
示例
依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| <spring-boot.version>2.5.6</spring-boot.version> 。。。 <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-undertow</artifactId> </dependency>
|
application.yml配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| server: http: port: 80 port: 443 ssl: key-store-type: JKS key-store: "classpath:{证书名字}.jks" key-password: 证书密码 enabled: true accesslog: enabled: true dir: . pattern: combined undertow: threads: io: 4 worker: 128 compression: enabled: true
|
配置undertow
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| package com.ming.base.config;
import io.undertow.Undertow; import io.undertow.UndertowOptions; import io.undertow.servlet.api.SecurityConstraint; import io.undertow.servlet.api.SecurityInfo; import io.undertow.servlet.api.TransportGuaranteeType; import io.undertow.servlet.api.WebResourceCollection; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.web.embedded.undertow.UndertowBuilderCustomizer; import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory; import org.springframework.boot.web.servlet.server.ServletWebServerFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment;
@Configuration @Slf4j @ConditionalOnProperty(name = "server.ssl.enabled", havingValue = "true", matchIfMissing = false) public class UndertowConfig {
@Autowired private Environment environment;
@Bean public ServletWebServerFactory embeddedServletContainerFactory() { UndertowServletWebServerFactory factory = new UndertowServletWebServerFactory(); factory.addBuilderCustomizers(builder -> builder.setServerOption(UndertowOptions.ENABLE_HTTP2, true)); factory.addBuilderCustomizers(new UndertowBuilderCustomizer() { @Override public void customize(Undertow.Builder builder) { builder.addHttpListener(getHttpPort(), "0.0.0.0"); } }); factory.addDeploymentInfoCustomizers(deploymentInfo -> { deploymentInfo.addSecurityConstraint(new SecurityConstraint() .addWebResourceCollection(new WebResourceCollection() .addUrlPattern("/*")).setTransportGuaranteeType(TransportGuaranteeType.CONFIDENTIAL) .setEmptyRoleSemantic(SecurityInfo.EmptyRoleSemantic.PERMIT)) .setConfidentialPortManager(exchange -> getHttpsPort()); }); log.info("deploy https {}!", getHttpsPort()); return factory;
}
private int getHttpPort() { return environment.getProperty("server.http.port", int.class, 8080); }
private int getHttpsPort() { return environment.getProperty("server.port", int.class, 8443); }
}
|
启动spring boot
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package com.ming;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication() public class Start { public static void main(String[] args) { SpringApplication.run(Start.class, args); } }
|
启动成功的日志 开启了 443 和80
1 2
| 。。。 :::2022-02-14 10:41:59.693 [main] INFO o.s.boot.web.embedded.undertow.UndertowWebServer- Undertow started on port(s) 443 (https) 80 (http)
|
总结
undertow jboss出品
没啥毛病