java Sping Boot -限制创建的连接数

cedebl8k  于 2023-05-21  发布在  Java
关注(0)|答案(5)|浏览(149)

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

ma8fv8wu

ma8fv8wu1#

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

Tomcat的线程数

您可以在www.example.com中指定此属性application.properties

server.tomcat.threads.max=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中找到属性列表

goqiplq2

goqiplq22#

也许,你可以看看springboot的配置

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.
izkcnapc

izkcnapc3#

虽然接受的答案是非常有用的,我最近经历了我认为是相同的问题,作为原来的海报。这是我能找到的唯一一个与我的经验直接相关的搜索结果,所以我想我应该添加我的解决方案,以防它能帮助到别人。
在我的例子中,观察到的并发限制20是由org.apache.coyote.http2.Http2ProtocolmaxConcurrentStreamExecution属性的默认设置20强加的。
如果您遇到此问题并且正在使用HTTP/2,那么增加maxConcurrentStreamExecution很有可能会有所帮助。
您可以在Tomcat Configuration Reference中找到更多信息,它实际上指出默认情况下应将其设置为200(而不是20)。但是,您可以在org.apache.coyote.http2.Http2Protocol中看到默认设置20,所以我不确定这是一个错字,还是只是在Tomcat的嵌入式版本中以不同的方式呈现。

8aqjt8rx

8aqjt8rx4#

如果你有执行器,你可以看到指标
/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 Boot 似乎从来没有完全使用过max-threads,但是你可以从更多开始

server:
   tomcat:
     min-spare-threads: 40
esyap4oy

esyap4oy5#

在Sping 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));
                }
            });
        }
    };
}

相关问题