我有一个Spring Boot REST服务,有时会将第三方服务作为请求的一部分.我想在我的所有资源上设置一个超时(让我们说5秒),这样如果任何请求处理(整个链,从传入到响应)花费的时间超过5秒,我的控制器会响应HTTP 503而不是实际响应.如果这只是一个Spring属性,例如设置,那将是非常棒的
spring.mvc.async.request-timeout=5000
Run Code Online (Sandbox Code Playgroud)
但我没有运气.我也尝试过扩展WebMvcConfigurationSupport并覆盖configureAsyncSupport:
@Override
public void configureAsyncSupport(final AsyncSupportConfigurer configurer) {
configurer.setDefaultTimeout(5000);
configurer.registerCallableInterceptors(timeoutInterceptor());
}
@Bean
public TimeoutCallableProcessingInterceptor timeoutInterceptor() {
return new TimeoutCallableProcessingInterceptor();
}
Run Code Online (Sandbox Code Playgroud)
没有运气.
我怀疑我必须手动计算所有第三方呼叫的时间,如果它们花费的时间太长,则抛出超时异常.是对的吗?或者是否有涵盖我所有请求端点的更简单,整体的解决方案?
我是否在application.properties文件中配置了像connectionTimeout这样的属性,或者在其他地方配置它?我无法从谷歌那里得知这一点.
我找到了这个Spring-Boot示例,但它不包含connectionTimeout属性,当我server.tomcat.connectionTimeout=60000在application.properties文件中设置时,我收到一个错误.
我收到 504 网关超时问题 Spring Boot Rest 调用使用 HTTP GET 调用重记录(超过 80K),我正在调用其他服务以使用 RestTemplate 对象 resClient 获取数据,代码如下:
public ResponseEntity<String> getData(String endPointUrl, Map<String, Object> parameterMap, String smToken) throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
headers.add("Cookie", smToken);
//headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
HttpEntity<Map<String, Object>> entity = new HttpEntity<Map<String, Object>>(parameterMap, headers);
ResponseEntity<String> responseEntity = null;
try {
SSLUtil.turnOffSslChecking();
LOGGER.info("Start call to end point : " +endPointUrl+ " at :"+ (new Date().toString()) );
//resClient.getMessageConverters()
//.add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
responseEntity = resClient.exchange(endPointUrl, HttpMethod.POST, entity,String.class);
LOGGER.info("End of call to end point …Run Code Online (Sandbox Code Playgroud)