当前位置: 首页 > 知识库问答 >
问题:

如何修复java.net.UnknownHostException错误,同时使用Spring Boot和Eureka服务器?

闻人凯泽
2023-03-14

我有一个名为“Documents-MicroService”的Spring Boot微服务,它注册在Eureka服务器中。我试图使用URL中的名称并使用resttemplate访问此微服务,如下所示:

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

虽然我确信服务名称在运行在8761端口上的Eureka服务器上可用(见下图),但我仍然得到以下错误:

错误:

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
    @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;
    }

共有1个答案

常甫
2023-03-14

您需要做以下几件事:

  • 首先:您需要RestTemplate配置而不是在客户机中,而是在调用该客户机的服务(1)中
@Configuration
public class WebClientConfiguration {

    @Bean @LoadBalanced
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
}

>

  • 第二:您需要相同服务的pom.xml中对应的依赖项(1)

  •  类似资料:
    • 今天在更新根文件夹中的play服务后,我面临以下问题。我不知道如何解决这个问题。 有人能帮我解决这个问题吗? 这个错误让人很恼火。我不知道冲突在哪里。顺便说一下,为什么它显示冲突,而没有版本是相互关联的。 错误: 库com.google.android.gms: play-services-metaming-base正在被[[15.0.0,15.0.0]、[15.0.2,15.0.2]]的其他库请

    • 根据这个博客 https://spring.io/blog/2015/07/14/microservices-with-spring 能够顺利运行应用程序。按此顺序: java-jarmicroservice-demo-0.0.1-SNAPSHOT.jar注册1111 java-jarmicroservice-demo-0.0.1-SNAPSHOT.jar帐户2222 java-jarmicros

    • 我试图通过ajax调用从表中获取数据。但我得到的响应是“500内部服务器错误”。 ajax调用的脚本如下所示 方法中调用。PHP如下所示 add_action('wp_ajax_nopriv_getsections','getsections');add_action('wp_ajax_getsections','getsections');

    • 我试图通过ajax调用从表中获取数据。但我得到的响应是“500内部服务器错误” ajax调用的脚本如下 要在functions.php调用的方法如下 add_action('wp_ajax_nopriv_getsections'、'getsections');add_action(‘wp_ajax_getsections’、‘getSection’);

    • 我正在为我们的应用程序使用Spring boot Microservices体系结构。在我们的项目中,我们使用的是OAuth2、Jwt、Zuul和Eureka服务,我的疑问是,我是否需要将这些服务作为一个独立的服务来实现,或者我是否可以将所有服务开发成一个单一的应用程序。 如果我必须作为单个应用程序实现,那么更好的方法是什么。请澄清

    • 我正在尝试注册一个客户端到spring-eureka-server,客户端在注册后就注销了 Eureka-客户端日志: 请告诉我有什么可能出了问题。