Spring 启动 - 限制创建的连接数

Spring Boot - Limit on number of connections created

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

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

Tomcat的线程数

您可以在 application.properties

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

你说你数了20个线程,但是根据这个other Whosebug question/answer, the default number of thread should be 200 with tomcat, since server.tomcat.max-threads's default value is 0. See tomcat's documentation:

The maximum number of request processing threads to be created by this Connector, which therefore determines the maximum number of simultaneous requests that can be handled. If not specified, this attribute is set to 200. If an executor is associated with this connector, this attribute is ignored as the connector will execute tasks using the executor rather than an internal thread pool.

此外,属性 用于:

  • 暗流: server.undertow.worker-threads

  • 码头: server.jetty.acceptors

您将在 Spring's documentation

中找到属性列表

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

在我的例子中,org.apache.coyote.http2.Http2Protocol.

maxConcurrentStreamExecution 属性 的默认设置 20 强加了观察到的并发限制 20

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

您可以在 Tomcat Configuration Reference 中找到更多信息,其中实际上指出默认情况下应将其设置为 200(而不是 20)。不过,您肯定可以在 org.apache.coyote.http2.Http2Protocol 中看到默认设置 20,所以我不确定这是拼写错误还是只是在 Tomcat.[=15 的嵌入式版本中呈现不同的东西=]

在 Spring 引导 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));
                }
            });
        }
    };
}

也许,你可以看看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.

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

/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 个,具体取决于负载

spring 引导似乎从未完全使用最大线程 但是你可以从 more

开始
server:
   tomcat:
     min-spare-threads: 40