Register multiple instances of Spring Boot Eureka Client from a single host

UPDATE

The README in this repo has been updated to demonstrate the solution in the accepted answer.


I am working with a simple example of registering and opening Boot Eureka boot service based on this guide .

If I run one instance of the client, it registers correctly, and it can see itself through DiscoveryClient . If I run the second instance with a different name, it also works.

But if I run two instances with the same name, only one instance is displayed on the dashboard, and DiscoveryClient shows only the second instance.

When I kill the second instance, the first one is displayed again through the control panel and the discovery client.

Here are a few details about the steps I take and what I see:

Eureka Server

Start the server

 cd eureka-server mvn spring-boot:run 

Visit the Eureka dashboard at http: // localhost: 8761

Please note that there are no β€œinstances” registered yet.

Eureka Client

Start the client

 cd eureka-client mvn spring-boot:run 

Visit the client directly at http: // localhost: 8080 /

The endpoint /whoami will show the client self-identification of the name of its application and port

 { "springApplicationName":"eureka-client", "serverPort":"8080" } 

The /instances endpoint will take up to a minute to update, but should ultimately show all eureka-client instances that have been registered with the Eureka Discovery Client.

 [ { "host":"hostname", "port":8080, "serviceId":"EUREKA-CLIENT", "uri":"http://hostname:8080", "secure":false } ] 

Now you can visit Eureka dashoboard again and see it there.

Move another client with a different name

You can see that another client will be registered by following these steps:

 cd eureka-client mvn spring-boot:run -Dspring.application.name=foo -Dserver.port=8081 

The endpoint /whoami will display the name foo and port 8081 .

After a minute or so, the endpoint /instances will display information about this foo instance.

Two clients will now be registered on the Eureka dashboard.

Hide another client with the same name

Now try turning on another instance of eureka-client just by moving the port parameter:

 cd eureka-client mvn spring-boot:run -Dserver.port=8082 

The /whoami endpoint for http://localhost:8082 shows what we expect.

After a minute or so, the /instances endpoint now also shows an instance running on port 8082, but for some reason it does not show an instance running on port 8080.

And if we check the endpoint /instances at http://localhost:8080 , now we will see only the instance running on 8082 (although, obviously, the one that runs on 8080 since we ask.

The Eureka toolbar shows only 1 instance of eureka-client .

What's going on here?

Try to kill the instance running on 8082 and see what happens.

When we request /instances at 8080, it still shows only an instance at 8082.

But in a minute it will disappear, and we will again see an instance at 8080.

The question is, why do not we see both eureka-client instances when they both work?

+7
java spring spring-boot netflix-eureka
source share
2 answers

For on-premises deployments, try setting the {namespace} .instanceId property in the eureka-client.properties file (or eureka.instance.metadataMap.instanceId for the corresponding yaml file in the case of Spring based cloud settings). It is deeply rooted in how the Eureka server computes application lists and compares InstanceInfo for PeerAwareInstanceRegistryImpl - when more specific data (such as instance metadata is available), they try to get the identifier on behalf of the host.

I would not recommend it for deploying AWS, but causing clutter with instanceId will not give you the trouble of figuring out which machine has a particular service. On the other hand, I doubt that two identical services will be hosted on the same machine, right?

+7
source share

So that all instances are displayed on the admin portal by setting the unique euraka.instance.hostname file in the Eureka configuration file.

The host name is used as the key to store the InstanceInfo instance in com.netflix.discovery.shared.Application (because UniqueIdentifier is not set). Therefore, you need to use unique hostnames. When you test the tape in this scenario, you will see that the load will not be balanced.

The following is an example application.yml:

 server: port: ${PORT:0} info: component: example.server logging: level: com.netflix.discovery: 'OFF' org.springframework.cloud: 'DEBUG' eureka: instance: leaseRenewalIntervalInSeconds: 1 leaseExpirationDurationInSeconds: 1 metadataMap: instanceId: ${spring.application.name}:${spring.application.instance_id:${random.value}} instanceId: ${spring.application.name}:${spring.application.instance_id:${random.value}} 

This is a bug in Eureka, you can check additional information at https://github.com/codecentric/spring-boot-admin/issues/134

+3
source share

All Articles