nacos

Service discovery via Nacos

This is experimental discovery module for Nacos.

The performance of this module needs to be improved:

  1. send the request parallelly.

Configuration for Nacos

Add following configuration in conf/config.yaml :

  1. discovery:
  2. nacos:
  3. host:
  4. - "http://${username}:${password}@${host1}:${port1}"
  5. prefix: "/nacos/v1/"
  6. fetch_interval: 30 # default 30 sec
  7. weight: 100 # default 100
  8. timeout:
  9. connect: 2000 # default 2000 ms
  10. send: 2000 # default 2000 ms
  11. read: 5000 # default 5000 ms

And you can config it in short by default value:

  1. discovery:
  2. nacos:
  3. host:
  4. - "http://192.168.33.1:8848"

Upstream setting

Here is an example of routing a request with a URL of “/nacos/*“ to a service which named “http://192.168.33.1:8848/nacos/v1/ns/instance/list?serviceName=APISIX-NACOS” and use nacos 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": "/nacos/*",
  4. "upstream": {
  5. "service_name": "APISIX-NACOS",
  6. "type": "roundrobin",
  7. "discovery_type": "nacos"
  8. }
  9. }'

The formatted response as below:

  1. {
  2. "node": {
  3. "key": "\/apisix\/routes\/1",
  4. "value": {
  5. "id": "1",
  6. "create_time": 1615796097,
  7. "status": 1,
  8. "update_time": 1615799165,
  9. "upstream": {
  10. "hash_on": "vars",
  11. "pass_host": "pass",
  12. "scheme": "http",
  13. "service_name": "APISIX-NACOS",
  14. "type": "roundrobin",
  15. "discovery_type": "nacos"
  16. },
  17. "priority": 0,
  18. "uri": "\/nacos\/*"
  19. }
  20. },
  21. "action": "set"
  22. }

discovery_args

NameTypeRequirementDefaultValidDescription
namespace_idstringoptionalpublicThis parameter is used to specify the namespace of the corresponding service
group_namestringoptionalDEFAULT_GROUPThis parameter is used to specify the group of the corresponding service

Specify the namespace

Example of routing a request with a URL of “/nacosWithNamespaceId/*“ to a service which name, namespaceId “http://192.168.33.1:8848/nacos/v1/ns/instance/list?serviceName=APISIX-NACOS&namespaceId=test_ns” and use nacos discovery client in the registry:

  1. $ curl http://127.0.0.1:9080/apisix/admin/routes/2 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '
  2. {
  3. "uri": "/nacosWithNamespaceId/*",
  4. "upstream": {
  5. "service_name": "APISIX-NACOS",
  6. "type": "roundrobin",
  7. "discovery_type": "nacos",
  8. "discovery_args": {
  9. "namespace_id": "test_ns"
  10. }
  11. }
  12. }'

The formatted response as below:

  1. {
  2. "node": {
  3. "key": "\/apisix\/routes\/2",
  4. "value": {
  5. "id": "2",
  6. "create_time": 1615796097,
  7. "status": 1,
  8. "update_time": 1615799165,
  9. "upstream": {
  10. "hash_on": "vars",
  11. "pass_host": "pass",
  12. "scheme": "http",
  13. "service_name": "APISIX-NACOS",
  14. "type": "roundrobin",
  15. "discovery_type": "nacos",
  16. "discovery_args": {
  17. "namespace_id": "test_ns"
  18. }
  19. },
  20. "priority": 0,
  21. "uri": "\/nacosWithNamespaceId\/*"
  22. }
  23. },
  24. "action": "set"
  25. }

Specify the group

Example of routing a request with a URL of “/nacosWithGroupName/*“ to a service which name, groupName “http://192.168.33.1:8848/nacos/v1/ns/instance/list?serviceName=APISIX-NACOS&groupName=test_group” and use nacos discovery client in the registry:

  1. $ curl http://127.0.0.1:9080/apisix/admin/routes/3 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '
  2. {
  3. "uri": "/nacosWithGroupName/*",
  4. "upstream": {
  5. "service_name": "APISIX-NACOS",
  6. "type": "roundrobin",
  7. "discovery_type": "nacos",
  8. "discovery_args": {
  9. "group_name": "test_group"
  10. }
  11. }
  12. }'

The formatted response as below:

  1. {
  2. "node": {
  3. "key": "\/apisix\/routes\/3",
  4. "value": {
  5. "id": "3",
  6. "create_time": 1615796097,
  7. "status": 1,
  8. "update_time": 1615799165,
  9. "upstream": {
  10. "hash_on": "vars",
  11. "pass_host": "pass",
  12. "scheme": "http",
  13. "service_name": "APISIX-NACOS",
  14. "type": "roundrobin",
  15. "discovery_type": "nacos",
  16. "discovery_args": {
  17. "group_name": "test_group"
  18. }
  19. },
  20. "priority": 0,
  21. "uri": "\/nacosWithGroupName\/*"
  22. }
  23. },
  24. "action": "set"
  25. }

Specify the namespace and group

Example of routing a request with a URL of “/nacosWithNamespaceIdAndGroupName/*“ to a service which name, namespaceId, groupName “http://192.168.33.1:8848/nacos/v1/ns/instance/list?serviceName=APISIX-NACOS&namespaceId=test_ns&groupName=test_group” and use nacos discovery client in the registry:

  1. $ curl http://127.0.0.1:9080/apisix/admin/routes/4 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '
  2. {
  3. "uri": "/nacosWithNamespaceIdAndGroupName/*",
  4. "upstream": {
  5. "service_name": "APISIX-NACOS",
  6. "type": "roundrobin",
  7. "discovery_type": "nacos",
  8. "discovery_args": {
  9. "namespace_id": "test_ns",
  10. "group_name": "test_group"
  11. }
  12. }
  13. }'

The formatted response as below:

  1. {
  2. "node": {
  3. "key": "\/apisix\/routes\/4",
  4. "value": {
  5. "id": "4",
  6. "create_time": 1615796097,
  7. "status": 1,
  8. "update_time": 1615799165,
  9. "upstream": {
  10. "hash_on": "vars",
  11. "pass_host": "pass",
  12. "scheme": "http",
  13. "service_name": "APISIX-NACOS",
  14. "type": "roundrobin",
  15. "discovery_type": "nacos",
  16. "discovery_args": {
  17. "namespace_id": "test_ns",
  18. "group_name": "test_group"
  19. }
  20. },
  21. "priority": 0,
  22. "uri": "\/nacosWithNamespaceIdAndGroupName\/*"
  23. }
  24. },
  25. "action": "set"
  26. }