nacos

    1. 并行发送请求。

    在文件 中添加以下配置到:

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

    1. discovery:
    2. nacos:
    3. host:
    4. - "http://192.168.33.1:8848"
    1. $ curl http://127.0.0.1:9180/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. }'

    响应如下:

    指定命名空间

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

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

    指定组

    例如,转发 URI 匹配 “/nacosWithGroupName/*“ 的请求到一个上游服务, 该服务在 Nacos 中的服务名是 APISIX-NACOS,组名是 test_group,查询地址是 ,创建路由时指定服务发现类型为 nacos 。

    响应如下:

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

    同时指定命名空间和组

    1. $ curl http://127.0.0.1:9180/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. }'

    响应如下: