Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eureka registration prefer hostname instead of IP #2084

Closed
dhanvi opened this issue Jun 30, 2017 · 12 comments
Closed

Eureka registration prefer hostname instead of IP #2084

dhanvi opened this issue Jun 30, 2017 · 12 comments
Labels

Comments

@dhanvi
Copy link
Contributor

dhanvi commented Jun 30, 2017

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.

@ryanjbaxter
Copy link
Contributor

Could you provide your configuration?

@dhanvi
Copy link
Contributor Author

dhanvi commented Jun 30, 2017

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.

@spencergibb
Copy link
Member

spencergibb commented Jun 30, 2017

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.

@spencergibb
Copy link
Member

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

@dhanvi
Copy link
Contributor Author

dhanvi commented Jul 5, 2017

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.

@saadlu
Copy link

saadlu commented Dec 18, 2018

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

@dhanvi
Copy link
Contributor Author

dhanvi commented Dec 22, 2018

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

@msalmanST
Copy link

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.

@dhanvi
Copy link
Contributor Author

dhanvi commented Mar 12, 2019

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

@msalmanST
Copy link

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

@phongphuphang0107
Copy link

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

@spencergibb
Copy link
Member

@phongphuphang0107 please ask on stack overflow or gitter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants