新手上路Springboot+Nacos报错java.net.UnknownHostException:serviceName

java.net.UnknownHostException: routeservice
	at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:196) ~[na:1.8.0_381]
	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:162) ~[na:1.8.0_381]
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:394) ~[na:1.8.0_381]
	at java.net.Socket.connect(Socket.java:606) ~[na:1.8.0_381]
	at sun.net.NetworkClient.doConnect(NetworkClient.java:175) ~[na:1.8.0_381]
	at sun.net.www.http.HttpClient.openServer(HttpClient.java:499) ~[na:1.8.0_381]
	at sun.net.www.http.HttpClient.openServer(HttpClient.java:594) ~[na:1.8.0_381]
	at sun.net.www.http.HttpClient.(HttpClient.java:278) ~[na:1.8.0_381]
	at sun.net.www.http.HttpClient.New(HttpClient.java:375) ~[na:1.8.0_381]
	at sun.net.www.http.HttpClient.New(HttpClient.java:393) ~[na:1.8.0_381]
	at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1240) ~[na:1.8.0_381]
	at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1167) ~[na:1.8.0_381]
	at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1061) ~[na:1.8.0_381]
	at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:995) ~[na:1.8.0_381]
	at org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:76) ~[spring-web-5.2.15.RELEASE.jar:5.2.15.RELEASE]
	at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48) ~[spring-web-5.2.15.RELEASE.jar:5.2.15.RELEASE]
	at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53) ~[spring-web-5.2.15.RELEASE.jar:5.2.15.RELEASE]
	at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:737) ~[spring-web-5.2.15.RELEASE.jar:5.2.15.RELEASE]
	at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:672) ~[spring-web-5.2.15.RELEASE.jar:5.2.15.RELEASE]
	at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:581) ~[spring-web-5.2.15.RELEASE.jar:5.2.15.RELEASE]
	at com.yang.controller.TravelController.getDetailInfo(TravelController.java:43) ~[classes/:na]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_381]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_381]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_381]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_381]
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.15.RELEASE.jar:5.2.15.RELEASE]
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.2.15.RELEASE.jar:5.2.15.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) ~[spring-webmvc-5.2.15.RELEASE.jar:5.2.15.RELEASE]

#版本信息
springboot:2.3.12.RELEASE
alibaba.cloud:2.2.7.RElEASE
nacos-server:2.0.3
spring-cloud-starter-alibaba-nacos-discovery:2.2.7.RElEASE

一开始以为是我使用alibaba-nacos版本是不自带ribbon,无法实现负载均衡,并通过restTemplate发送HTTP请求,用服务名访问其他服务,结果察看

        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
            2.2.7.RELEASE
        

依赖包里面是有ribbon的。

解决办法:

在自定义的RestTemplateConfig配置中的restTemplate函数上添加注解@LoadBalanced。

新手上路Springboot+Nacos报错java.net.UnknownHostException:serviceName_第1张图片

实现微服务之间的服务通过服务名调用。

你可能感兴趣的:(java,spring,boot,.net)