nacos

基于 Nacos 的服务发现

当前模块的性能有待改进:

  1. 并行发送请求。

Nacos 配置

在文件 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

也可以这样简洁配置(未配置项使用默认值):

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

Upstream 设置

七层

例如,转发 URI 匹配 “/nacos/*“ 的请求到一个上游服务, 该服务在 Nacos 中的服务名是 APISIX-NACOS,查询地址是 http://192.168.33.1:8848/nacos/v1/ns/instance/list?serviceName=APISIX-NACOS,创建路由时指定服务发现类型为 nacos。

nacos - 图1note

您可以这样从 config.yaml 中获取 admin_key 并存入环境变量:

  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. }'

响应如下:

  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. }

四层

nacos 服务发现也支持在四层中使用,配置方式与七层的类似。

  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. }'

参数

名字类型可选项默认值有效值说明
namespace_idstring可选public服务所在的命名空间
group_namestring可选DEFAULT_GROUP服务所在的组

指定命名空间

例如,转发 URI 匹配 “/nacosWithNamespaceId/*“ 的请求到一个上游服务, 该服务在 Nacos 中的服务名是 APISIX-NACOS,命名空间是 test_ns,查询地址是 http://192.168.33.1:8848/nacos/v1/ns/instance/list?serviceName=APISIX-NACOS&namespaceId=test_ns,创建路由时指定服务发现类型为 nacos。

  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. }'

响应如下:

  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. }

指定组

例如,转发 URI 匹配 “/nacosWithGroupName/*“ 的请求到一个上游服务, 该服务在 Nacos 中的服务名是 APISIX-NACOS,组名是 test_group,查询地址是 http://192.168.33.1:8848/nacos/v1/ns/instance/list?serviceName=APISIX-NACOS&groupName=test_group,创建路由时指定服务发现类型为 nacos。

  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. }'

响应如下:

  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. }

同时指定命名空间和组

例如,转发 URI 匹配 “/nacosWithNamespaceIdAndGroupName/*“ 的请求到一个上游服务, 该服务在 Nacos 中的服务名是 APISIX-NACOS,命名空间是 test_ns,组名是 test_group,查询地址是 http://192.168.33.1:8848/nacos/v1/ns/instance/list?serviceName=APISIX-NACOS&namespaceId=test_ns&groupName=test_group,创建路由时指定服务发现类型为 nacos。

  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. }'

响应如下:

  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. }