zoukankan      html  css  js  c++  java
  • 嵌入式Tomcat容器的参数(maxParameterCount)设定

    背景

    昨天同事遇到了error一起看了一下感觉比较重要在这记录一下

    基本情况是页面上选中9K+的数据向后台发送请求,然后系统就崩了。。。

    error信息如下

    More than the maximum number of request parameters (GET plus POST) for a single request ([10,000]) were detected.

    Any parameters beyond this limit have been ignored. To change this limit, set the maxParameterCount attribute on the Connector.

    简说 单次请求的参数超出限制,通过maxParameterCount来更改容器的限制。

    经验里对于tomcat容器的设定最对就是端口号,超时,最大线程的设置比较多

    这个【maxParameterCount 】的设定还没有过然后到网上去翻了

    在官网的文档(tomcat doc)里找到了如下

    maxParameterCount The maximum number of parameter and value pairs (GET plus POST) which will be automatically parsed by the container. Parameter and value pairs beyond this limit will be ignored. A value of less than 0 means no limit. If not specified, a default of 10000 is used. Note that FailedRequestFilter filter can be used to reject requests that hit the limit.
    maxPostSize The maximum size in bytes of the POST which will be handled by the container FORM URL parameter parsing. The limit can be disabled by setting this attribute to a value less than zero. If not specified, this attribute is set to 2097152 (2 megabytes). Note that the FailedRequestFilter can be used to reject requests that exceed this limit.

    简说

    maxParameterCount 是tomcat容器来限定你 单次请求的参数的最大数量,默认是10000。所以通过这个属性你可以根据情况给设定适当的值。当然也有超出1w的情况怎么办?

    上面文档里也有给出答案 小于0的设定可以禁用此制限。这也是很多网上资料设置-1的原因。

    maxPostSize 是http-post单次请求内容或者说数据的最大限制,默认值为2M。同样小于0的设定可以禁用此制限

    具体使用

    tomcat/conf/server.xml文件中找到如下节点

    <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />

    (这里有好几个connector看自己用的是哪个端口的)

    修改后

    <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" maxPostSize="-1" maxParameterCount="-1"/>

    这是通常的做法,但有时如果tomcat容器内置的话你可能都找不到server.xml文件,

    比如spring boo项目内置tomcat

    这时候通常会想到 可以配置在application.properties里

    然后翻一下看看application.properties里怎么配置

    spring-boot的doc看到只看到如下

    server.tomcat.max-http-post-size=0 # Maximum size in bytes of the HTTP post content.

     

    并没有找到关于maxParameterCount信息,猜测不支持在配置文件里配置

    继续翻

    找到可以写在java类里的方法

        @Bean
        public EmbeddedServletContainerFactory mbeddedServletContainerFactory() {
            TomcatEmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory = new TomcatEmbeddedServletContainerFactory();
            
            tomcatEmbeddedServletContainerFactory.addConnectorCustomizers(connector ->{
                connector.setMaxPostSize(2);
                System.out.println("connector.getMaxPostSize: "+connector.getMaxPostSize());
                System.out.println("connector.getPort: "+connector.getPort());
            });
            
            return tomcatEmbeddedServletContainerFactory;
        }

     代码本身并不多,过程比较曲折,大概说一下

    EmbeddedServletContainerCustomizer

    这是一个自定义内置容器的接口,通过实现它可以创建内置容器

    然后还找到已经实现了它类,

    AbstractEmbeddedServletContainerFactory,
    JettyEmbeddedServletContainerFactory,
    TomcatEmbeddedServletContainerFactory,
    UndertowEmbeddedServletContainerFactory

    这里看到了定制tomcat容器的工厂类

    继续看看这个类里都什么可用(源码有点多就不贴了贴一张截图)

    这里就是我们这次用到的函数了为什么标记两个因为上边那个也可用,下面是官网给的说明

    当然其它的属性也可以在这里设定贴一下官网连接就不再代码体现了。

     

  • 相关阅读:
    总结(1)--- 数据库
    在路上---学习篇(一)Python 数据结构和算法 (4) --希尔排序、归并排序
    在路上---学习篇(一)Python 数据结构和算法 (3) --快速排序
    在路上---学习篇(一)Python 数据结构和算法 (2) -- 冒泡排序、选择排序、插入排序
    javascript观察者模式
    es6对象的扩展
    es6数组的扩展
    vue全局API
    javascript原型链
    简单工厂,工厂方法模式
  • 原文地址:https://www.cnblogs.com/zhufu9426/p/8275365.html
Copyright © 2011-2022 走看看