• 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

  • 相关阅读:
    以太坊公开拍卖智能合约案例
    部署智能合约
    ERC20 Token
    查看crontab运行状态
    php把excel数值格式转成日期格式
    PHP识别中文编码并自动转换为UTF-8
    python中文件内容替换
    mysqldump备份指定的数据
    linux设置服务器时间
    git常用命令
  • 原文地址:https://www.cnblogs.com/bjlhx/p/8672848.html
Copyright © 2020-2023  润新知