consul api

  1. kv - Key/Value存储
  2. agent - Agent控制,一般用来服务注册和检查注册
  3. catalog - 管理nodes和services
  4. health - 管理健康监测
  5. session - Session操作
  6. acl - ACL创建和管理
  7. event - 用户Events
  8. status - Consul系统状态

开发过程中主要使用前四个

API接口详解:API接口详解

健康检查类

  1. HTTP——consul注册中心向服务定期检测url情况
  2. TCP——consul注册中心向服务定期检测tcp套接字连接情况
  3. TTL——服务主动向consul报告自己的健康状况

这三个是主要的健康检查方式,这里使用TCP作为健康检查。

健康检查详细资料:健康检测详细资料

服务注册

  1. 服务注册

    PUT:[protocol]://[ip]:[port]/[ver]/agent/service/register

    body:

    {
       "id": "service_a:1.1.1.1:6005",
       "name":"service_a",
       "address":"10.227.4.192",
       "port": 6005,
       "check":{
           "tcp": "10.227.4.192",
           "interval": "1s",
           "DeregisterCriticalServiceAfter":"1m"
       }
    }

    body解释:

    id: 服务唯一标识符

    name: 服务名

    address: 服务地址

    port: 服务端口

    check: 【可选】健康检查,其中tcp表示使用tcp的方式, interval表示间隔多久发一次检查包,DeregisterCriticalServiceAfter表示不健康多久就

服务发现

  1. 首先查询datacenter

    GET:[protocol]://[ip]:[port]/[ver]/catalog/datacenters

    无需传body

    返回的消息体如下:

    ["datacenter_a"]
  2. 通过数据中心查找对应服务,假设服务为service_a

    GET:[protocol]://[ip]:[port]/[ver]/catalog/services

    body:

    {"dc": "datacenter_a"}

    返回的重要消息头:

    X-Consul-Index: 1517405

    返回的消息体如下:

    {"service_a":[],"service_b":[],"service_c":[],"service_d":[],"service_e":[]}
  3. 获取数据中心所有的检查项的index,结果不进行保存,仅仅获取index,因为服务监控的时候需要使用该X-Consul-Index进行查询

    GET:[protocol]://[ip]:[port]/[ver]/health/state/[state]?dc=datacenter_a

    其中,state: any, unknown, passing, warning, critical,这里用any

    无需传body

    返回的重要消息头:

    X-Consul-Index: 1517405
  4. 筛选包含service_a的有效服务

    [protocol]://[ip]:[port]/[ver]/health/service/service_a

    body:

    {"dc": "datacenter_a"}

    返回的消息体如下:

    [{"Node":{"ID":"bc3ce288-1e3f-8611-7563-e7bc08ffc97c","Node":"service_a_node","Address":"10.227.4.192","Datacenter":"datacenter_a","TaggedAddresses":{"lan":"10.227.4.192","lan_ipv4":"10.227.4.192","wan":"10.227.4.192","wan_ipv4":"10.227.4.192"},"Meta":{"consul-network-segment":""},"CreateIndex":453198,"ModifyIndex":453198},"Service":{"ID":"service_a:10.227.4.192:6005","Service":"service_a","Tags":[],"Address":"10.227.4.192","TaggedAddresses":{"lan_ipv4":{"Address":"10.227.4.192","Port":6005},"wan_ipv4":{"Address":"10.227.4.192","Port":6005}},"Meta":null,"Port":6005,"Weights":{"Passing":1,"Warning":1},"EnableTagOverride":false,"Proxy":{"Mode":"","MeshGateway":{},"Expose":{}},"Connect":{},"CreateIndex":1052690,"ModifyIndex":1052690},"Checks":[{"Node":"service_a_node","CheckID":"serfHealth","Name":"Serf Health Status","Status":"passing","Notes":"","Output":"Agent alive and reachable","ServiceID":"","ServiceName":"","ServiceTags":[],"Type":"","Interval":"","Timeout":"","ExposedPort":0,"Definition":{},"CreateIndex":453199,"ModifyIndex":1122951},{"Node":"service_a_node","CheckID":"service:service_a:10.227.4.192:6005","Name":"Service 'service_a' check","Status":"passing","Notes":"","Output":"TCP connect 10.227.4.192:6005: Success","ServiceID":"service_a:10.227.4.192:6005","ServiceName":"service_a","ServiceTags":[],"Type":"tcp","Interval":"1s","Timeout":"","ExposedPort":0,"Definition":{},"CreateIndex":1052690,"ModifyIndex":1052691}]}]

    返回格式是一个列表,每个表项都是一个服务,每个服务包含三个数据:NodeChecksService

    checks需要大于1才能视为service存活存活,第一个check为agent 存活的检查。

    Service中的重要参数:ID Port Address,通过参数我们可以拿到已注册的存活的服务的ip和端口

  5. 查找包含service_a的服务

    GET:[protocol]://[ip]:[port]/[ver]/catalog/service/service_a

    返回的body如下:

    [{"ID":"1e7df31b-feac-2e6b-011d-84189b2cec0e","Node":"service_a_node","Address":"10.227.4.192","Datacenter":"datacenter_a","TaggedAddresses":{"lan":"10.227.4.192","lan_ipv4":"10.227.4.192","wan":"10.227.4.192","wan_ipv4":"10.227.4.192"},"NodeMeta":{"consul-network-segment":""},"ServiceKind":"","ServiceID":"service_a:10.227.4.192:6005","ServiceName":"service_a","ServiceTags":[],"ServiceAddress":"10.227.4.192","ServiceTaggedAddresses":{"lan_ipv4":{"Address":"10.227.4.192","Port":8005},"wan_ipv4":{"Address":"10.227.4.192","Port":6005}},"ServiceWeights":{"Passing":1,"Warning":1},"ServiceMeta":{},"ServicePort":6005,"ServiceSocketPath":"","ServiceEnableTagOverride":false,"ServiceProxy":{"Mode":"","MeshGateway":{},"Expose":{}},"ServiceConnect":{},"CreateIndex":1573147,"ModifyIndex":1573147}]

服务监控

  1. 获取数据中心所有的检查项的index,结果不进行保存,仅仅获取index,因为服务监控的时候需要使用该X-Consul-Index进行查询

    GET:[protocol]://[ip]:[port]/[ver]/health/state/[state]?dc=datacenter_a&index=1517405&wait=10s

    其中,state: any, unknown, passing, warning, critical,这里用any

    dc: 数据中心

    wait: 最长等待时间

    无需传body

    返回的重要消息头:

    X-Consul-Index: 1517405

    如果返回的X-Consul-Index值和之前拿到的X-Consul-Index值相同,则表明服务没有发生变化,不用进行服务发现,

Categories:

Tags:

还没发表评论,快来发表第一个评论吧~

发表回复