springboot 源码分析 内嵌式 tomcat 启动

springboot版本2.1.6

研究一下springboot 是如何启动内嵌的tomcat 的

首先找到springboot 自动配置时引入的类

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration,\

这里只截取了tomcat 相关的

首先看EmbeddedWebServerFactoryCustomizerAutoConfiguration.class

这个类做了几件事情,

1 引入ServerProperties 相当于加载配置文件中server相关的配置

2 注册TomcatWebServerFactoryCustomizer 类,这个customizer将来会将ServerProperties中的配置加载进 Tomcat

EmbeddedWebServerFactoryCustomizerAutoConfiguration.class
@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(ServerProperties.class)
public class EmbeddedWebServerFactoryCustomizerAutoConfiguration {

   /**
    * Nested configuration if Tomcat is being used.
    */
   @Configuration
   @ConditionalOnClass({ Tomcat.class, UpgradeProtocol.class })
   public static class TomcatWebServerFactoryCustomizerConfiguration {

      @Bean
      public TomcatWebServerFactoryCustomizer tomcatWebServerFactoryCustomizer(Environment environment,
            ServerProperties serverProperties) {
         return new TomcatWebServerFactoryCustomizer(environment, serverProperties);
      }

   }
}   

看一下ServerProperties ,配置文件中server开头的配置都会加载到这里

@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
public class ServerProperties {
  

在看一下TomcatWebServerFactoryCustomizer,这个customize 会将ServerProperties 中的部分配置添加进创建 tomcat 的 factory,工厂创建服务器的时候会使用这些 properties , 注意通过断点发现, 系统中相关的 Customizer 共有5个,

public class TomcatWebServerFactoryCustomizer
      implements WebServerFactoryCustomizer<ConfigurableTomcatWebServerFactory>, Ordered { 
      
    private final Environment environment;

    private final ServerProperties serverProperties;
    
    public TomcatWebServerFactoryCustomizer(Environment environment, ServerProperties serverProperties) {
       this.environment = environment;
       this.serverProperties = serverProperties;
    }
          @Override
    public void customize(ConfigurableTomcatWebServerFactory factory) {
       ServerProperties properties = this.serverProperties;
       ServerProperties.Tomcat tomcatProperties = properties.getTomcat();
       PropertyMapper propertyMapper = PropertyMapper.get();
       propertyMapper.from(tomcatProperties::getBasedir).whenNonNull().to(factory::setBaseDirectory);
       propertyMapper.from(tomcatProperties::getBackgroundProcessorDelay).whenNonNull().as(Duration::getSeconds)
             .as(Long::intValue).to(factory::setBackgroundProcessorDelay);
       customizeRemoteIpValve(factory);
       propertyMapper.from(tomcatProperties::getMaxThreads).when(this::isPositive)
             .to((maxThreads) -> customizeMaxThreads(factory, tomcatProperties.getMaxThreads()));
       propertyMapper.from(tomcatProperties::getMinSpareThreads).when(this::isPositive)
             .to((minSpareThreads) -> customizeMinThreads(factory, minSpareThreads));
       propertyMapper.from(this::determineMaxHttpHeaderSize).whenNonNull().asInt(DataSize::toBytes)
             .when(this::isPositive)
             .to((maxHttpHeaderSize) -> customizeMaxHttpHeaderSize(factory, maxHttpHeaderSize));
       propertyMapper.from(tomcatProperties::getMaxSwallowSize).whenNonNull().asInt(DataSize::toBytes)
             .to((maxSwallowSize) -> customizeMaxSwallowSize(factory, maxSwallowSize));
       propertyMapper.from(tomcatProperties::getMaxHttpPostSize).asInt(DataSize::toBytes)
             .when((maxHttpPostSize) -> maxHttpPostSize != 0)
             .to((maxHttpPostSize) -> customizeMaxHttpPostSize(factory, maxHttpPostSize));
       propertyMapper.from(tomcatProperties::getAccesslog).when(ServerProperties.Tomcat.Accesslog::isEnabled)
             .to((enabled) -> customizeAccessLog(factory));
       propertyMapper.from(tomcatProperties::getUriEncoding).whenNonNull().to(factory::setUriEncoding);
       propertyMapper.from(properties::getConnectionTimeout).whenNonNull()
             .to((connectionTimeout) -> customizeConnectionTimeout(factory, connectionTimeout));
       propertyMapper.from(tomcatProperties::getMaxConnections).when(this::isPositive)
             .to((maxConnections) -> customizeMaxConnections(factory, maxConnections));
       propertyMapper.from(tomcatProperties::getAcceptCount).when(this::isPositive)
             .to((acceptCount) -> customizeAcceptCount(factory, acceptCount));
       customizeStaticResources(factory);
       customizeErrorReportValve(properties.getError(), 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值