Java Spring Boot - 限制创建的连接数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39002090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot - Limit on number of connections created
提问by Punter Vicky
I developed a microservice using Spring Boot. I was performance testing the service by stubbing the backend calls. When I looked at the thread count , I see that the maximum number of threads that created to the service is 20 at any point in time even though the number of calls made is much higher. Are there any limitations with respect to number of calls that can be made to a microservice developed using Spring Boot. Please can you guide in what steps I need to follow to troubleshoot / increase the number of connections accepted by the service?
我使用 Spring Boot 开发了一个微服务。我通过存根后端调用来测试服务的性能。当我查看 thread count 时,我看到在任何时间点为服务创建的最大线程数为 20,即使调用的数量要高得多。对使用 Spring Boot 开发的微服务的调用次数是否有任何限制。请您指导我需要遵循哪些步骤来排除故障/增加服务接受的连接数?
采纳答案by alexbt
This setting is derived from the embedded container (tomcat, jetty...).
此设置源自嵌入式容器(tomcat、jetty...)。
Tomcat's number of threads
Tomcat的线程数
You may specify this property in your application.properties
您可以在 application.properties 中指定此属性
server.tomcat.max-threads=400
You say you counted 20 threads, however according to this other stackoverflow 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:
你说你数了 20 个线程,但是根据这个其他 stackoverflow 问题/答案,tomcat 的默认线程数应该是 200,因为 server.tomcat.max-threads 的默认值为 0。请参阅tomcat 的文档:
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.
此连接器要创建的最大请求处理线程数,因此决定了可以处理的最大并发请求数。如果未指定,则此属性设置为 200。如果执行程序与此连接器关联,则忽略此属性,因为连接器将使用执行程序而不是内部线程池执行任务。
Also, the property for:
此外,该属性为:
undertow:
server.undertow.worker-threads
jetty:
server.jetty.acceptors
底流:
server.undertow.worker-threads
码头:
server.jetty.acceptors
You'll find the list of properties in Spring's documentation
您将在Spring 的文档中找到属性列表
回答by Derek
While the accepted answer is very useful, I recently experienced what I believe to be the same problem as the original poster. This is the only search result I could find that directly correlated with my experience, so I thought I'd add my solution in case it helps someone.
虽然接受的答案非常有用,但我最近遇到了我认为与原始海报相同的问题。这是我能找到的唯一与我的经验直接相关的搜索结果,所以我想我会添加我的解决方案,以防它对某人有帮助。
In my case, the observed concurrency limit of 20 was imposed by the default setting of 20 for themaxConcurrentStreamExecution
property in org.apache.coyote.http2.Http2Protocol
.
在我的例子中,观察到的并发限制 20 是由 中的maxConcurrentStreamExecution
属性的默认设置 20 强加的org.apache.coyote.http2.Http2Protocol
。
If you're experiencing this problem and you're using HTTP/2, there's a good chance that increasing maxConcurrentStreamExecution
will help.
如果您遇到此问题并且您正在使用 HTTP/2,那么增加很有可能maxConcurrentStreamExecution
会有所帮助。
You can find more info in the Tomcat Configuration Reference, which actually states that this should be set to 200 by default (not 20). You can definitely see the default setting of 20 in org.apache.coyote.http2.Http2Protocol
, though, so I'm not sure if this is a typo or just something that presents itself differently in the embedded version of Tomcat.
您可以在Tomcat 配置参考中找到更多信息,其中实际上指出默认情况下应将其设置为 200(而不是 20)。不过,您绝对可以在 中看到默认设置 20 org.apache.coyote.http2.Http2Protocol
,所以我不确定这是一个错字,还是只是在 Tomcat 的嵌入式版本中表现出不同的东西。
回答by oshilan
Increase maxConcurrentStreamExecution
(set 200) for HTTP/2 in Spring Boot 2:
maxConcurrentStreamExecution
Spring Boot 2 中 HTTP/2 的增加(设置 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));
}
});
}
};
}
回答by Bruce
Maybe, you can have a look at the springboot's config
也许,你可以看看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.
回答by Kalpesh Soni
If you have actuator you can see the metrics
如果你有执行器,你可以看到指标
/actuator/metrics/tomcat.threads.config.max
/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"]
}]
}
The actual value tomcat decided to create? /actuator/metrics/tomcat.threads.current
tomcat决定创造的实际价值?/actuator/metrics/tomcat.threads.current
you may see 10 there depending on the load
根据负载,您可能会在那里看到 10 个
spring boot never seems to fully use max-threads you can however start with more
spring boot 似乎从来没有完全使用过最大线程,但是你可以从更多开始
server:
tomcat:
min-spare-threads: 40