Netflix Eureka and 2 instances of application on local environment

余生颓废 提交于 2019-12-03 20:37:12

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).

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}

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!