Spring-cloud-netflix: Eureka registration prefer hostname instead of IP

Created on 30 Jun 2017  路  12Comments  路  Source: spring-cloud/spring-cloud-netflix

Is there a way for eureka to register via hostname instead of IP, I have tried setting eureka.instance.preferIpAddress to false but it takes hostname some times and IP address sometime. Related stackoverflow Question

Using hostname is useful when running micro services with docker swarm, as docker-id (which is same as hostname of the container) of the container is resolvable to all the containers (even if the containers are on different nodes/VM's)

I am aware of setting preferIpAddress as true and ignoring networks but I am looking for a straight forward solution.

Thanks in Advance.

question

Most helpful comment

If the hostname is available as an env var (say THE_HOST) you can do eureka.instance.hostname=${THE_HOST}.

All 12 comments

Could you provide your configuration?

Eureka

eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://discovery-eureka:8761/eureka/

client

eureka:
  client:
    healthcheck:
      enabled: true
    serviceUrl:
      defaultZone: http://discovery-eureka:8761/eureka/
  instance:
    preferIpAddress: false

Here discovery-eureka is the docker service name (In docker swarm you can hit the containers using the service name) and we have only one instance (container) of eureka running.

We try and guess. What we get comes from java's NetworkInterfaces class see here. In some cases, when the hostname can't be determined by java, the IP is returned as the hostname. The only sure way to ensure the hostname is to set the hostname in eureka.instance.hostname. That eliminates any guessing.

If the hostname is available as an env var (say THE_HOST) you can do eureka.instance.hostname=${THE_HOST}.

Thank you! @spencergibb for mentioning setting the environment variable. By setting the environment variable it's working exactly as I needed.

I am setting the environment variable at the run time of the container using a shell script as the ENTRYPOINT in Dockerfile, the shell script sets the hostname environment variable and starts the jar.

@dhanvi
I have found that

setting

     deploy:
        endpoint_mode: dnsrr

in the docker-compose make the client register with hostname. This worked independent of any eureka setting in the application configuration.

When scaling this works with dynamically generated hostname from swarm.

@saadlu yep seen your solution before, I have also updated my answer https://stackoverflow.com/a/45233417/3514300 to reflect the same.

Hey,

seems setting eureka.instance.hostname=mydomain.com is not working for me, both my server and client service running on two different docker containers in side two different machines on aws with different public IP. Any help is much appreciated.

@msalmanST what networking are you using ? did you try host networking type network ? https://docs.docker.com/network/host/

@dhanvi yes, I am using network_mode host for container networking.

I take more time for this but not resolved now, I have uaa, registry in server IP 192.168.1.2 and micro service in 192.168.1.3. When I go to registry see Application Instances and click to micro service and working but when we login received message: "detail": "No instances available for 192.168.1.2

@phongphuphang0107 please ask on stack overflow or gitter.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

niral22 picture niral22  路  4Comments

jabrena picture jabrena  路  4Comments

huhuhong picture huhuhong  路  4Comments

mekhaba picture mekhaba  路  4Comments

jjparsons picture jjparsons  路  3Comments