美文网首页spring boot
Spring Boot2.x 内置Tomcat调优,使用Apr模

Spring Boot2.x 内置Tomcat调优,使用Apr模

作者: 不敢预言的预言家 | 来源:发表于2018-07-26 23:27 被阅读553次
  • 启用Apr模式,需要调用方法 org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory#setProtocol
  • windows下安装配置native和apr只需要在环境变量path里能够找到 tcnative-1.dll即可,这个文件在 高版本tomcat\bin\ 目录下会存在
  • linux下安装配置native和apr,参考 https://tomcat.apache.org/native-doc/

TomcatConfig.java

package site.yuyanjia.template.common.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.lang.reflect.Field;

/**
 * TomcatConfig
 * <p>
 * Tomcat优化:
 * 1、增加JVM内存:java -jar -Xms256m -Xmx256m abc.jar
 * 2、优化Tomcat配置参数
 * 3、使用APR
 *
 * @author seer
 * @date 2018/7/17 11:48
 */
@Configuration
@Slf4j
@EnableConfigurationProperties(ConfigProperties.class)
public class TomcatConfig {

    @Bean
    public ServletWebServerFactory servletWebServerFactory(ConfigProperties configProperties) {
        ConfigProperties.Tomcat tomcat = configProperties.getTomcat();

        TomcatServletWebServerFactory tomcatServletWebServerFactory = new TomcatServletWebServerFactory();
        tomcatServletWebServerFactory.setProtocol(tomcat.getProtocol());

        tomcatServletWebServerFactory.addConnectorCustomizers((TomcatConnectorCustomizer) connector -> {
            Field[] fields = tomcat.getClass().getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                try {
                    connector.setAttribute(field.getName(), field.get(tomcat));
                } catch (IllegalAccessException e) {
                    log.error("Tomcat connector 配置异常", e);
                    continue;
                }
            }
        });

        return tomcatServletWebServerFactory;
    }
}

ConfigProperties.java


package site.yuyanjia.template.common.config;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * 配置资源
 *
 * @author seer
 * @date 2018/7/24 14:38
 */
@ConfigurationProperties(value = ConfigProperties.PREFIX)
public class ConfigProperties {
    public static final String PREFIX = "yuyanjia";

    private Tomcat tomcat;

    public Tomcat getTomcat() {
        return tomcat;
    }

    public void setTomcat(Tomcat tomcat) {
        this.tomcat = tomcat;
    }

    /**
     * Tomcat配置文件
     *
     * @author seer
     * @date 2018/7/24 14:45
     */
    public static class Tomcat {
        /**
         * 连接超时,单位ms
         */
        private Integer connectionTimeout = 20000;

        /**
         * 接收连接线程数量,参考cpu核数
         */
        private Integer acceptorThreadCount = 2;

        /**
         * 最小监听线程
         */
        private Integer minSpareThreads = 5;

        /**
         * 最大监听线程
         * 同时相应客户请求最大值
         */
        private Integer maxSpareThreads = 200;

        /**
         * 最大排队数
         */
        private Integer acceptCount = 200;

        /**
         * 最大连接数
         */
        private Integer maxConnections = 800;

        /**
         * 最大线程数
         */
        private Integer maxThreads = 500;

        /**
         * 运行模式
         */
        private String protocol = "org.apache.coyote.http11.Http11NioProtocol";

        public Integer getConnectionTimeout() {
            return connectionTimeout;
        }

        public void setConnectionTimeout(Integer connectionTimeout) {
            this.connectionTimeout = connectionTimeout;
        }

        public Integer getAcceptorThreadCount() {
            return acceptorThreadCount;
        }

        public void setAcceptorThreadCount(Integer acceptorThreadCount) {
            this.acceptorThreadCount = acceptorThreadCount;
        }

        public Integer getMinSpareThreads() {
            return minSpareThreads;
        }

        public void setMinSpareThreads(Integer minSpareThreads) {
            this.minSpareThreads = minSpareThreads;
        }

        public Integer getMaxSpareThreads() {
            return maxSpareThreads;
        }

        public void setMaxSpareThreads(Integer maxSpareThreads) {
            this.maxSpareThreads = maxSpareThreads;
        }

        public Integer getAcceptCount() {
            return acceptCount;
        }

        public void setAcceptCount(Integer acceptCount) {
            this.acceptCount = acceptCount;
        }

        public Integer getMaxConnections() {
            return maxConnections;
        }

        public void setMaxConnections(Integer maxConnections) {
            this.maxConnections = maxConnections;
        }

        public Integer getMaxThreads() {
            return maxThreads;
        }

        public void setMaxThreads(Integer maxThreads) {
            this.maxThreads = maxThreads;
        }

        public String getProtocol() {
            return protocol;
        }

        public void setProtocol(String protocol) {
            this.protocol = protocol;
        }
    }

}

application.yml

yuyanjia:
  tomcat:
    connection-timeout: 10000
    acceptor-thread-count: 4
    min-spare-threads: 100
    max-spare-threads: 300
    accept-count: 200
    max-connections: 800
    max-threads: 500
    protocol: org.apache.coyote.http11.Http11AprProtocol

image.png

相关文章

网友评论

    本文标题:Spring Boot2.x 内置Tomcat调优,使用Apr模

    本文链接:https://www.haomeiwen.com/subject/phiimftx.html