需求背景
针对框架内嵌的Tomcat,实现自定义配置用法:
1. 可以通过application.yml文件实现
2. 可以通过代码,实现自定义配置
技术点
两个技术点
1. .yml文件格式用法
配置Tomcat参数,框架自动识别和加载
2. TomcatServletWebServerFactory对象
自定义实现Tomcat配置
功能介绍
功能技术点:
1. Tomcat容器配置用法,使用.yml文件方式
2. 设置tomcat的最大连接数和最大并发数
3. Springboot1和Springboot2版本之间的差异用法
代码演示
1. Tomcat容器配置用法,使用.yml文件方式
# tomcat
server:
tomcat:
# 最大线程数
max-threads: 2
# 最大连接数
max-connections: 10
# 监听端口
port: 9090
2. Springboot2代码实现
package com.md.demo;
import org.apache.catalina.connector.Connector;
import org.apache.coyote.http11.Http11NioProtocol;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 自定义实现Tomcat配置
*
* @author Minbo
*
*/
@Configuration
public class MyTomcatCustomizer {
@Bean
public ConfigurableServletWebServerFactory configurableServletWebServerFactory() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
tomcat.addAdditionalTomcatConnectors(createConnector());
return tomcat;
}
private Connector createConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
connector.setPort(8090);
// 最大线程数
protocol.setMaxThreads(2);
// 最大连接数
protocol.setMaxConnections(10);
return connector;
}
}
注:为了验证两者的差别,我故意把端口分别用了8090和9090端口
验证参数配置是否生效:
我这里故意设置了maxThreads为2,可以调用接口,验证配置是否生效,如下图
3. 其他参数说明
可以进入源码查看
附加其他实现
4. Springboot1代码实现
package com.yfax.webapi;
import org.apache.catalina.connector.Connector;
import org.apache.coyote.http11.Http11NioProtocol;
import org.springframework.boot.context.embedded.EmbeddedServletContainer;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.stereotype.Component;
@Component
public class WebEmbedContainer extends TomcatEmbeddedServletContainerFactory {
public EmbeddedServletContainer getEmbeddedServletContainer(ServletContextInitializer... initializers) {
return super.getEmbeddedServletContainer(initializers);
}
protected void customizeConnector(Connector connector) {
super.customizeConnector(connector);
Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
// 设置最大线程数
protocol.setMaxThreads(350);
// 设置最大连接数
protocol.setMaxConnections(500);
protocol.setAcceptorThreadCount(500);
protocol.setMinSpareThreads(350);
// 设置超时时间
protocol.setConnectionTimeout(5000);
protocol.setKeepAliveTimeout(5000);
}
}
完整源码下载
https://github.com/hemin1003/spring-boot-study/tree/master/spring-boot2-study/spring-boot2-parent
官方资料
官方代码链接地址,代码如下
@Bean
public ServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
tomcat.addAdditionalTomcatConnectors(createSslConnector());
return tomcat;
}
private Connector createSslConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
try {
File keystore = new ClassPathResource("keystore").getFile();
File truststore = new ClassPathResource("keystore").getFile();
connector.setScheme("https");
connector.setSecure(true);
connector.setPort(8443);
protocol.setSSLEnabled(true);
protocol.setKeystoreFile(keystore.getAbsolutePath());
protocol.setKeystorePass("changeit");
protocol.setTruststoreFile(truststore.getAbsolutePath());
protocol.setTruststorePass("changeit");
protocol.setKeyAlias("apitester");
return connector;
}
catch (IOException ex) {
throw new IllegalStateException("can't access keystore: [" + "keystore"
+ "] or truststore: [" + "keystore" + "]", ex);
}
}
下一章教程
SpringBoot从入门到精通教程(六)- Mysql和Mybatis+XML用法详解
该系列教程
至此,全部介绍就结束了
------------------------------------------------------
------------------------------------------------------
关于我(个人域名)
期望和大家一起学习,共同进步,共勉,O(∩_∩)O谢谢
欢迎交流问题,可加个人QQ 469580884,
或者,加我的群号 751925591,一起探讨交流问题
不讲虚的,只做实干家
Talk is cheap,show me the code
如果觉得内容赞,您可以请我喝杯咖啡: