【Consul】Consul实践指导-服务注册(Service)

x33g5p2x  于2021-12-20 转载在 其他  
字(2.4k)|赞(0)|评价(0)|浏览(267)

      服务发现是consul的另一主要功能。Consul Agent提供简单的Service定义格式用于申报服务的可用性,并与健康检查相关联。如果健康检查与服务关联,则认为服务是应用级的。服务可以定义在配置文件中或在运行时通过HTTP接口添加。 

1.1.1  Service Definition

      脚本服务定义示例如下:

{
  "service": {
    "name": "redis",
    "tags": ["master"],
    "address": "127.0.0.1",
    "port": 8000,
    "enableTagOverride": false,
    "checks": [
      {
        "script": "/usr/local/bin/check_redis.py",
        "interval": "10s"
      }
    ]
  }
}

      定义服务必须包含name字段,其他字段id、tags、address、port、check、enableTagOverride是可选字段。如果id字段没有定义,那么会被赋值为name。在当前节点上,服务ID必须是唯一的,name是可以重复的,那么id必须配置。

      Tags字段是增强配置的可读性,consul不使用,可以说明当前节点是master还是slave,及其版本等信息。

      Address字段可以指定一个给定服务的IP。默认情况下,通常使用agent的IP,不是必须的。使用端口port可以使得面向服务的体系架构更容易配置。

      服务与健康检查关联,可以替换失败节点和数据库。健康检查很好地集成了DNS接口,如果服务健康检查失败或者节点存在系统级检查失败。DNS接口将从服务队列中删除该节点。

      Check必须是Script、HTTP、TCP、TTL四种类型中的一种。Script类型需要提供Script脚本和interval变量。HTTP类型必须提供http和Interval字段。TCP类型需要提供tcp和Interval字段,TTL类型秩序提供ttl。Check的name字段是自动通过service:<service-id>生成,如果有多个service,则由service:<service-id>:<num>生成

(详情参见check配置-http://blog.csdn.net/younger_china/article/details/52243759) 

      Service配置是可以重载的,两种方法,1. 发送SIGHUP 信号到Agent;2. 通过HTTP API动态配置。

1.1.2  Multiple Service Definitions

      使用services字段,示例如下:

{
  "services": [
    {
      "id": "red0",
      "name": "redis",
      "tags": [
        "master"
      ],
      "address": "127.0.0.1",
      "port": 6000,
      "checks": [
        {
          "script": "/bin/check_redis-p 6000",
          "interval": "5s",
          "ttl": "20s"
        }
      ]
    },
    {
      "id": "red1",
      "name": "redis",
      "tags": [
        "delayed",
        "slave"
      ],
      "address": "127.0.0.1",
      "port": 7000,
      "checks": [
        {
          "script": "/bin/check_redis-p 7000",
          "interval": "30s",
          "ttl": "60s"
        }
      ]
    },
    ...
  ]
}

1.1.3 Service and Tag Names with DNS

      Consul exposes service definitions and tags over the DNS interface. DNS queries have a strict set of allowed characters anda well-defined format that Consul cannot override. While it is possible toregister services or tags with names that don't match the conventions, thoseservices and tags will not be discoverable via the DNS interface. It isrecommended to always use DNS-compliant service and tag names.

DNS-compliant service and tag names may contain any alpha-numericcharacters, as well as dashes. Dots are not supported because Consul internallyuses them to delimit service tags.

相关文章