Search code examples
netflix-eurekaspring-cloud-netflix

Get Eureka instance Id


I would like to know if it exist a way for an Eureka client to know it actual instanceId (I am looking for a solution that works both on same host and in distributed conf).

So far, I was using my manually defined eureka.instance.instance-id: ${spring.application.name}:${random.int}, but each time I access it value, I get a new one due to the random. So it will always be different from the one that have been generated and gived to the server at registration time.


Solution

  • Yes, the RandomValuePropertySource is stateless, so you get new value everytime you access that property.

    First, you might want to consider using the host:port as the instanceId, no randomness there.

    If that's not good enough, Inject EurekaInstanceConfigBean and get the instanceId. (Requires some testing)

    Best is to set the instance-id has a system property yourself before SpringApplication.run.