Spring Boot中的`java.util.concurrent.ThreadPoolExecutor`大小

huangapple go评论110阅读模式
英文:

Spring Boot java.util.concurrent.ThreadPoolExecutor size

问题

  1. 目前我正在测试我的Spring Boot应用程序,它是一个带有断路器模式的REST服务。现在,我同时使用了20个线程调用我的服务,并获得了以下日志条目:

Task java.util.concurrent.FutureTask@127adac1[未完成,任务 = java.util.concurrent.Executors$RunnableAdapter@74bf28cd[封装的任务 = null]] 被从 java.util.concurrent.ThreadPoolExecutor@19ae13b2[运行中,池大小 = 10,活动线程 = 10,排队任务 = 0,已完成任务 = 16] 中拒绝

  1. 所以我的问题是线程池的最大大小真的是10吗?我能否将它设置为其他值?
  2. 我找到了属性 `server.tomcat.max-threads` 并将其设置为10,所有请求都将通过。
  3. **编辑**
  4. 我正在使用Spring Boot RestTemplate调用另一个REST服务,这可能导致问题吗?
  1. @HystrixCommand(
  2. commandKey = "callRestService", fallbackMethod = "failCallRestService", ignoreExceptions = DataNotFoundException.class)
  3. public ResponseEntity<DataAtomsResponse> callRestService(String searchItem, String trackingId)
  4. throws RestServiceNotAvailableException
  5. {
  6. ThreadContext.put("trackingID", trackingId);
  7. configureRestTemplate();
  8. LOGGER.info("收到带有trackingId的请求:{}", trackingId);
  9. Map<String, String> restUrlParams = new HashMap<>();
  10. restUrlParams.put(REST_URL_PARAMETER, searchItem);
  11. HttpEntity<String> entity = getRestParameters(trackingId);
  12. DataAtomsResponse dataAtomsResponse;
  13. LOGGER.info("请求RestService跟踪ID:{}", trackingId);
  14. ResponseEntity<String> dataResponse =
  15. restTemplate.exchange(config.getRestServiceUrl(), HttpMethod.GET, entity, String.class, restUrlParams);
  16. if (dataResponse.getStatusCode() == HttpStatus.OK || dataResponse.getStatusCode() == HttpStatus.NOT_FOUND) {
  17. LOGGER.debug("将RestService的结果转换为JSON跟踪ID:{}", trackingId);
  18. dataAtomsResponse = dataParser.parse(dataResponse.getBody(), searchItem, trackingId);
  19. return ResponseEntity.ok(dataAtomsResponse);
  20. }
  21. else {
  22. throw new RestServiceNotAvailableException(dataResponse.getStatusCode().getReasonPhrase());
  23. }
  24. }
  1. 我在任何其他类中都没有实现过 ThreadPoolExecutor
英文:

currently I am testing my Spring Boot app, which is a rest service with a circuit breaker pattern. Now
I called my service with 20 threads at the same time and get the following log entry:

  1. Task java.util.concurrent.FutureTask@127adac1[Not completed, task = java.util.concurrent.Executors$RunnableAdapter@74bf28cd[Wrapped task = null]] rejected from java.util.concurrent.ThreadPoolExecutor@19ae13b2[Running, pool size = 10, active threads = 10, queued tasks = 0, completed tasks = 16]

So my question would be is the maximum size of the thread pool realy 10 and am I able to set it to somthing differen?

I found the propertie server.tomcat.max-threads and set it to 10 all request will pass.

Edit

I am calling another rest service with the Spring Boot Resttemplate could this one cause the problem?

  1. @HystrixCommand(
  2. commandKey = &quot;callRestService&quot;, fallbackMethod = &quot;failCallRestService&quot;, ignoreExceptions = DataNotFoundException.class)
  3. public ResponseEntity&lt;DataAtomsResponse&gt; callRestService(String searchItem, String trackingId)
  4. throws RestServiceNotAvailableException
  5. {
  6. ThreadContext.put(&quot;trackingID&quot;, trackingId);
  7. configureRestTemplate();
  8. LOGGER.info(&quot;Received request with trackingId: {}&quot;, trackingId);
  9. Map&lt;String, String&gt; restUrlParams = new HashMap&lt;&gt;();
  10. restUrlParams.put(REST_URL_PARAMETER, searchItem);
  11. HttpEntity&lt;String&gt; entity = getRestParameters(trackingId);
  12. DataAtomsResponse dataAtomsResponse;
  13. LOGGER.info(&quot;Request RestService trackingID: {}&quot;, trackingId);
  14. ResponseEntity&lt;String&gt; dataResponse=
  15. restTemplate.exchange(config.getRestServiceUrl(), HttpMethod.GET, entity, String.class, restUrlParams);
  16. if (dataResponse.getStatusCode() == HttpStatus.OK || dataResponse.getStatusCode() == HttpStatus.NOT_FOUND) {
  17. LOGGER.debug(&quot;Transform result from RestService to JSON trackingID: {}&quot;, trackingId);
  18. dataAtomsResponse = dataParser.parse(dataResponse.getBody(), searchItem, trackingId);
  19. return ResponseEntity.ok(dataAtomsResponse );
  20. }
  21. else {
  22. throw new RestServiceNotAvailableException(dataResponse.getStatusCode().getReasonPhrase());
  23. }
  24. }

I have not implemented any ThreadPoolExecuter in any other class.

答案1

得分: 0

我发现了问题所在。
Histrix使用了普通的Java ThreadPoolExecutor,最大线程数设置为10。这篇文章对我帮助很大:https://medium.com/@truongminhtriet96/playing-with-hystrix-thread-pool-c7eebb5b0ddc。所以我设置了以下配置:

  1. hystrix.threadpool.default.maximumSize=32
  2. hystrix.threadpool.default.allowMaximumSizeToDivergeFromCoreSize=true
  3. server.tomcat.max-threads=32```
  4. <details>
  5. <summary>英文:</summary>
  6. I found out what was the problem.
  7. Histrix uses the normal java ThreadPoolExecutor and the value of maximum threads is set to 10. This https://medium.com/@truongminhtriet96/playing-with-hystrix-thread-pool-c7eebb5b0ddc article helped me alot. So I set these configs
  8. ```hystrix.threadpool.default.coreSize=10
  9. hystrix.threadpool.default.maximumSize=32
  10. hystrix.threadpool.default.allowMaximumSizeToDivergeFromCoreSize=true
  11. server.tomcat.max-threads=32```
  12. </details>

huangapple
  • 本文由 发表于 2020年9月21日 23:06:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63995034.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定