spring cloud ribbon 服务名无法转换成IP

发布于 2022-09-07 09:02:06 字数 1362 浏览 21 评论 0

服务已经注册,如图:

clipboard.png

服务调用代码如下:
启动类:

@SpringCloudApplication
public class RibbonConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(RibbonConsumerApplication.class, args);
    }

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

}

// Controller类
@RestController
public class ConsumerController {
    @Autowired
    RestTemplate restTemplate;

    @RequestMapping(value = "/ribbon-consumer", method = RequestMethod.GET)
    public String helloController() {
        String url = "http://HELLOSERVICE/hello";
        ResponseEntity<String> forEntity = restTemplate.getForEntity(url, String.class);
        return forEntity.getBody();
    }

    public String error() {
        return "error";
    }

}

配置文件application.properties
spring.application.name=ribbon-consumer
server.port=9111
eureka.client.service-url.defaultZone=http://localhost:1111/eureka

但是总是报错:
java.net.UnknownHostException: HELLOSERVICE

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

﹏半生如梦愿梦如真 2022-09-14 09:02:06

有人会解决这个问题吗?

≈。彩虹 2022-09-14 09:02:06

我也遇到相似的情况,但不确定是否和上面的是一样的,具体原因未知。
使用的spring-boot版本是2.1.7-release,降级到2.0.7-release解决了。

2.1.7-release:

eureka注册:
图片描述

相关配置也没什么问题,但是consumer获取不到provider的server list

2019-02-22 15:27:31.355  INFO 98140 --- [nio-8001-exec-1] c.netflix.loadbalancer.BaseLoadBalancer  : Client: SPRING-CLOUD-PROVIDER instantiated a LoadBalancer: DynamicServerListLoadBalancer:{NFLoadBalancer:name=SPRING-CLOUD-PROVIDER,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:null
2019-02-22 15:27:31.363  INFO 98140 --- [nio-8001-exec-1] c.n.l.DynamicServerListLoadBalancer      : Using serverListUpdater PollingServerListUpdater
2019-02-22 15:27:31.397  INFO 98140 --- [nio-8001-exec-1] c.netflix.config.ChainedDynamicProperty  : Flipping property: SPRING-CLOUD-PROVIDER.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2019-02-22 15:27:31.399  INFO 98140 --- [nio-8001-exec-1] c.n.l.DynamicServerListLoadBalancer      : DynamicServerListLoadBalancer for client SPRING-CLOUD-PROVIDER initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=SPRING-CLOUD-PROVIDER,current list of Servers=[172.17.53.148:8000],Load balancer stats=Zone stats: {defaultzone=[Zone:defaultzone;    Instance count:1;    Active connections count: 0;    Circuit breaker tripped count: 0;    Active connections per server: 0.0;]
},Server stats: [[Server:172.17.53.148:8000;    Zone:defaultZone;    Total Requests:0;    Successive connection failure:0;    Total blackout seconds:0;    Last connection made:Thu Jan 01 08:00:00 CST 1970;    First connection made: Thu Jan 01 08:00:00 CST 1970;    Active Connections:0;    total failure count in last (1000) msecs:0;    average resp time:0.0;    90 percentile resp time:0.0;    95 percentile resp time:0.0;    min resp time:0.0;    max resp time:0.0;    stddev resp time:0.0]
]}ServerList:org.springframework.cloud.netflix.ribbon.eureka.DomainExtractingServerList@1ee56dd7
2019-02-22 15:27:31.862 ERROR 98140 --- [nio-8001-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.HttpServerErrorException$ServiceUnavailable: 503 Service Unavailable] with root cause

org.springframework.web.client.HttpServerErrorException$ServiceUnavailable: 503 Service Unavailable
    at org.springframework.web.client.HttpServerErrorException.create(HttpServerErrorException.java:85) ~[spring-web-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:124) ~[spring-web-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:102) ~[spring-web-5.1.5.RELEASE.jar:5.1.5.RELEASE]

debug发现请求的地址果然是http://spring-cloud-provider/...,没有解析

2.0.7-release:

eureka注册:
图片描述

相关日志:

2019-02-22 15:44:45.728  INFO 98465 --- [nio-8001-exec-1] c.n.l.DynamicServerListLoadBalancer      : DynamicServerListLoadBalancer for client spring-cloud-provider initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=spring-cloud-provider,current list of Servers=[172.17.53.148:8000],Load balancer stats=Zone stats: {defaultzone=[Zone:defaultzone;    Instance count:1;    Active connections count: 0;    Circuit breaker tripped count: 0;    Active connections per server: 0.0;]
},Server stats: [[Server:172.17.53.148:8000;    Zone:defaultZone;    Total Requests:0;    Successive connection failure:0;    Total blackout seconds:0;    Last connection made:Thu Jan 01 08:00:00 CST 1970;    First connection made: Thu Jan 01 08:00:00 CST 1970;    Active Connections:0;    total failure count in last (1000) msecs:0;    average resp time:0.0;    90 percentile resp time:0.0;    95 percentile resp time:0.0;    min resp time:0.0;    max resp time:0.0;    stddev resp time:0.0]
]}ServerList:org.springframework.cloud.netflix.ribbon.eureka.DomainExtractingServerList@132e4091
2019-02-22 15:44:46.679  INFO 98465 --- [erListUpdater-0] c.netflix.config.ChainedDynamicProperty  : Flipping property: spring-cloud-provider.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647

调用成功,拿到数据了,current list of Servers=[172.17.53.148:8000]也是正常的。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文