← 返回
未分类 Key

Consul API

Consul HTTP API operations for service discovery, key-value store, health checks, and service mesh management. Use when working with Consul clusters for: (1)...
Consul HTTP API 操作,用于服务发现、键值存储、健康检查和服务网格管理。使用场景包括:(1)…
jonasgao
未分类 clawhub v1.0.0 1 版本 100000 Key: 需要
★ 0
Stars
📥 383
下载
💾 0
安装
1
版本
#latest

概述

Consul API

HashiCorp Consul HTTP API 操作指南。提供服务发现、KV 存储、健康检查和服务网格管理能力。

Quick Start

Base URL: http://127.0.0.1:8500/v1/

认证方式:

# 方式 1: X-Consul-Token header (推荐)
curl -H "X-Consul-Token: <token>" http://127.0.0.1:8500/v1/agent/members

# 方式 2: Bearer token
curl -H "Authorization: Bearer <token>" http://127.0.0.1:8500/v1/agent/members

常用查询参数:

  • dc - 指定数据中心
  • ns - 指定命名空间 (Enterprise)
  • pretty - 格式化 JSON 输出
  • wait= - 阻塞查询等待时间
  • index= - 阻塞查询索引

Service Discovery (Catalog)

列出所有服务

GET /catalog/services
GET /catalog/services?dc=dc1

返回: {"service-name": ["tag1", "tag2"], ...}

查询服务的所有实例

GET /catalog/service/:service_name
GET /catalog/service/redis?dc=dc1&ns=default

列出所有节点

GET /catalog/nodes
GET /catalog/nodes?near=_agent

注册服务

PUT /catalog/register
{
  "Node": "node1",
  "Address": "192.168.1.1",
  "Service": {
    "Service": "redis",
    "ID": "redis-1",
    "Tags": ["primary", "v1"],
    "Address": "127.0.0.1",
    "Port": 6379
  }
}

注销服务

PUT /catalog/deregister
{
  "Node": "node1",
  "ServiceID": "redis-1"
}

Key-Value Store

读取 KV

GET /kv/:key              # 单个 key
GET /kv/:prefix?recurse   # 递归读取前缀下所有 key
GET /kv/:prefix?keys      # 只返回 key 列表
GET /kv/:key?raw          # 返回原始值 (text/plain)

返回值是 base64 编码,需解码:

curl -s http://127.0.0.1:8500/v1/kv/my-key | jq -r '.[0].Value' | base64 -d

写入 KV

PUT /kv/:key
PUT /kv/:key?flags=123    # 附加 flags
PUT /kv/:key?cas=100      # Check-And-Set (乐观锁)

curl -X PUT -d 'my value' http://127.0.0.1:8500/v1/kv/my-key

删除 KV

DELETE /kv/:key           # 删除单个 key
DELETE /kv/:prefix?recurse  # 删除前缀下所有 key
DELETE /kv/:key?cas=100   # Check-And-Set 删除

分布式锁

# 获取锁
PUT /kv/:key?acquire=<session-id>

# 释放锁
PUT /kv/:key?release=<session-id>

Health Checks

查询节点健康状态

GET /health/node/:node

查询服务的健康检查

GET /health/checks/:service
GET /health/checks/redis?passing  # 只返回 passing 状态

查询健康的服务实例

GET /health/service/:service
GET /health/service/redis?passing  # 只返回健康实例

按状态查询

GET /health/state/passing
GET /health/state/warning
GET /health/state/critical
GET /health/state/any

健康状态: passing, warning, critical

ACL (Access Control)

创建 Token

PUT /acl/create
{
  "Name": "my-token",
  "Type": "client",
  "Rules": "key \"secret/\" { policy = \"read\" }"
}

列出 Tokens

GET /acl/list
GET /acl/token/self  # 当前 token 信息

读取/更新 Token

GET /acl/token/:accessor_id
PUT /acl/token/:accessor_id

Agent Operations

查看 Agent 成员

GET /agent/members

查看 Agent 自身信息

GET /agent/self

查看本地服务

GET /agent/services
GET /agent/service/:service_id

注册服务 (Agent 端点)

PUT /agent/service/register
{
  "Name": "redis",
  "ID": "redis-1",
  "Tags": ["primary"],
  "Address": "127.0.0.1",
  "Port": 6379,
  "Check": {
    "HTTP": "http://localhost:6379/health",
    "Interval": "10s"
  }
}

注销服务 (Agent 端点)

PUT /agent/service/deregister/:service_id

Service Mesh (Connect)

查询 Connect 服务

GET /catalog/connect/:service
GET /health/connect/:service?passing

查询 Ingress Gateway

GET /health/ingress/:service

服务意图 (Intentions)

GET /connect/intentions
POST /connect/intention
{
  "SourceName": "web",
  "DestinationName": "db",
  "Action": "allow"
}
DELETE /connect/intention/:id

Sessions (分布式锁)

创建 Session

PUT /session/create
{
  "Name": "my-lock",
  "TTL": "30s",
  "Behavior": "delete"
}

返回: {"ID": "session-uuid"}

查询 Session

GET /session/info/:session-id
GET /session/node/:node

销毁 Session

PUT /session/destroy/:session-id

Transactions

原子操作多个 KV 或 Catalog 操作:

PUT /txn
[
  {"KV": {"Verb": "set", "Key": "key1", "Value": "dGVzdA=="}},
  {"KV": {"Verb": "get", "Key": "key2"}},
  {"Service": {"Verb": "register", "Service": {...}}}
]

支持的 Verb:

  • KV: set, get, delete, check-index, lock, unlock
  • Service: register, deregister

Blocking Queries

使用 indexwait 实现长轮询:

# 等待 key 变化
GET /kv/my-key?index=100
GET /kv/my-key?wait=5m&index=100

响应头 X-Consul-Index 返回当前索引。

Error Handling

常见 HTTP 状态码:

  • 200 - 成功
  • 404 - 资源不存在
  • 400 - 请求格式错误
  • 403 - ACL 权限不足
  • 500 - 服务器错误

响应头:

  • X-Consul-Index - 当前状态索引
  • X-Consul-KnownLeader - 是否有已知 leader
  • X-Consul-LastContact - 最后联系 leader 的时间
  • X-Consul-Default-ACL-Policy - 默认 ACL 策略

References

For detailed API specifications, see:

版本历史

共 1 个版本

  • v1.0.0 当前
    2026-05-07 05:16 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

云效 DevOps MCP

jonasgao
阿里云云效MCP工具,通过MCP SSE协议操作云效平台,支持项目管理、工作项、流水线、代码仓库等165+工具。
★ 0 📥 474

GitLab API Client

jonasgao
通过 GitLab API 管理项目、议题、合并请求、分支、流水线、用户、群组等,用于用户需要执行 GitL 相关操作时。
★ 0 📥 386

Consul CLI

jonasgao
Consul CLI 命令参考,涵盖服务发现、键值存储、健康检查、集群管理及服务网格操作。用于执行 Consul 命令。
★ 0 📥 362