Spring Boot - 限制创建的连接数

时间:2016-08-17 16:42:15

标签: java spring spring-boot pivotal-cloud-foundry

我使用Spring Boot开发了一个微服务。我通过存根后端调用来测试服务的性能。当我查看线程计数时,我发现在任何时间点创建到服务的最大线程数是20,即使调用的数量要高得多。对于可以对使用Spring Boot开发的微服务进行的调用次数是否有任何限制。请您指导我需要遵循哪些步骤来排除/增加服务接受的连接数量?

5 个答案:

答案 0 :(得分:31)

此设置源自嵌入式容器(tomcat,jetty ...)。

Tomcat的线程数

您可以在application.properties

中指定此属性
server.tomcat.max-threads=400

你说你计算了20个线程,但是根据这个other stackoverflow question/answer,tomcat的默认线程数应该是200,因为server.tomcat.max-threads的默认值是0.见tomcat's documentation

  

此连接器要创建的请求处理线程的最大数量,因此确定可以处理的最大并发请求数。如果未指定,则此属性设置为200.如果执行程序与此连接器关联,则忽略此属性,因为连接器将使用执行程序而不是内部线程池执行任务。

此外,属性:

  • 承诺server.undertow.worker-threads

  • 码头server.jetty.acceptors

您可以在Spring's documentation

中找到属性列表

答案 1 :(得分:7)

虽然接受的答案非常有用,但我最近经历了我认为与原始海报相同的问题。这是我能找到的与我的经历直接相关的唯一搜索结果,所以我想我会添加我的解决方案,以防有人帮助。

在我的情况下,观察到的并发限制为20是由maxConcurrentStreamExecutionorg.apache.coyote.http2.Http2Protocol属性的默认设置20强加的。

如果您遇到此问题并且正在使用HTTP / 2,那么增加maxConcurrentStreamExecution很有可能会有所帮助。

您可以在Tomcat Configuration Reference中找到更多信息,这些信息实际上表明默认情况下应该设置为200(而不是20)。你肯定可以在org.apache.coyote.http2.Http2Protocol中看到默认设置20,所以我不确定这是一个拼写错误,还是在嵌入式Tomcat版本中出现不同的东西。

答案 2 :(得分:1)

在Spring Boot 2中为HTTP / 2增加maxConcurrentStreamExecution(设置200)

@Bean
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> containerCustomizer() {
    return new WebServerFactoryCustomizer<TomcatServletWebServerFactory>() {
        @Override
        public void customize(TomcatServletWebServerFactory factory) {
            factory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
                @Override
                public void customize(Connector connector) {
                    Arrays.stream(connector.getProtocolHandler().findUpgradeProtocols())
                        .filter(upgradeProtocol -> upgradeProtocol instanceof Http2Protocol)
                        .map(upgradeProtocol -> (Http2Protocol) upgradeProtocol)
                        .forEach(http2Protocol -> http2Protocol.setMaxConcurrentStreamExecution(200));
                }
            });
        }
    };
}

答案 3 :(得分:1)

也许,您可以看看springboot's config

server.tomcat.accept-count=100 # Maximum queue length for incoming connection requests when all possible request processing threads are in use.
server.tomcat.additional-tld-skip-patterns= # Comma-separated list of additional patterns that match jars to ignore for TLD scanning.
server.tomcat.background-processor-delay=10s # Delay between the invocation of backgroundProcess methods. If a duration suffix is not specified, seconds will be used.
server.tomcat.basedir= # Tomcat base directory. If not specified, a temporary directory is used.
server.tomcat.max-connections=10000 # Maximum number of connections that the server accepts and processes at any given time.
server.tomcat.max-http-header-size=0 # Maximum size in bytes of the HTTP message header.
server.tomcat.max-http-post-size=2097152 # Maximum size in bytes of the HTTP post content.
server.tomcat.max-threads=200 # Maximum amount of worker threads.
server.tomcat.min-spare-threads=10 # Minimum amount of worker threads.
server.tomcat.port-header=X-Forwarded-Port # Name of the HTTP header used to override the original port value.
server.tomcat.protocol-header= # Header that holds the incoming protocol, usually named "X-Forwarded-Proto".
server.tomcat.protocol-header-https-value=https # Value of the protocol header indicating whether the incoming request uses SSL.
server.tomcat.redirect-context-root=true # Whether requests to the context root should be redirected by appending a / to the path.
server.tomcat.remote-ip-header= # Name of the HTTP header from which the remote IP is extracted. For instance, `X-FORWARDED-FOR`.
server.tomcat.resource.cache-ttl= # Time-to-live of the static resource cache.
server.tomcat.uri-encoding=UTF-8 # Character encoding to use to decode the URI.
server.tomcat.use-relative-redirects= # Whether HTTP 1.1 and later location headers generated by a call to sendRedirect will use relative or absolute redirects.

答案 4 :(得分:1)

如果您有执行器,则可以查看指标

/actuator/metrics/tomcat.threads.config.max

{
  "name": "tomcat.threads.config.max",
  "description": null,
  "baseUnit": null,
  "measurements": [{
    "statistic": "VALUE",
    "value": 200.0
  }],
  "availableTags": [{
    "tag": "name",
    "values": ["http-nio-8080"]
  }]
}

tomcat决定创建的实际值? /actuator/metrics/tomcat.threads.current

根据负载情况,您可能会看到10个