我正在使用带有嵌入式Tomcat 9.0.36的Spring Boot。它在Kubernetes中用作Docker映像。最近,升级特使后,我开始出现异常。
"upstream connect error or disconnect/reset before headers. reset reason: connection termination" with 503 status code
[有人建议将空闲连接超时增加到60秒,但是它可以自动引导我找到“连接超时”和“保持活动超时”。我使用下面的代码将它们增加到5分钟。
@Configuration
public class TomcatCustomizer implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {
private static final Logger LOGGER = LoggerFactory.getLogger(TomcatCustomizer.class);
@Override
public void customize(TomcatServletWebServerFactory factory) {
factory.addConnectorCustomizers(connector -> {
AbstractHttp11Protocol protocol = (AbstractHttp11Protocol) connector.getProtocolHandler();
//Setting up connection time out
protocol.setKeepAliveTimeout(360000);
protocol.setConnectionTimeout(360000);
protocol.setMaxKeepAliveRequests(120);
});
}
}
仍然,我遇到了同样的错误。该应用程序内部调用了另一个服务,该服务也托管在Kubernetes中。我可以在服务中看到成功的响应,但是此后,我看不到任何日志。
我花了一个星期从应用程序的角度进行分析。我遵循了运营团队建议的一些步骤。
//Earlier (Forwarding same headers received from internal service to UI)
ResponseEntity responseEntity = //Received by calling other APIs;
return responseEntity;
//Now (Dropped headers)
ResponseEntity responseEntity = //Received by calling other APIs;
MultiValueMap<String, String> newHeaders = new LinkedMultiValueMap<>();
if (Objects.nonNull(responseEntity) && Objects.nonNull(responseEntity.getBody())) {
newHeaders.set("Content-type", responseEntity.getHeaders().getContentType().toString());
return new ResponseEntity(responseEntity.getBody(), newHeaders, responseEntity.getStatusCode());
}