k8s 部署 springcloud 微服务遇到 consul 报 java.net.unkonwHostException:failed to resolve
一、问题
服务调用java.net.UnknownHostException(集群环境,本地环境不会有)
feign,loadBalancer,discoveryClient都返回了hostname而非IP地址。本地测试的时候显示IP address.
2020-12-21 06:21:06.576 ERROR 6 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : 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 "http://gateway-service/xxx/xxx": gateway-deploy-77456755b7-gsxrj; nested exception is java.net.UnknownHostException: gateway-deploy-77456755b7-gsxrj] with root cause
java.net.UnknownHostException: gateway-deploy-77456755b7-gsxrj
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184) ~[na:1.8.0_121]
二、解决
添加配置
为了解决这个问题,需要在注册服务的时候,让服务以ip的方式注册:
#${spring.cloud.client.ip-address}这个属性是spring cloud内置,用来获取ip,
spring.cloud.consul.discovery.prefer-ip-address=true
相关文章:
用阿里云K8S搭建SpringCloud(服务发现:Consul)微服务--问题小结
基于consul的服务注册
为者常成,行者常至
自由转载-非商用-非衍生-保持署名(创意共享3.0许可证)