Netflix Eureka and 2 instances of application on local environment
Asked Answered
I

3

4

I'm getting started with Netflix Eureka and using its 1.1.145 (https://github.com/Netflix/eureka/tree/1.1.145) version.

I want to start locally 2 instances of the same application on different ports and have them both registered with Eureka. I'm using sample service (https://github.com/Netflix/eureka/blob/1.1.145/eureka-server/conf/sampleservice/sample-eureka-service.properties)

So I start Eureka itself and 2 instances using above config - one app on 8001 port and another on 8002.

For some reason I'm getting only one instance registered with Eureka at any given time. Both of them start without exceptions and can talk to Eureka OK. When I start a second instance, it seems to simply overwrite info about 1st instance with its own info.

What I want is to have 2 'instance' elements under the same logical eureka.name at http://localhost/eureka/v2/apps

What am I missing?

Intussusception answered 15/4, 2015 at 14:47 Comment(0)
C
7

The default instance ID is the host name, so to run two of anything on the same host you need to manually set the eureka.instance.metadataMap.instanceId (that works in a Spring Cloud app anyway).

Chlorohydrin answered 11/5, 2015 at 7:23 Comment(0)
R
4

I'm using spring-boot-starter-parent 1.5.1.RELEASE but

eureka.instance.metadataMap.instanceId 

does not work, so I found a solution for my app

first, set server.port to zero:

server:
  port: 0   # HTTP (Tomcat) port

then set eureka.instance.instanceId, to something random. I used integer randoms:

eureka:
  instance:
    instanceId: ${spring.application.name}:${random.int}

if you prefer a long random, you can use random.value like this:

eureka:
  instance:
    instanceId: ${spring.application.name}:${random.value}
Relentless answered 22/3, 2017 at 20:4 Comment(0)
T
0

When you want several services instances on the same host, you explicitly specify their instanceId when you run them like this:

    mvn spring-boot:run -Dserver.port=8081 -Deureka.instance.metadataMap.instanceId=instance1

    mvn spring-boot:run -Dserver.port=8082 -Deureka.instance.metadataMap.instanceId=instance2
 ...

Or

   java -jar target/app.jar -Dserver.port=8081 -Deureka.instance.metadataMap.instanceId=instance1
...

You can also make it dynamically by specify this in your application properties file :

eureka.instance.instanceId: applicationname:${spring.application.instance_id:${random.value}}

And I am not sure if it is linked, but unregistration with eureka went realy long when I shut down instances (they may even never unregister), so I had to toggle of the self-preservation mode :

eureka.server.enable-self-preservation to false

Taggart answered 10/5, 2016 at 14:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.