springboot项目中使用tomcatweb服务器

编程

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

以上是 springboot项目中使用tomcatweb服务器 的全部内容, 来源链接: utcz.com/z/514208.html

回到顶部