Resourceaccessexception: I / O error on post request for and connection timed out

1. Cause of error:

When the resttemplate is used to call the third-party API, the local test is normal, but when it is deployed to the server, it will report: Dan, 19:06 org.springframework.web . client.ResourceAccessException : I/O error on POST request for "XXX": No route to host (Host unreachable); nested exception is java.net.NoRouteToHostException : no route to host (host unreachable) , at first, I thought that the timeout was not set, so I configured it through online methods

 SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
 requestFactory.setConnectTimeout(10*1000);
 requestFactory.setReadTimeout(10*1000);
 RestTemplate rest = new RestTemplate(requestFactory);	

Deployment or error

2. Solutions

Finally, the interface setting the timeout time is replaced with httpcomponentsclienthttprequestfactory to solve the problem successfully

 HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
 requestFactory.setConnectTimeout(10*1000);
 requestFactory.setReadTimeout(10*1000);
 RestTemplate rest = new RestTemplate(requestFactory);

Read More: