在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):hamishforbes/lua-resty-consul开源软件地址(OpenSource Url):https://github.com/hamishforbes/lua-resty-consul开源编程语言(OpenSource Language):Perl 78.4%开源软件介绍(OpenSource Introduction):lua-resty-consulLibrary to interface with the consul HTTP API from ngx_lua Table of ContentsOverviewMethods all return a lua-resty-http response object. All response headers are available at The ACL Token parameter is always sent as the If 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 DependenciesBasic Methodsnew
Create a new consul client.
get
Performs a GET request to the provided path. API Version is automatically prepended.
Returns a lua-resty-http response object. put
Performs a PUT request to the provided path. API Version is automatically prepended.
If Returns a lua-resty-http response object. delete
Performs a GET request to the provided path. API Version is automatically prepended.
Returns a lua-resty-http response object. get_client_body_readerProxy method to lua-resty-http Key Value HelpersThese methods automatically prepend get_key
Retrieve a Consul KV key. Values are Base64 decoded.
Returns a lua-resty-http response object. put_key
Create or update a KV key.
If Returns a lua-resty-http response object. delete
Delete a KV entry.
Returns a lua-resty-http response object. list_keys
Retrieve all the keys in the KV strore. Optionally within a
Returns a lua-resty-http response object. Transaction Helpertxn
Performs a
Returns a lua-resty-http response object. KV values in the response body are automatically base64 decoded. 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" |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论