lua-resty-consul

与 Consul HTTP API 交互的库

$ opm get hamishforbes/lua-resty-consul

lua-resty-consul

从 ngx_lua 与 Consul HTTP API 交互的库

概述

所有方法都返回一个 lua-resty-http 响应对象。响应主体已读取并设置为 res.body,如果响应的 Content-Type 头为 Application/JSON,则进行 JSON 解码。

所有响应头都可以在 res.headers 中找到。

ACL Token 参数始终作为 X-Consul-Token 头发送,而不是包含在查询字符串中。

如果提供了 waitindex 参数,则请求读取超时将相应延长。wait 必须以秒为单位传递,不要包含 s 或任何其他单位字符串。

    local resty_consul = require('resty.consul')
    local consul = resty_consul:new({
            host            = "127.0.0.1",
            port            = 8500,
            connect_timeout = (60*1000), -- 60s
            read_timeout    = (60*1000), -- 60s
            default_args    = {
                token = "my-default-token"
            },
            ssl             = false,
            ssl_verify      = true,
            sni_host        = nil,
        })
    
    local res, err = consul:get('/agent/services')
    if not res then
        ngx.log(ngx.ERR, err)
        return
    end
    
    ngx.print(res.status) -- 200
    local services = res.body -- JSON decoded response
    
    
    local res, err = consul:put('/agent/service/register', my_service_definition, { token = "override-token" })
    if not res then
        ngx.log(ngx.ERR, err)
        return
    end
    
    ngx.print(res.status) -- 200
    ngx.print(res.headers["X-Consul-Knownleader"]) -- "true"
    local service_register_response = res.body -- JSON decoded response
    
    
    local res, err = consul:list_keys() -- Get all keys
    if not res then
        ngx.log(ngx.ERR, err)
        return
    end
    
    local keys = {}
    if res.status == 200 then
        keys = res.body
    end
    
    for _, key in ipairs(keys) do
        local res, err = consul:get_key(key)
        if not res then
            ngx.log(ngx.ERR, err)
            return
        end
    
        ngx.print(res.body[1].Value) -- Key value after base64 decoding
    end

依赖项

基本方法

new

语法:client = consul:new(opts?)

创建一个新的 Consul 客户端。opts 是一个设置以下选项的表

  • host 默认值为 127.0.0.1

  • port 默认值为 8500。如果使用 Unix 套接字作为 host,则设置为 0

  • connect_timeout 连接超时时间(毫秒)。默认为 60 秒

  • read_timeout 读取超时时间(毫秒)。默认为 60 秒

  • default_args 要与所有请求一起发送的查询字符串参数表(例如 token)。默认为空

  • ssl 布尔值,启用 HTTPS 请求。默认为 false

  • ssl_verify 布尔值,验证 SSL 证书。默认为 true = true,

  • sni_host 验证 SSL 证书时使用的主机名。

get

语法:res, err = consul:get(path, args?)

对提供的路径执行 GET 请求。API 版本会自动添加前缀。

args 是一个要添加到 URI 的查询字符串参数表。

返回一个 lua-resty-http 响应对象。出错时返回 nil 和错误消息。

put

语法:res, err = consul:put(path, body, args?)

对提供的路径执行 PUT 请求。API 版本会自动添加前缀。

args 是要添加到 URI 的查询字符串参数表。

如果 body 是一个表或布尔值,则会在发送之前自动进行 JSON 编码。否则,lua-resty-http 接受的任何主体输入都是有效的。

返回一个 lua-resty-http 响应对象。出错时返回 nil 和错误消息。

delete

语法:res, err = consul:delete(path, args?)

对提供的路径执行 GET 请求。API 版本会自动添加前缀。

args 是一个要添加到 URI 的查询字符串参数表。

返回一个 lua-resty-http 响应对象。出错时返回 nil 和错误消息。

get_client_body_reader

代理方法到 lua-resty-http

键值助手

这些方法会自动添加前缀 /v1/kv,只需要传递实际的键即可。Base64 编码的值会自动解码。

get_key

语法:res, err = consul:get_key(key, args?)

检索 Consul KV 键。值会被 Base64 解码。

args 是一个要添加到 URI 的查询字符串参数表。

返回一个 lua-resty-http 响应对象。出错时返回 nil 和错误消息。

put_key

语法:res, err = consul:put_key(key, value, args?)

创建或更新 KV 键。

args 是要添加到 URI 的查询字符串参数表。

如果 value 是一个表或布尔值,则会在发送之前自动进行 JSON 编码。否则,lua-resty-http 接受的任何主体输入都是有效的。

返回一个 lua-resty-http 响应对象。出错时返回 nil 和错误消息。

delete

语法:res, err = consul:delete_key(key, args?)

删除 KV 条目。

args 是一个要添加到 URI 的查询字符串参数表。

返回一个 lua-resty-http 响应对象。出错时返回 nil 和错误消息。

list_keys

语法:res, err = consul:list_keys(prefix?, args?)

检索 KV 存储中的所有键。可以选择在 prefix 内检索。

args 是一个要添加到 URI 的查询字符串参数表。使用此方法时,keys 始终设置为查询字符串参数

返回一个 lua-resty-http 响应对象。出错时返回 nil 和错误消息。

事务助手

txn

语法:res, err = consul:txn(payload, args?)

/v1/txn API 端点执行 PUT 请求,并使用提供的有效负载。

payload 可以作为 Lua 表提供,在这种情况下,Value 键将自动进行 Base64 编码。否则,lua-resty-http 接受的任何主体输入都是有效的。

返回一个 lua-resty-http 响应对象。出错时返回 nil 和错误消息。

响应主体中的 KV 值会自动进行 Base64 解码。

    local txn_payload = {
        {
            KV = {
                Verb   = "set",
                Key    = "foo",
                Value  = "bar",
            }
        },
        {
            KV = {
                Verb   = "get",
                Key    = "foobar",
            }
        }
    }
    
    local consul = resty_consul:new()
    
    local res, err = consul:txn(txn_payload)
    if not res then
        ngx.say(err)
        return
    end
    
    ngx.say(res.body.Results[2].KV.Value) -- "bar"

作者

Hamish Forbes

许可证

mit

依赖项

版本