Consul註冊服務的方式有兩種:
1.配置文件
2.API接口redis
固然做爲微服務的註冊中心,添加配置文件後,能夠reload配置,不須要停機。 json
查詢已經註冊的服務有三種方式:數組
下面經過例子,演示相關操做。curl
咱們經過配置文件註冊一個服務。
按照約定,把配置文件放在/etc/consul.d中。ide
root@598361668db6:/data# mkdir /etc/consul.d root@598361668db6:/etc/consul.d# echo '{"service":{"name":"student","tags":["student"],"port":80}}'>/etc/consul.d/student.json root@598361668db6:/data# nohup ./consul agent -dev -client 0.0.0.0 -config-dir=/etc/consul.d >consul.log &
咱們看到配置文件是JSON格式的。而且咱們定義了名爲student的服務,同時給服務添加了一個標籤也叫student,端口爲80。
爲了方便快速閱讀,JSON文件完整的格式參照文章結尾處。微服務
root@598361668db6:/data# curl http://localhost:8500/v1/catalog/service/student [ { "ID": "eece8f25-51f9-a62d-ca02-7dacf641716e", "Node": "598361668db6", "Address": "127.0.0.1", "Datacenter": "dc1", "TaggedAddresses": { "lan": "127.0.0.1", "wan": "127.0.0.1" }, "NodeMeta": { "consul-network-segment": "" }, "ServiceID": "student", "ServiceName": "student", "ServiceTags": [ "student" ], "ServiceAddress": "", "ServicePort": 80, "ServiceEnableTagOverride": false, "CreateIndex": 6, "ModifyIndex": 6 } ]
root@598361668db6:/data# curl 'http://localhost:8500/v1/health/service/student?passing' [ { "Node": { "ID": "eece8f25-51f9-a62d-ca02-7dacf641716e", "Node": "598361668db6", "Address": "127.0.0.1", "Datacenter": "dc1", "TaggedAddresses": { "lan": "127.0.0.1", "wan": "127.0.0.1" }, "Meta": { "consul-network-segment": "" }, "CreateIndex": 5, "ModifyIndex": 6 }, "Service": { "ID": "student", "Service": "student", "Tags": [ "student" ], "Address": "", "Port": 80, "EnableTagOverride": false, "CreateIndex": 6, "ModifyIndex": 6 }, "Checks": [ { "Node": "598361668db6", "CheckID": "serfHealth", "Name": "Serf Health Status", "Status": "passing", "Notes": "", "Output": "Agent alive and reachable", "ServiceID": "", "ServiceName": "", "ServiceTags": [], "Definition": {}, "CreateIndex": 5, "ModifyIndex": 5 } ] } ]
consul會在DNS中爲每一個服務添加一條A記錄,名稱爲serviceName.service.consul。若是服務有標籤還會添加一個tag.serviceName.service.consul的記錄。url
root@598361668db6:/data# dig @127.0.0.1 -p 8600 student.service.consul ; <<>> DiG 9.10.3-P4-Ubuntu <<>> @127.0.0.1 -p 8600 student.service.consul ; (1 server found) ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 52319 ;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1 ;; WARNING: recursion requested but not available ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ;student.service.consul. IN A ;; ANSWER SECTION: student.service.consul. 0 IN A 127.0.0.1 ;; Query time: 2 msec ;; SERVER: 127.0.0.1#8600(127.0.0.1) ;; WHEN: Wed Dec 20 12:15:42 UTC 2017 ;; MSG SIZE rcvd: 67 root@598361668db6:/data# dig @127.0.0.1 -p 8600 student.student.service.consul ; <<>> DiG 9.10.3-P4-Ubuntu <<>> @127.0.0.1 -p 8600 student.student.service.consul ; (1 server found) ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 32043 ;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1 ;; WARNING: recursion requested but not available ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ;student.student.service.consul. IN A ;; ANSWER SECTION: student.student.service.consul. 0 IN A 127.0.0.1 ;; Query time: 0 msec ;; SERVER: 127.0.0.1#8600(127.0.0.1) ;; WHEN: Wed Dec 20 12:15:52 UTC 2017 ;; MSG SIZE rcvd: 75
root@598361668db6:/data# ./consul reload Configuration reload triggered
{ "service": { "id": "red0", "name": "redis", "tags": ["primary"], "address": "", "port": 8000, "enable_tag_override": false, "checks": [ { "script": "/usr/local/bin/check_redis.py", "interval": "10s" } ] } }
含義:命令行
名稱 | 類型 | 含義 |
---|---|---|
id | String | 服務的惟一標識 |
name | String | 名稱 |
tags | String數組 | 標籤 |
address | String | 地址,ip或hostname |
port | int | 端口號 |
enable_tag_override | bool | 標籤是否容許覆蓋 |
checks | 數組 | 檢測服務是否可用 |
關於ckecks會在下文中具體介紹。code