consul_kv
Thanks to @fatman-x guy, who developed this module, called , and its worker process data flow is below:
Add following configuration in conf/config.yaml
:
And you can config it in short by default value:
discovery:
consul_kv:
servers:
- "http://127.0.0.1:8500"
The keepalive
has two optional values:
true
, default and recommend value, use the long pull way to query consul serversfalse
, not recommend, it would use the short pull way to query consul servers, then you can set thefetch_interval
for fetch interval
Dump Data
http_access_phase(): failed to set upstream: no valid upstream node
So, we import the dump
function for consul_kv
module. When reload, would load the dump file before from consul; when the registered nodes in consul been updated, would dump the upstream nodes into file automatically.
The dump
has three optional values now:
path
, the dump file save path- support relative path, eg:
logs/consul_kv.dump
- support absolute path, eg:
/tmp/consul_kv.bin
- make sure the dump file’s parent path exist
- make sure the
apisix
has the dump file’s read-write access permission,eg:chown www:root conf/upstream.d/
- support relative path, eg:
load_on_init
, default value istrue
- if
true
, just try to load the data from the dump file before loading data from consul when starting, does not care the dump file exists or not - if
false
, ignore loading data from the dump file - Whether
true
orfalse
, we don’t need to prepare a dump file for apisix at anytime
- if
expire
, unit sec, avoiding load expired dump data when load- default
0
, it is unexpired forever - recommend 2592000, which is 30 days(equals 3600 * 24 * 30)
- default
Service register Key&Value template:
The register consul key use upstreams
as prefix by default. The http api service name called webpages
for example, and you can also use webpages/oneteam/hello
as service name. The api instance of node’s ip and port make up new key: <IP>:<Port>
.
Now, register nodes into consul:
curl \
-X PUT \
-d ' {"weight": 1, "max_fails": 2, "fail_timeout": 1}' \
http://127.0.0.1:8500/v1/kv/upstreams/webpages/172.19.5.12:8000
curl \
-X PUT \
-d ' {"weight": 1, "max_fails": 2, "fail_timeout": 1}' \
http://127.0.0.1:8500/v1/kv/upstreams/webpages/172.19.5.13:8000
Here is an example of routing a request with a URL of “/*“ to a service which named “http://127.0.0.1:8500/v1/kv/upstreams/webpages/“ and use consul_kv discovery client in the registry :
$ curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '
{
"uri": "/*",
"service_name": "http://127.0.0.1:8500/v1/kv/upstreams/webpages/",
"discovery_type": "consul_kv"
}
}'
The format response as below:
You could find more usage in the apisix/t/discovery/consul_kv.t
file.
It also offers control api for debugging.
GET /v1/discovery/consul_kv/dump
For example:
# curl http://127.0.0.1:9090/v1/discovery/consul_kv/dump | jq
{
"config": {
"fetch_interval": 3,
"timeout": {
"wait": 60,
"connect": 6000,
"read": 6000
},
"prefix": "upstreams",
"weight": 1,
"servers": [
"http://172.19.5.30:8500",
"http://172.19.5.31:8500"
],
"keepalive": true,
"default_service": {
"host": "172.19.5.11",
"port": 8899,
"metadata": {
"fail_timeout": 1,
"weight": 1,
"max_fails": 1
}
},
"skip_keys": [
"upstreams/myapi/gateway/apisix/"
]
},
"services": {
"http://172.19.5.31:8500/v1/kv/upstreams/webpages/": [
{
"host": "127.0.0.1",
"port": 30513,
"weight": 1
},
{
"weight": 1
}
],
"http://172.19.5.30:8500/v1/kv/upstreams/1614480/grpc/": [
{
"host": "172.19.5.51",
"port": 50051,
"weight": 1
}
],
"http://172.19.5.30:8500/v1/kv/upstreams/webpages/": [
{
"host": "127.0.0.1",
"port": 30511,
"weight": 1
},
{
"host": "127.0.0.1",
"port": 30512,
"weight": 1
}
]
}
}
For example:
curl http://127.0.0.1:9090/v1/discovery/consul_kv/show_dump_file | jq
{
"services": {
"http://172.19.5.31:8500/v1/kv/upstreams/1614480/webpages/": [
{
"host": "172.19.5.12",
"port": 8000,
"weight": 120
},
{
"host": "172.19.5.13",
"port": 8000,
"weight": 120
}
]
},
"expire": 0,
"last_update": 1615877468