015-Spring Boot 定制和优化内嵌的Tomcat

Posted 木子旭

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了015-Spring Boot 定制和优化内嵌的Tomcat相关的知识,希望对你有一定的参考价值。

一、内嵌web容器

  参看http://www.cnblogs.com/bjlhx/p/8372584.html

  查看源码可知提供以下三种:

    

二、定制优化tomcat

2.1、配置文件配置

  通过application.properties配置tomcat

  端口配置:server.port=10001
  地址配置:server.adress=192.168.1.1

     通过ServerProperties查看到tomcat变量,进入即可配置,如下

  启用日志配置:

server.tomcat.accesslog.enabled=true
server.tomcat.accesslog.directory=d:/logs

  原理分析  

  在spring-boot-autoconfigure-1.5.9.RELEASE.jar包中的org.springframework.boot.autoconfigure.web.ServerProperties查看

  可以查看到以上所示属性

2.2、通过代码方式配置一-实现EmbeddedServletContainerCustomizer接口

  EmbeddedServletContainerCustomizer→三个具体默认实现

    

  1》代码方式自定义

  定义一个类MyEmbeddedServletContainerCustomizer实现EmbeddedServletContainerCustomizer接口,即可

  设置基本配置

@Component
public class MyEmbeddedServletContainerCustomizer implements EmbeddedServletContainerCustomizer {

    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) {
        System.out.println(container.getClass());
        TomcatEmbeddedServletContainerFactory factory =(TomcatEmbeddedServletContainerFactory)container;
        factory.setPort(8181);
        factory.setBaseDirectory(new File("d:/tmp/tomcat"));
    }
}
View Code

  设置日志

package com.lhx.spring.springboot_web;

import java.io.File;

import org.apache.catalina.valves.AccessLogValve;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.stereotype.Component;

@Component
public class MyEmbeddedServletContainerCustomizer implements EmbeddedServletContainerCustomizer {

    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) {
        System.out.println(container.getClass());
        TomcatEmbeddedServletContainerFactory factory =(TomcatEmbeddedServletContainerFactory)container;
        factory.setPort(8181);
        factory.setBaseDirectory(new File("d:/tmp/tomcat"));
        
        factory.addContextValves(getAccessLogValve());
    }
    
    private AccessLogValve getAccessLogValve() {
        AccessLogValve log = new AccessLogValve();
        log.setDirectory("d:/tmp/logs");
        log.setEnabled(true);
        log.setPattern("common");
        log.setPrefix("spring-boot-lhx-access-log");
        log.setSuffix(".txt");
        return log;
        
    }
}
View Code

  设置连接数配置、最大线程

package com.lhx.spring.springboot_web;

import java.io.File;

import org.apache.catalina.connector.Connector;
import org.apache.catalina.valves.AccessLogValve;
import org.apache.coyote.http11.Http11NioProtocol;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.stereotype.Component;

@Component
public class MyEmbeddedServletContainerCustomizer implements EmbeddedServletContainerCustomizer {

    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) {
        System.out.println(container.getClass());
        TomcatEmbeddedServletContainerFactory factory =(TomcatEmbeddedServletContainerFactory)container;
        factory.setPort(8181);
        factory.setBaseDirectory(new File("d:/tmp/tomcat"));
        
        factory.addContextValves(getAccessLogValve());
        
        factory.addConnectorCustomizers(new MyTomcatConnectorCustomizers());
    }
    
    private AccessLogValve getAccessLogValve() {
        AccessLogValve log = new AccessLogValve();
        log.setDirectory("d:/tmp/logs");
        log.setEnabled(true);
        log.setPattern("common");
        log.setPrefix("spring-boot-lhx-access-log");
        log.setSuffix(".txt");
        return log;        
    }
    
    
}
class MyTomcatConnectorCustomizers implements TomcatConnectorCustomizer{

    @Override
    public void customize(Connector connector) {
         Http11NioProtocol protocol =  (Http11NioProtocol)connector.getProtocolHandler();
         protocol.setMaxConnections(2000);
         protocol.setMaxThreads(500);
    }    
}
View Code

2.5、通过代码方式配置二-默认配置类方式

package com.lhx.spring.springboot_web;

import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;

@SpringBootConfiguration
public class WebServletConfiguration {
    @Bean
    public EmbeddedServletContainerFactory createEmbeddedServletContainerFactory() {
        TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
        factory.setPort(1008);
        factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404.html"));
        return factory;
    }
}
View Code

  添加初始化

        factory.addInitializers((servletContext)->{
            System.out.println("----------servletContext startup----------");
        });

  此时可以获取到servletContext信息,可以进行添加Filter、设置Attribute等

   源码地址:https://github.com/bjlhx15/spring-boot.git

以上是关于015-Spring Boot 定制和优化内嵌的Tomcat的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot 揭秘与实战 服务器篇 - 其他内嵌服务器 发表于 2017-01-03 | Spring框架 | Spri

记录spring boot 图片上传与显示

Spring Boot启动过程:Spring Boot内嵌Tomcat启动

Spring Boot项目打包方式为war

Spring Boot 2.0 新特性和发展方向

web性能优化之---JavaScript中的无阻塞加载性能优化方案