RestTemplate 调用本地服务 connection refused

当需要使用服务间的互相调用的时候,通常来说最优雅的方式莫过于Feign调用了。但是有时候特殊原因还是需要使用httpClient之类的工具。

本次我在使用RestTemplate调用本地服务的时候,会出现如下错误:

Servlet.service() for servlet [dispatcherServlet] in context with path [] 
threw exception [Request processing failed; nested exception is org.springframework.web.client.ResourceAccessException:
I/O error on GET request for "https://xx/xx/xx": Connect to xx:xx [xx/xx] failed:
Connection refused (Connection refused); nested exception is org.apache.http.conn.HttpHostConnectException:
Connect to xx:xx [xx/xx] failed: Connection refused (Connection refused)]
with root cause

用同样的代码调用其他服务器上的服务时并未出现这样的错误,曾一度怀疑是url有误,然而浪费了很多时间去尝试这个。然后又怀疑是网关,或防火墙阻拦了?,然而又浪费了很多时间。

刷了会微博冷静了下,才想起来可能是url地址的写法有误。

当调用本地服务的时候,url应该写成

https://localhost:xx/xx/xx

(我想应该也可以写成服务名代替ip的那种吧,)

然而,改完之后,还是有问题,不过还好的是错误变了:

org.springframework.web.util.NestedServletException: Request processing failed; 
nested exception is org.springframework.web.client.ResourceAccessException: I/O error on GET request for "https://localhost:xx/xx/xx":
Unrecognized SSL message, plaintext connection?; nested exception is javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?

这个错是使用https请求了http服务,很好改:

http://localhost:xx/xx/xx

o了。



原文地址:https://www.cnblogs.com/goblinn/p/10882527.html