Java Spring Gateway — Connection prematurely closed
While working on a Java Spring Gateway, you can have this kind of issue which seems to come from out of nowhere.
reactor.netty.http.client.PrematureCloseException: Connection prematurely closed DURING response
Solution 1 — Update configs
After reviewing a lot of GitHub [1] or StackOverflow [2] posts, the only thing I saw to correct it was to:
- update the netty library
In the case of Spring Gateway, update the spring library because it embeds netty, or you can override the version used in the pom.xml file if the latest version of Spring doesn’t use the latest version of netty.
- update some configs
Solution 2 — Check infrastructure
But maybe it won’t help you that much. If you continue to have this issue after trying to, check your infrastructure.
Get Maxime Guilbert’s stories in your inbox
Join Medium for free to get updates from this writer.
In our case, our load balancers were the problem. (For information, my services are deployed on AWS.)
For some reason, I changed our CLB (Classic Load Balancer [3]) for a NLB (Network Load Balancer [4]). But the default configuration wasn’t correct for my usage. So due to that, I randomly had this “Connection prematurely closed” error. Then, when I go back to CLB, it worked well.
Currently, I didn’t take the time to explore why this is happening with a NLB, but I really want to share this point that I didn’t see anywhere else.
I hope it will help you!
Links
[1] Github links :
- https://github.com/spring-cloud/spring-cloud-gateway/issues/2046
- https://github.com/spring-cloud/spring-cloud-gateway/issues/1148
[2] StackOverFlow links :
- https://stackoverflow.com/questions/53683273/spring-cloud-gateway-gives-connection-closed-prematurely
[3] AWS Classic Load Balancer : https://docs.aws.amazon.com/elasticloadbalancing/latest/classic/introduction.html
[4] AWS Network Load Balancer : https://docs.aws.amazon.com/elasticloadbalancing/latest/network/introduction.html
First publish january 25th on dev.to








