简体   繁体   English

如何在使用 Spring Boot 和 Eureka Server 时修复错误 java.net.UnknownHostException?

[英]How to fix the error java.net.UnknownHostException while using Spring Boot and Eureka Server?

I have a Spring Boot microservice called "documents-microservice", which is registered in Eureka Server.我有一个名为“documents-microservice”的 Spring Boot 微服务,它在 Eureka Server 中注册。 I am trying to access this microservice using its name in the URL and with RestTemplate , as the following:我正在尝试使用 URL 中的名称和RestTemplate访问此微服务,如下所示:

ResponseEntity<String> response =    restTemplate.exchange("http://documents-microservice/document-name", HttpMethod.POST, entity, String.class);

Although I am sure that the service name is available on Eureka Server which is running on port 8761 (See picture below), I still get the following errors:尽管我确定在端口 8761 上运行的 Eureka Server 上可以使用该服务名称(见下图),但我仍然收到以下错误:

尤里卡服务器运行

Errors:错误:

org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://documents-microservice/document-name": documents-microservice; nested exception is java.net.UnknownHostException: documents-microservice
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:674)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:621)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:539)
    at ae.gov.adm.saeed.web.controller.util.CircularsControllerUtil.circularListView(CircularsControllerUtil.java:196)
    at ae.gov.adm.saeed.web.controller.CircularsController.viewCircularList(CircularsController.java:58)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at ae.gov.adm.saeed.web.security.AuthFilter.doFilter(AuthFilter.java:335)
    at ae.gov.adm.saeed.web.security.AuthFilter.doFilter(AuthFilter.java:610)
    at ae.gov.adm.common.web.filter.AbstractFilter.doFilter(AbstractFilter.java:47)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:417)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:754)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1376)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.UnknownHostException: documents-microservice
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at java.net.Socket.connect(Socket.java:538)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:180)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:463)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:558)
    at sun.net.www.http.HttpClient.<init>(HttpClient.java:242)
    at sun.net.www.http.HttpClient.New(HttpClient.java:339)
    at sun.net.www.http.HttpClient.New(HttpClient.java:357)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1220)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1156)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1050)
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:984)
    at org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:78)
    at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)
    at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:660)
    ... 52 more
I/O error on POST request for "http://documents-microservice/document-name": documents-microservice; nested exception is java.net.UnknownHostException: documents-microservice

Update: Please note that I have the following method in the client for setting RestTemplate:更新:请注意,我在客户端有以下设置 RestTemplate 的方法:

    @Bean //this methods will be executed only once to avoid getting multiple instances.
    @LoadBalanced //I will not give you service URL, but rather a hint for what you need to discover.
    public RestTemplate getRestTemplate()
    {
         final RestTemplate restTemplate = new RestTemplate();

         System.out.println("rest template defined");

         return  restTemplate;
    }

Update: Please note that currently, my client code is not a Spring Boot Application.更新:请注意,目前,我的客户端代码不是 Spring Boot 应用程序。

There are several things that you need to do:您需要做几件事:

  • first: you need the RestTemplate configuration not in the client, but in the service(1) that calls that client第一:您需要的不是在客户端中的 RestTemplate 配置,而是在调用该客户端的service(1)
@Configuration
public class WebClientConfiguration {

    @Bean @LoadBalanced
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
}
  • second: you need the corresponding dependencies in the pom.xml of the same service(1)第二:需要同服务的pom.xml中对应的依赖(1)

  • third: add @EnableDiscoveryClient in your main application第三:在你的主应用程序中添加@EnableDiscoveryClient

And other configurations , but you must provide a full example to help you more.其他配置一样,但是你必须提供一个完整的例子来帮助你更多。
This example can help you这个例子可以帮助你

You can access it by name "documents-microservice" via a @Feign client, otherwise you should try the gateway EG, hqtpm001....:8081您可以通过@Feign 客户端按名称“documents-microservice”访问它,否则您应该尝试网关 EG,hqtpm001....:8081

Furthermore, Eureka server is not the host of the application, it is more or less the traffic cop and lets you as the client know where to go to get the application.此外,Eureka 服务器不是应用程序的主机,它或多或少是交通警察,让您作为客户端知道去哪里获取应用程序。 aka the Service Registry又名服务注册中心

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 【Eureka&springCloud】java.net.UnknownHostException - 【Eureka&springCloud】java.net.UnknownHostException java.net.UnknownHostException来自spring boot应用程序的dockerized mysql - java.net.UnknownHostException dockerized mysql from spring boot application 将 Spring 引导容器连接到 Mongo 容器:java.net.UnknownHostException:mongo:系统错误? - Connecting Spring boot Container to Mongo Container: java.net.UnknownHostException: mongo: System error? 无法连接到数据库 java.net.UnknownHostException 使用 Spring boot 和elephantsql - Cannot connect to database java.net.UnknownHostException using Spring boot and elephantsql 获取 java.net.UnknownHostException: hostname: Name or service not known while using spring-data-redis-starter - Getting java.net.UnknownHostException: hostname: Name or service not known while using spring-data-redis-starter Spring Cloud Gateway Kubernetes Eureka java.net.UnknownHostException:无法解决 - Spring Cloud Gateway Kubernetes Eureka java.net.UnknownHostException: failed to resolve 错误:使用 Java =&gt; java.net.UnknownHostException 连接 Google Cloud Storage 时:oauth2.googleapis.com - Error: while connecting Google Cloud Storage using Java => java.net.UnknownHostException: oauth2.googleapis.com 通过 WebDriverManager 使用 Selenium 时如何修复 java.net.UnknownHostException: npm.taobao.org - How to fix java.net.UnknownHostException: npm.taobao.org when using Selenium through WebDriverManager 的java.net.UnknownHostException - java.net.UnknownHostException java.net.UnknownHostException:&lt; <hostname> &gt;:未知错误 - java.net.UnknownHostException: <<hostname>> : unknown error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM