(1)注册服务到consul
在配置文件路径中,D:\consul\consul1.10.10,构建web.json 文件。
(2)重新启动consul
linux
consul agent -server -bootstrap-expect 1 -data-dir /tmp/consul -node=n1 -bind=192.168.1.4 -ui -rejoin -config-dir=/etc/consul.d/ -client 0.0.0.0
windows
consul agent -server -bootstrap-expect 1 -data-dir /tmp/consul -node=n1 -bind=192.168.1.4 -ui -rejoin -config-dir=D:\consul/ -client 0.0.0.0
(3)查询服务
方法一:
在google网页上 输入192.168.*.* ,显示consul页面
方法二:
终端命令查看
我们可以通过DNS或者HTTP的API来查询服务,这里我们通过HTTP来查询服务,
在终端输入:
curl -s 127.0.0.1:8500/v1/catalog/service/bj38
得到一串json数据,
[{"ID":"59e895cb-07e8-40a3-7716-fccf1cd18ad2","Node":"n1","Address":"192.168.1.4","Datacenter":"dc1","TaggedAddresses":{"lan":"192.168.1.4","lan_ipv4":"192.168.1.4","wan":"192.168.1.4","wan_ipv4":"192.168.1.4"},"NodeMeta":{"consul-network-segment":""},"ServiceKind":"","ServiceID":"bj38","ServiceName":"bj38","ServiceTags":["itcast","itheima"],"ServiceAddress":"","ServiceWeights":{"Passing":1,"Warning":1},"ServiceMeta":{},"ServicePort":8800,"ServiceSocketPath":"","ServiceEnableTagOverride":false,"ServiceProxy":{"Mode":"","MeshGateway":{},"Expose":{}},"ServiceConnect":{},"CreateIndex":380,"ModifyIndex":380}]
上面数据可以到json网页上显示
(4)consul健康检查
健康检查是服务发现的关键组件。预防使用到不健康的服务。