本文介绍了在随机端口上运行微服务时,Eureka无法找到端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Spring Boot应用程序中,我正在使用eureka进行服务发现,并使用功能区进行负载平衡.当我在固定端口上运行在eureka上注册的微服务时,它可以正常工作,但是当我在随机端口上运行它们时,尽管我可以看到在eureka仪表板上注册的服务,但找不到端口号.并且尝试启动该服务时收到以下错误.

I am using eureka for service discover and ribbon for load balancing in my spring boot application. When i run my micro services which are registered with eureka on a fix port it works fine but when i run them on random port though i can see services registered on eureka dashboard it is not able to find the port number. and i get the following error when trying to hit the service.

   2018-11-27 07:55:15.853  INFO 7240 --- [nio-8079-exec-1] c.n.l.DynamicServerListLoadBalancer      : DynamicServerListLoadBalancer for client MYAPP initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=MYAPP,current list of Servers=[nawnit:0, nawnit:0],Load balancer stats=Zone stats: {defaultzone=[Zone:defaultzone;    Instance count:2;   Active connections count: 0;    Circuit breaker tripped count: 0;   Active connections per server: 0.0;]
},Server stats: [[Server:nawnit:0;  Zone:defaultZone;   Total Requests:0;   Successive connection failure:0;    Total blackout seconds:0;   Last connection made:Thu Jan 01 05:30:00 IST 1970;  First connection made: Thu Jan 01 05:30:00 IST 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@563d5f30
2018-11-27 07:55:15.907 ERROR 7240 --- [nio-8079-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 POST request for "http://MYAPP/validation/1": connect: Address is invalid on local machine, or port is not valid on remote machine; nested exception is java.net.ConnectException: connect: Address is invalid on local machine, or port is not valid on remote machine] with root cause

java.net.ConnectException: connect: Address is invalid on local machine, or port is not valid on remote machine
    at java.net.DualStackPlainSocketImpl.connect0(Native Method) ~[na:1.8.0_181]
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79) ~[na:1.8.0_181]
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_181]
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_181]
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_181]
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[na:1.8.0_181]
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_181]
    at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_181]
    at java.net.Socket.connect(Socket.java:538) ~[na:1.8.0_181]
    at sun.net.NetworkClient.doConnect(NetworkClient.java:180) ~[na:1.8.0_181]
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:463) ~[na:1.8.0_181]
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:558) ~[na:1.8.0_181]
    at sun.net.www.http.HttpClient.<init>(HttpClient.java:242) ~[na:1.8.0_181]
    at sun.net.www.http.HttpClient.New(HttpClient.java:339) ~[na:1.8.0_181]
    at sun.net.www.http.HttpClient.New(HttpClient.java:357) ~[na:1.8.0_181]
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1220) ~[na:1.8.0_181]
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1156) ~[na:1.8.0_181]
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1050) ~[na:1.8.0_181]
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:984) ~[na:1.8.0_181]
    at org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:76) ~[spring-web-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.spr

这是我正在从中调用其他服务的控制器类.

public ResponseEntity<BaseResponse> verifyEmail(@RequestBody CustomerIdentity identity,
            @PathVariable Integer simID) throws HttpClientErrorException {
        System.out.println(this.discoveryClient.getInstances("MYAPP").get(0).getUri().toString());
        return restTemplate.postForEntity("http://MYAPP"+ "/validation/" + simID, identity, BaseResponse.class);
    }

这是我添加的属性

eureka.client.service-url.defaultZone=http://localhost:8761/eureka

我的服务已在eureka上注册,名称为 MYAPP .我还尝试打印该URL,如您所见,它在固定端口上运行但在随机端口上运行时给出http://nawnit:8080我得到http://nawnit:0 (nawnit是我的电脑名称)

My service is registered with eureka with name MYAPP .i have also tried the print the url as you can see,it gives http://nawnit:8080 when running on a fix port but when running on random port i get http://nawnit:0 (nawnit is my pc name)

这是我的eureka服务器的配置属性.

server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
logging.level.com.netflix.eureka=OFF
logging.level.com.netflix.discovery=OFF

eureka客户端的pom.xml:

pom.xml of eureka client:

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.M3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>

注意:我正在使用的restTemplate被配置为负载平衡.

Note: The restTemplate that i am using is configured as load balanced.

推荐答案

这是一个错误,已得到修复.参见 https://github.com/spring-cloud/spring-cloud -netflix/issues/3294 进行一次重新创建问题的测试.还在此处进行了修复 https://github.com/spring-cloud /spring-cloud-commons/issues/451

This is a bug and has been fixed. See https://github.com/spring-cloud/spring-cloud-netflix/issues/3294 for a test that recreated the issue. Fixes were also made here https://github.com/spring-cloud/spring-cloud-commons/issues/451

应在下周(2018年12月6日)作为Greenwich.RC1的一部分提供.参见 https://github.com/spring-cloud/spring-cloud-release /milestones 进行更新.

This should be available next week (Dec 6, 2018) as part of Greenwich.RC1. See https://github.com/spring-cloud/spring-cloud-release/milestones for updates.

这篇关于在随机端口上运行微服务时,Eureka无法找到端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-26 11:40