spring-cloud-netflix icon indicating copy to clipboard operation
spring-cloud-netflix copied to clipboard

eureka client instance registers incorrectly if 'server.port' is set to 0

Open kdvolder opened this issue 11 years ago • 14 comments

I created basic service that registers itself with a local Eureka server. The application .yml of my client looks like this:

server:
  port: 8001
spring:
  application:
    name: date-service
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

That works.

When I change 'server.port' to '0' so the port is chosen dynamically then the service doesn't appear to register itself correctly anymore. (It still registers, but with the wrong port, 80).

Being able to use dynamic port would be nice, if one wants to run multiple instances of a service locally without having to create multiple configurations of it (i.e. just relaunch the same thing a few times and have each running instance dynamically pick a port).

kdvolder avatar Oct 02 '15 17:10 kdvolder

I just tried it and it worked. What version are you using?

spencergibb avatar Oct 02 '15 17:10 spencergibb

I recreated it. This used to work, so it is a regression. Though the workaround is to set a port. Random ports were built for testing.

spencergibb avatar Oct 09 '15 17:10 spencergibb

Hi @spencergibb I just tested this with Brixton M4 which has 1.1.0.M4 of cloud-netflix (which is where I am guessing this fix went in?) but I am still having this issue. Here's a test service that you can start up and register to a eureka server. Port is random (server.port is 0) but when it registers multiple instances with the same name/ip (locally) it overwrites them. Here's the test service: https://github.com/shivangshah/test-service

shivangshah avatar Jan 04 '16 22:01 shivangshah

Another weird thing that I found when I was setting server.port to actual values, lets say 8080 and 8081, from the eureka UI, it indeed does show 2 instances running BUT when you click on those instances, they both point to the same url of "info" .. and that is of the one that registered first .. in my case it was 8080 .. Not sure if that's how it's supposed to be?

shivangshah avatar Jan 04 '16 22:01 shivangshah

@shivangshah could you please open a new issue.

spencergibb avatar Jan 20 '16 00:01 spencergibb

I'm trying with Dalston.SR1 same happens. If server.port=0 (random) eureka only show (and gives traffic) to the last registered, leaving formers unusable.

Can we reopen the issue or should i create a new one?

Canos avatar Jul 04 '17 15:07 Canos

Have you set eurkea.instance.instanceId? Otherwise with port=0 eureka will think it is one instance

spencergibb avatar Jul 04 '17 15:07 spencergibb

Perfect, an instanceId with a random value works. Sorry, i'm still a newbie.

thanks

Canos avatar Jul 04 '17 15:07 Canos

after that port number is generated....is there anyway i can get that value? If I use "${server.port}" ill get 0.

TimB0 avatar Jul 17 '17 00:07 TimB0

There is an event ServletWebServerInitializedEvent that you can listen to. @TimB0

spencergibb avatar Jul 18 '17 21:07 spencergibb

@TimB0 I know this is already too late but it may help others. You could have used "${local.server.port}" instead.

tminussi avatar Nov 17 '17 13:11 tminussi

This problem of using a random port and the service not registering with the discovery service, is still happening:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>Finchley.RELEASE</version>
    <type>pom</type>
    <scope>import</scope>
  </dependency>

bootstrap.properties, port 0 commented out (defaults to 8080):

# server.port=0

works:

2019-08-08 16:19:01.707  INFO 18020 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_SERVICEALPHA/localhost:servicealpha: registering service...
2019-08-08 16:19:01.751  INFO 18020 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path '/eurekaservice'
2019-08-08 16:19:01.755  INFO 18020 --- [           main] .d.o.m.e.ExampleEurekaServiceApplication : Started ExampleEurekaServiceApplication in 11.2 seconds (JVM running for 12.782)
2019-08-08 16:19:01.764  INFO 18020 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_SERVICEALPHA/localhost:servicealpha - registration status: 204

bootstrap.properties, add server.port=0 so that it's using a random port:

server.port=0

result is that it doesn't register with the eureka discovery service anymore:

2019-08-08 16:22:43.758  INFO 19776 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 2 endpoint(s) beneath base path '/actuator'
2019-08-08 16:22:43.897  INFO 19776 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 60973 (http) with context path '/eurekaservice'
2019-08-08 16:22:43.900  INFO 19776 --- [           main] .d.o.m.e.ExampleEurekaServiceApplication : Started ExampleEurekaServiceApplication in 10.67 seconds (JVM running for 12.327)

alanlupsha avatar Aug 08 '19 20:08 alanlupsha

Please use Greenwich.SR2 and if the problem still exists please open a separate issue

ryanjbaxter avatar Aug 14 '19 15:08 ryanjbaxter

Hi. I meet a similar problem server.port = ${random.int[8091,8099]} Eurica client log says that Tomcat started on port(s): 8092 (http) with context path ''" I see on the Eureka server's GUI: Fujitsu:custom-app:8091 as the only registered instance. But it is not the all: On the client side I try this.discoveryClient.getInstances(applicationName); I Use GET on on http://fujitsu:8092/service-instances/custom-app (8092! as Embeded Tomcat logs!! on client- it means that EUREKA sees the embedded instance of Tomcat properly! This is that I see in JSON of ServiceInstance list {instances":[{"port":8093,"host":"Fujitsu","metadata":{"management.port":"8093"},"secure":false,"uri":"http://Fujitsu:8093","instanceId":"Fujitsu:custom-app:8091","serviceId":"CUSTOM-APP","instanceInfo":{"instanceId":"Fujitsu:custom-app:8091","app":"CUSTOM-APP","appGroupName":null,"ipAddr":"10.100.102.5","sid":"na","homePageUrl":"http://Fujitsu:8093/","statusPageUrl":"http://Fujitsu:8093/actuator/info","healthCheckUrl":"http://Fujitsu:8093/actuator/health","secureHealthCheckUrl":null,"vipAddress":"custom-app","secureVipAddress":"custom-app","countryId":1,"dataCenterInfo":{"@class":"com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo","name":"MyOwn"},"hostName":"Fujitsu","status":"UP","overriddenStatus":"UNKNOWN","leaseInfo":{"renewalIntervalInSecs":30,"durationInSecs":90,"registrationTimestamp":1575196026084,"lastRenewalTimestamp":1575196235916,"evictionTimestamp":0,"serviceUpTimestamp":1575196025561},"isCoordinatingDiscoveryServer":false,"metadata":{"management.port":"8093"},"lastUpdatedTimestamp":1575196026085,"lastDirtyTimestamp":1575196025406,"actionType":"ADDED","asgName":null},"scheme":null}]} URI is this list is 8093(!) already. It does not work on this port of coarse. But 8092 is mentioned nowhere!

SimonCanJer avatar Dec 01 '19 10:12 SimonCanJer