假设当前服务器的ip是:192.168.56.130
1、允许 所有ip 注册(验证可行)
consul agent -server -ui -bootstrap-expect=1 -data-dir=/usr/local/consul -node=devmaster -advertise=192.168.56.130 -bind=0.0.0.0 -client=0.0.0.0
2、只允许 当前ip 注册
consul agent -server -ui -bootstrap-expect=1 -data-dir=/usr/local/consul -node=devmaster -advertise=192.168.56.130 -bind=192.168.56.130 -client=127.0.0.1
此时,若别的ip的项目想要连接这个Consul,那么启动就会报错: com.ecwid.consul.transport.TransportException:org.apache.http.conn.HttpHostConnectException: Connect to 192.168.56.130:8500 [/192.168.56.130] failed: Connection refused: connect
注意:
(1)-bind要用完整ip地址,如果使用127.0.0.1,否则虽然consul能启动,但是consul日志会不停地打印如下信息
2018/12/17 15:56:14 [WARN] consul: error getting server health from "devmaster": context deadline exceeded
2018/12/17 15:56:15 [WARN] consul: error getting server health from "devmaster": rpc error getting client: failed to get conn: dial tcp 127.0.0.1:0->192.168.56.130:8300: connect: connection refused
2018/12/17 15:56:16 [WARN] consul: error getting server health from "devmaster": context deadline exceeded
2018/12/17 15:56:17 [WARN] consul: error getting server health from "devmaster": rpc error getting client: failed to get conn: dial tcp 127.0.0.1:0->192.168.56.130:8300: connect: connection refused
(2)-client 需要用127.0.0.1,如果用真实的ip地址,consul都启动不起来
(3)consul agent -dev 也是只允许当前ip注册,但这个是本地开发时用的,正式服务器往往不用
(4)项目中的配置文件也要相应的更改,使用127.0.0.1 ,而不是真实ip地址。否则,项目在服务器端也启动不起来了,例如:
spring:
cloud:
consul:
host: 127.0.0.1
port: 8500