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

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

Spring Boot java.util.concurrent.ThreadPoolExecutor size

问题

目前我正在测试我的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] 中拒绝


所以我的问题是线程池的最大大小真的是10吗?我能否将它设置为其他值?

我找到了属性 `server.tomcat.max-threads` 并将其设置为10,所有请求都将通过。

**编辑**

我正在使用Spring Boot RestTemplate调用另一个REST服务,这可能导致问题吗?

@HystrixCommand(
    commandKey = "callRestService", fallbackMethod = "failCallRestService", ignoreExceptions = DataNotFoundException.class)
public ResponseEntity<DataAtomsResponse> callRestService(String searchItem, String trackingId)
    throws RestServiceNotAvailableException
{
    ThreadContext.put("trackingID", trackingId);
    configureRestTemplate();
    LOGGER.info("收到带有trackingId的请求:{}", trackingId);
    Map<String, String> restUrlParams = new HashMap<>();
    restUrlParams.put(REST_URL_PARAMETER, searchItem);
    HttpEntity<String> entity = getRestParameters(trackingId);

    DataAtomsResponse dataAtomsResponse;
    LOGGER.info("请求RestService跟踪ID:{}", trackingId);
    ResponseEntity<String> dataResponse =
        restTemplate.exchange(config.getRestServiceUrl(), HttpMethod.GET, entity, String.class, restUrlParams);

    if (dataResponse.getStatusCode() == HttpStatus.OK || dataResponse.getStatusCode() == HttpStatus.NOT_FOUND) {
        LOGGER.debug("将RestService的结果转换为JSON跟踪ID:{}", trackingId);
        dataAtomsResponse = dataParser.parse(dataResponse.getBody(), searchItem, trackingId);
        return ResponseEntity.ok(dataAtomsResponse);
    }
    else {
        throw new RestServiceNotAvailableException(dataResponse.getStatusCode().getReasonPhrase());
    }
}

我在任何其他类中都没有实现过 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:

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?

    @HystrixCommand(
        commandKey = &quot;callRestService&quot;, fallbackMethod = &quot;failCallRestService&quot;, ignoreExceptions = DataNotFoundException.class)
    public ResponseEntity&lt;DataAtomsResponse&gt; callRestService(String searchItem, String trackingId)
        throws RestServiceNotAvailableException
    {
        ThreadContext.put(&quot;trackingID&quot;, trackingId);
        configureRestTemplate();
        LOGGER.info(&quot;Received request with trackingId: {}&quot;, trackingId);
        Map&lt;String, String&gt; restUrlParams = new HashMap&lt;&gt;();
        restUrlParams.put(REST_URL_PARAMETER, searchItem);
        HttpEntity&lt;String&gt; entity = getRestParameters(trackingId);

        DataAtomsResponse dataAtomsResponse;
        LOGGER.info(&quot;Request RestService trackingID: {}&quot;, trackingId);
        ResponseEntity&lt;String&gt; dataResponse=
            restTemplate.exchange(config.getRestServiceUrl(), HttpMethod.GET, entity, String.class, restUrlParams);

        if (dataResponse.getStatusCode() == HttpStatus.OK || dataResponse.getStatusCode() == HttpStatus.NOT_FOUND) {
            LOGGER.debug(&quot;Transform result from RestService to JSON trackingID: {}&quot;, trackingId);
            dataAtomsResponse = dataParser.parse(dataResponse.getBody(), searchItem, trackingId);
            return ResponseEntity.ok(dataAtomsResponse );
        }
        else {
            throw new RestServiceNotAvailableException(dataResponse.getStatusCode().getReasonPhrase());
        }
    }

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。所以我设置了以下配置:

hystrix.threadpool.default.maximumSize=32
hystrix.threadpool.default.allowMaximumSizeToDivergeFromCoreSize=true
server.tomcat.max-threads=32```

<details>
<summary>英文:</summary>

I found out what was the problem.
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

```hystrix.threadpool.default.coreSize=10
hystrix.threadpool.default.maximumSize=32
hystrix.threadpool.default.allowMaximumSizeToDivergeFromCoreSize=true
server.tomcat.max-threads=32```

</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:

确定