nacos

Service discovery via 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` is the `default_weight` that will be attached to each discovered node that
  8. # doesn't have a weight explicitly provided in nacos results
  9. weight: 100 # default 100
  10. timeout:
  11. connect: 2000 # default 2000 ms
  12. send: 2000 # default 2000 ms
  13. 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

L7

Here is an example of routing a request with an URI 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:

nacos - 图1note

You can fetch the admin_key from config.yaml and save to an environment variable with the following command:

  1. admin_key=$(yq '.deployment.admin.admin_key[0].key' conf/config.yaml | sed 's/"//g')
  1. $ curl http://127.0.0.1:9180/apisix/admin/routes/1 -H "X-API-KEY: $admin_key" -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. }

L4

Nacos service discovery also supports use in L4, the configuration method is similar to L7.

  1. $ curl http://127.0.0.1:9180/apisix/admin/stream_routes/1 -H "X-API-KEY: $admin_key" -X PUT -i -d '
  2. {
  3. "remote_addr": "127.0.0.1",
  4. "upstream": {
  5. "scheme": "tcp",
  6. "discovery_type": "nacos",
  7. "service_name": "APISIX-NACOS",
  8. "type": "roundrobin"
  9. }
  10. }'

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 an URI of “/nacosWithNamespaceId/*“ to a service with 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:9180/apisix/admin/routes/2 -H "X-API-KEY: $admin_key" -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. }

Specify the group

Example of routing a request with an URI of “/nacosWithGroupName/*“ to a service with 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:9180/apisix/admin/routes/3 -H "X-API-KEY: $admin_key" -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. }

Specify the namespace and group

Example of routing a request with an URI of “/nacosWithNamespaceIdAndGroupName/*“ to a service with 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:9180/apisix/admin/routes/4 -H "X-API-KEY: $admin_key" -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. }