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:

    1. discovery:
    2. consul_kv:
    3. servers:
    4. - "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 servers
    • false, not recommend, it would use the short pull way to query consul servers, then you can set the fetch_interval for fetch interval

    Dump Data

    1. 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/
    • load_on_init, default value is true
      • 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 or false, we don’t need to prepare a dump file for apisix at anytime
    • 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)

    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:

    1. curl \
    2. -X PUT \
    3. -d ' {"weight": 1, "max_fails": 2, "fail_timeout": 1}' \
    4. http://127.0.0.1:8500/v1/kv/upstreams/webpages/172.19.5.12:8000
    5. curl \
    6. -X PUT \
    7. -d ' {"weight": 1, "max_fails": 2, "fail_timeout": 1}' \
    8. 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 :

    1. $ curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '
    2. {
    3. "uri": "/*",
    4. "service_name": "http://127.0.0.1:8500/v1/kv/upstreams/webpages/",
    5. "discovery_type": "consul_kv"
    6. }
    7. }'

    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.

    1. GET /v1/discovery/consul_kv/dump

    For example:

    1. # curl http://127.0.0.1:9090/v1/discovery/consul_kv/dump | jq
    2. {
    3. "config": {
    4. "fetch_interval": 3,
    5. "timeout": {
    6. "wait": 60,
    7. "connect": 6000,
    8. "read": 6000
    9. },
    10. "prefix": "upstreams",
    11. "weight": 1,
    12. "servers": [
    13. "http://172.19.5.30:8500",
    14. "http://172.19.5.31:8500"
    15. ],
    16. "keepalive": true,
    17. "default_service": {
    18. "host": "172.19.5.11",
    19. "port": 8899,
    20. "metadata": {
    21. "fail_timeout": 1,
    22. "weight": 1,
    23. "max_fails": 1
    24. }
    25. },
    26. "skip_keys": [
    27. "upstreams/myapi/gateway/apisix/"
    28. ]
    29. },
    30. "services": {
    31. "http://172.19.5.31:8500/v1/kv/upstreams/webpages/": [
    32. {
    33. "host": "127.0.0.1",
    34. "port": 30513,
    35. "weight": 1
    36. },
    37. {
    38. "weight": 1
    39. }
    40. ],
    41. "http://172.19.5.30:8500/v1/kv/upstreams/1614480/grpc/": [
    42. {
    43. "host": "172.19.5.51",
    44. "port": 50051,
    45. "weight": 1
    46. }
    47. ],
    48. "http://172.19.5.30:8500/v1/kv/upstreams/webpages/": [
    49. {
    50. "host": "127.0.0.1",
    51. "port": 30511,
    52. "weight": 1
    53. },
    54. {
    55. "host": "127.0.0.1",
    56. "port": 30512,
    57. "weight": 1
    58. }
    59. ]
    60. }
    61. }

    For example:

    1. curl http://127.0.0.1:9090/v1/discovery/consul_kv/show_dump_file | jq
    2. {
    3. "services": {
    4. "http://172.19.5.31:8500/v1/kv/upstreams/1614480/webpages/": [
    5. {
    6. "host": "172.19.5.12",
    7. "port": 8000,
    8. "weight": 120
    9. },
    10. {
    11. "host": "172.19.5.13",
    12. "port": 8000,
    13. "weight": 120
    14. }
    15. ]
    16. },
    17. "expire": 0,
    18. "last_update": 1615877468