专注于 JetBrains IDEA 全家桶,永久激活,教程
持续更新 PyCharm,IDEA,WebStorm,PhpStorm,DataGrip,RubyMine,CLion,AppCode 永久激活教程

springboot项目中使用tomcat web服务器

1、tomcat服务器

Tomcat适合处理少数非常繁忙的链接,也就是说链接生命周期短的话,Tomcat的总体性能更高。 另外,Tomcat默认采用BIO(阻塞IO)处理I/O请求,在处理静态资源时,性能较差。

2、只导包spring-boot-starter-web即可,里面包含了spring-boot-starter-tomcat包。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

3、在spring-boot-autoconfigure 这个jar中的org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration类中, 通过Import注解加入了 Tomcat的三种支持

@Configuration
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@ConditionalOnClass(ServletRequest.class)
@ConditionalOnWebApplication(type = Type.SERVLET)
@EnableConfigurationProperties(ServerProperties.class)
@Import({ ServletWebServerFactoryAutoConfiguration.BeanPostProcessorsRegistrar.class,
        ServletWebServerFactoryConfiguration.EmbeddedTomcat.class,
        ServletWebServerFactoryConfiguration.EmbeddedJetty.class,
        ServletWebServerFactoryConfiguration.EmbeddedUndertow.class })
public class ServletWebServerFactoryAutoConfiguration {

    @Bean
    public ServletWebServerFactoryCustomizer servletWebServerFactoryCustomizer(
            ServerProperties serverProperties) {
        return new ServletWebServerFactoryCustomizer(serverProperties);
    }

    @Bean
    @ConditionalOnClass(name = "org.apache.catalina.startup.Tomcat")
    public TomcatServletWebServerFactoryCustomizer tomcatServletWebServerFactoryCustomizer(
            ServerProperties serverProperties) {
        return new TomcatServletWebServerFactoryCustomizer(serverProperties);
    }

    ...
}

4、常用配置

server.port=8080
server.servlet.context-path=/

server.tomcat.uri-encoding=UTF-8
# 最大工作线程数,默认200。(4核8g内存,线程数经验值800,操作系统做线程之间的切换调度是有系统开销的,所以不是越多越好。)
server.tomcat.max-threads=200
# 等待队列长度,默认100。
server.tomcat.accept-count=100
# 默认值是10000
server.tomcat.max-connections=10000
server.tomcat.max-http-form-post-size=10
# 最小工作空闲线程数,默认10。(适当增大一些,以便应对突然增长的访问量)
server.tomcat.min-spare-threads=100
server.tomcat.connection-timeout=3600

server.tomcat.accesslog.enabled=true
server.tomcat.accesslog.directory=D:/logs
#%t [%I] %a %r %s (%b Byte) (%T ms)
server.tomcat.accesslog.pattern=%t %a "%r" %s (%D ms)

5、配置https访问

server.ssl.port=8443
server.ssl.enabled=true
server.ssl.key-store=classpath:ssl.keystore
server.ssl.key-store-password=0123789
server.ssl.key-store-type=PKCS12
server.ssl.key-alias=ssl_alias
@Value("${server.port:8080}")
private int port;
@Value("${server.ssl.port:443}")
private int sslPort;
@Value("${server.servlet.context-path:/*}")
private String path;

@Bean
public ServletWebServerFactory servletContainer() {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory(){
        protected void postProcessContext(Context context) {
            SecurityConstraint securityConstraint = new SecurityConstraint();
            securityConstraint.setUserConstraint("CONFIDENTIAL");
            SecurityCollection collection = new SecurityCollection();
            collection.addPattern(path);
            securityConstraint.addCollection(collection);
            context.addConstraint(securityConstraint);
        }
    };
    tomcat.addAdditionalTomcatConnectors(httpConnector());
    return tomcat;
}

/**
 * 使用代码配置http自动跳转https
 * @return
 */
@Bean
public Connector httpConnector(){
    Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
    connector.setScheme("http");
    connector.setPort(port);
    connector.setSecure(false);
    connector.setRedirectPort(sslPort);
    return connector;
}

源码:https://gitee.com/lion123/springboot-tomcat-demo

文章永久链接:https://tech.souyunku.com/25000

未经允许不得转载:搜云库技术团队 » springboot项目中使用tomcat web服务器

JetBrains 全家桶,激活、破解、教程

提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,支持 IntelliJ IDEA、PyCharm、WebStorm 等工具的永久激活。无论是破解教程,还是最新激活码,均可免费获得,帮助开发者解决常见激活问题,确保轻松破解并快速使用 JetBrains 软件。获取免费的破解补丁和激活码,快速解决激活难题,全面覆盖 2024/2025 版本!

联系我们联系我们