Ecwid / consul-api

Java client for Consul HTTP API
Apache License 2.0
416 stars 177 forks source link

how to connect a consul cluster? #137

Closed Jaware closed 6 years ago

Jaware commented 6 years ago

the api is as follows: ConsulClient client = new ConsulClient("localhost"); if the "localhost" died,the client will be died too? is there any other way like 1ip:8500,2ip:8500,2ip:8500 to prevent single point failure?

spencergibb commented 6 years ago

The recommended way is to connect to an agent which handles the cluster connection

vgv commented 6 years ago

Of course, you can connect to any consul server in the network on any port:

ConsulClient client = new ConsulClient("consul-server.internal.network", 2313);

However, as @spencergibb says, the general approach is connect to local consul.

vgv commented 6 years ago

Moreover, this is the general way to use consul. You should have the consul agent on every server in your network, so, it isn't hard to connect to 'localhost'

Jaware commented 6 years ago

thanks for your reply.but i use docker,you mean i have to start a consul client(or server?) in my java docker container? @vgv

vgv commented 6 years ago

@Jaware

It depends, but usually (in docker world) people have one container with consul per server. All other containers on this server connect to that one.

vgv commented 6 years ago

@Jaware

For network you have at least two options: 1) You can do some network magic in docker and all your services will see the consul container as localhost. In this case you change nothing, all of your ConsulClient's connect to localhost.

2) You do not make any network bridges between consul container and app containers and just connect to consul agent using its own host/IP from all app containers.

Jaware commented 6 years ago

thanks ,i guess i have to choose the second one,but the pain is that when the host is down,my app becomes down too,it will be very owesome to support cluster config like i said above(1ip:8500,2ip:8500,3ip:8500) or load balancer?

vgv commented 6 years ago

Why do you need this? The host is down => the consul agent on this host is down too => all other servers/services in your cluster know that that host is down and ALL services on this host is down too.

Consul handles this automatically out of the box.

Jaware commented 6 years ago

the node i mean is one of the consul node, may be it is the consul 's design problem....that issue is still open https://github.com/hashicorp/consul/issues/3473