Spring Boot - 限制创建的连接数

Pun*_*cky 31 java spring spring-boot pivotal-cloud-foundry

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

ale*_*xbt 40

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

Tomcat的线程数

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

server.tomcat.max-threads=400
Run Code Online (Sandbox Code Playgroud)

你说你计算了20个线程,但根据这个其他stackoverflow问题/答案,默认的线程数应该是200与tomcat,因为server.tomcat.max-threads的默认值是0.请参阅tomcat的文档:

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

此外,该财产:

  • 承诺:server.undertow.worker-threads

  • 码头:server.jetty.acceptors

您可以在Spring的文档中找到属性列表

  • 从 Spring Boot 2.3 开始,正确的属性是“server.tomcat.threads.max”。 (14认同)
  • 您可以注入“@Autowired private ServerProperties serverProperties;”,然后查看“serverProperties.tomcat.maxThreads”。但如果不设置,默认值为 0 (`private int maxThreads = 0;`) (2认同)
  • 您可以将 spring boot 执行器依赖项添加到您的项目中。它是一个很棒的工具,可为您提供大量环境信息。端点 /env 将为您提供环境属性。 (2认同)

Der*_*rek 9

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

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

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

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

  • 需要注意的是,“maxConcurrentStreamExecution”指的是通过 HTTP 2 协议的单个连接的并发流,而不是容器可以接受的最大连接数。 (2认同)

Bru*_*uce 6

也许,您可以看一下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.

Run Code Online (Sandbox Code Playgroud)

  • `accept-count` 是队列中等待处理的连接请求,这是队列大小。`max-connections` 表示可以同时处理的最大连接数,这是最大连接大小。通常,“max-connections”应该大于“accept-count”。 (5认同)
  • “接受计数”和“最大连接数”有什么区别?“accept-count”不应该大于“max-connections”吗? (2认同)

Kal*_*oni 5

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

/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"]
  }]
}
Run Code Online (Sandbox Code Playgroud)

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

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

Spring Boot 似乎从来没有完全使用最大线程,但是你可以从更多开始

server:
   tomcat:
     min-spare-threads: 40
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

49915 次

最近记录:

7 年,2 月 前