how to connect a consul cluster?
Closed this issue · 9 comments
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?
The recommended way is to connect to an agent which handles the cluster connection
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.
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'
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
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.
For network you have at least two options:
-
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.
-
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.
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?
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.
the node i mean is one of the consul node, may be it is the consul 's design problem....that issue is still open hashicorp/consul#3473