我使用Spring Boot构建了一个REST应用程序,后者又调用了另一个第三方REST API。我正在尝试将Apache的HTTPComponents CloseableHttpAsyncClient
与Spring AsyncRestTemplate
一起使用。这就是我在Spring Boot应用程序中设置AsyncRestTemplate
的方法:
@Bean
public AsyncRestTemplate asyncRestTemplate() throws IOReactorException {
ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor();
PoolingNHttpClientConnectionManager cm =
new PoolingNHttpClientConnectionManager(ioReactor);
CloseableHttpAsyncClient client =
HttpAsyncClients.custom().setConnectionManager(cm).build();
client.start();
return new AsyncRestTemplate(
new HttpComponentsAsyncClientHttpRequestFactory(client));
}
调用第三方REST API端点的代码如下:
ListenableFuture<ResponseEntity<String>> restFuture =
asyncRestTemplate.exchange(url, HttpMethod.GET, httpEntity, String.class);
当这一行执行时,我得到以下异常:
java.net.UnknownHostException: <<api.3rdpartyservices.com>>
at java.net.InetAddress.getAllByName0(InetAddress.java:1280) ~[na:1.8.0_112]
at java.net.InetAddress.getAllByName(InetAddress.java:1192) ~[na:1.8.0_112]
at java.net.InetAddress.getAllByName(InetAddress.java:1126) ~[na:1.8.0_112]
at org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:45) ~[httpclient-4.5.2.jar:4.5.2]
at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager$InternalAddressResolver.resolveRemoteAddress(PoolingNHttpClientConnectionManager.java:609) ~[httpasyncclient-4.1.2.jar:4.1.2]
at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager$InternalAddressResolver.resolveRemoteAddress(PoolingNHttpClientConnectionManager.java:580) ~[httpasyncclient-4.1.2.jar:4.1.2]
at org.apache.http.nio.pool.AbstractNIOConnPool.processPendingRequest(AbstractNIOConnPool.java:427) ~[httpcore-nio-4.4.5.jar:4.4.5]
at org.apache.http.nio.pool.AbstractNIOConnPool.lease(AbstractNIOConnPool.java:276) ~[httpcore-nio-4.4.5.jar:4.4.5]
at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager.requestConnection(PoolingNHttpClientConnectionManager.java:266) ~[httpasyncclient-4.1.2.jar:4.1.2]
at org.apache.http.impl.nio.client.AbstractClientExchangeHandler.requestConnection(AbstractClientExchangeHandler.java:363) ~[httpasyncclient-4.1.2.jar:4.1.2]
at org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl.start(DefaultClientExchangeHandlerImpl.java:125) ~[httpasyncclient-4.1.2.jar:4.1.2]
at org.apache.http.impl.nio.client.InternalHttpAsyncClient.execute(InternalHttpAsyncClient.java:141) ~[httpasyncclient-4.1.2.jar:4.1.2]
at org.apache.http.impl.nio.client.CloseableHttpAsyncClient.execute(CloseableHttpAsyncClient.java:75) ~[httpasyncclient-4.1.2.jar:4.1.2]
at org.apache.http.impl.nio.client.CloseableHttpAsyncClient.execute(CloseableHttpAsyncClient.java:108) ~[httpasyncclient-4.1.2.jar:4.1.2]
at org.springframework.http.client.HttpComponentsAsyncClientHttpRequest.executeInternal(HttpComponentsAsyncClientHttpRequest.java:96) ~[spring-web-4.3.4.RELEASE.jar:4.3.4.RELEASE]
但如果我按如下方式替换了AsyncRestTemplate的初始化,而没有改变任何其他内容:
@Bean
public AsyncRestTemplate asyncRestTemplate() throws IOReactorException {
return new AsyncRestTemplate(
new HttpComponentsAsyncClientHttpRequestFactory());
}
代码执行得很好。这是在Mac(OS X El Capitan)上。 Spring Boot版本是1.4.2,Java 8和HttpAsyncClient 4.1.2。我该如何解决这个问题?
注意:我用虚拟主机名(api.3rdpartyservices.com
)替换了实际主机名,因为我无法透露该信息。感谢@JimGarrison的评论,所以我可以为每个人澄清这一点。