服务注册方式
Consul注册服务的方式有两种:
1.配置文件
2.API接口
当然作为微服务的注册中心,添加配置文件后,可以reload配置,不需要停机。
查询服务
查询已经注册的服务有三种方式:
- API查询
- 命令行查询
- DNS查询
例子
下面通过例子,演示相关操作。
定义服务
我们通过配置文件注册一个服务。
按照约定,把配置文件放在/etc/consul.d中。
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文件完整的格式参照文章结尾处。
查询服务
界面查看
API查询
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
}
]
API查询可用服务
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
}
]
}
]
DNS查询
consul会在DNS中为每个服务添加一条A记录,名称为serviceName.service.consul。如果服务有标签还会添加一个tag.serviceName.service.consul的记录。
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
附录
JSON文件完整格式
{
"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会在下文中具体介绍。
转载于:https://blog.51cto.com/4436396/2052822