upstream connect error or disconnect/reset before headers. reset reason: connection termination when using Spring Boot
I spent a week analyzing this from the application point of view. I followed a few steps that were suggested by the Ops Team.
- Increase Timeout in Tomcat Server to 60 seconds because they have configured the same in Envoy
- I did increase the time but not able to solve the issue.
- I was using Spring Cloud Gateway for Gateway service I thought that is the issue so I changed it into Rest Templates but that also didn't solve the issue.
- Luckily Health Check APIs working fine except those which were communicating to other services internally. In Health APIs they were also communicating with other services to check their Health but I was not returning response directly. I was wrapping up the response body modifying it and den forwarding it to UI. I also applied the same and use the below code which you can understand easily.I created a new Response Entity and dropped all headers which I received from internal APIs and returned to UI. It worked like charm.
//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());}