Function review: Apache APIs IX implements service discovery based on Nacos

Posted by csabi_fl on Wed, 23 Feb 2022 10:41:28 +0100

This article introduces the basic concepts of Apache APIs IX and Nacos and the role of the registry, and shows you the specific operations of Apache APIs IX to realize service discovery based on Nacos.

background information

About Apache APIs IX

Apache APISIX is a dynamic, real-time and high-performance API gateway, which provides rich traffic management functions such as load balancing, dynamic upstream, gray publishing, service fusing, identity authentication, observability and so on. Apache APISIX not only has many practical plug-ins, but also supports plug-in dynamic change and hot plug.

About Nacos

Nacos is an easy-to-use dynamic service discovery, configuration and service management platform open source by Alibaba. It provides a set of easy-to-use feature sets, which can help you quickly realize dynamic service discovery, service configuration, service metadata and traffic management, so that you can build, deliver and manage the micro service platform more quickly and easily. Nacos is a service infrastructure for building a modern application architecture centered on "service" (such as micro service paradigm and cloud native paradigm).

Registration Center

What is a registry

Service registry is the core component of service management. It is similar to the role of directory service. It is also one of the most basic facilities in micro service architecture. It is mainly used to store service information, such as service provider URL, routing information, etc. The implementation of the registry is to map the complex server information into simple and understandable information to the client through a mapping method.

The core functions of the registry are as follows:

  • Service registration: the service provider registers with the registration center.
  • Service discovery: the service consumer can find the call routing information of the service provider through the registration center.
  • Health detection: ensure that the service nodes registered in the registration center can be called normally, so as to avoid the waste of calling resources caused by invalid nodes.

Why do I need a registry?

The essence of the registry is to decouple service providers and service consumers. In the micro service system, various business services will call each other frequently, and it is necessary to uniformly manage the IP, port and other routing information of each service. But how to manage it? We can provide relevant information of existing services to a unified registration center for management through the service registration function of the registration center.

From the above description, you can see that the registry can help users quickly find services and service addresses through mapping. With the business update iteration, the service will change frequently. After a new service is registered in the server or the service is down, the client can still pull the service list through the service discovery function of the registration center. If the service node of the registration center changes, the registration center will send a request to notify the client to pull again.

If the service of the server is suddenly down and there is no feedback to the registry, the client can actively report the heartbeat at a fixed time interval through the health check function of the registry to indicate its service status to the server. If the service status is abnormal, the registration center will be notified, and the registration center can eliminate the service nodes that have been down in time to avoid the waste of resources.

What application scenarios does Apache APIs IX + Nacos provide users with?

Apache APISIX + Nacos can centralize all business independent controls in each microservice node in Apache APISIX for unified management, that is, the ability to realize the proxy and routing forwarding of interface services through Apache APISIX. After each micro service is registered on Nacos, Apache apisid can obtain the service list and find the corresponding service address through the service discovery function of Nacos, so as to realize dynamic proxy.

[external chain picture transfer failed. The source station may have anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-sk6xaubu-1645607132156)( https://tfzcfxawmk.feishu.cn/...)]

Apache APIs IX implements service discovery based on Nacos

prerequisite

This article is based on the following environment.

Step 1: service registration

  1. Use node JS Koa framework starts a simple test service on port 3005 as Upstream.
const Koa = require('koa');
const app = new Koa();

app.use(async ctx => {
  ctx.body = 'Hello World';
});

app.listen(3005);
  1. Register the service on the command line by requesting the Nacos Open API.
curl -X POST 'http://127.0.0.1:8848/nacos/v1/ns/instance?serviceName=APISIX-NACOS&ip=127.0.0.1&port=3005&ephemeral=false'
  1. After service registration, use the following command to query the current service.
curl -X GET 'http://127.0.0.1:8848/nacos/v1/ns/instance/list?serviceName=APISIX-NACOS'

Examples of correct returned results are as follows:

{
  "name": "DEFAULT_GROUP@@APISIX-NACOS",
  "groupName": "DEFAULT_GROUP",
  "clusters": "",
  "cacheMillis": 10000,
  "hosts": [
    {
      "instanceId": "127.0.0.1#3005#DEFAULT#DEFAULT_GROUP@@APISIX-NACOS",
      "ip": "127.0.0.1",
      "port": 3005,
      "weight": 1.0,
      "healthy": true,
      "enabled": true,
      "ephemeral": true,
      "clusterName": "DEFAULT",
      "serviceName": "DEFAULT_GROUP@@APISIX-NACOS",
      "metadata": {},
      "instanceHeartBeatInterval": 5000,
      "instanceHeartBeatTimeOut": 15000,
      "ipDeleteTimeout": 30000,
      "instanceIdGenerator": "simple"
    }
  ],
  "lastRefTime": 1643191399694,
  "checksum": "",
  "allIPs": false,
  "reachProtectionThreshold": false,
  "valid": true
}

Step 2: add a new Nacos route

Use the Admin API provided by Apache APIs IX to create a new Route , Apis IX through upstream discovery_ In the type field, select the service discovery type to use, upstream service_ Name needs to be associated with the corresponding service name of the registry. Therefore, when creating a route, specify the service discovery type as nacos.

curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '
{
    "uri": "/nacos/*",
    "upstream": {
        "service_name": "APISIX-NACOS",
        "type": "roundrobin",
        "discovery_type": "nacos"
    }
}'

In the above command, the request header X-API-KEY is the access token of Admin API, which can be found in conf / config Apifix.com in yaml file admin_ key. Key view.

After successful addition, the correct returned results are as follows:

{
  "action": "set",
  "node": {
    "key": "\/apisix\/routes\/1",
    "value": {
      "update_time": 1643191044,
      "create_time": 1643176603,
      "priority": 0,
      "uri": "\/nacos\/*",
      "upstream": {
        "hash_on": "vars",
        "discovery_type": "nacos",
        "scheme": "http",
        "pass_host": "pass",
        "type": "roundrobin",
        "service_name": "APISIX-NACOS"
      },
      "id": "1",
      "status": 1
    }
  }
}

In addition, you can also use the discovery_ Args passes other service related parameters to specify the namespace or group where the service is located. Refer to the following table for details:

nametypeOptionalDefault valueEffective valueexplain
namespace_idstringOptionalpublic Namespace where the service is located
group_namestringOptionalDEFAULT_GROUP The group in which the service is located

Step 3: verify the configuration results

Use the following command to send the request to the route to be configured.

curl -i http://127.0.0.1:9080/nacos/

Examples of normal return results are as follows:

HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
Content-Length: 11
Connection: keep-alive
Date: Thu, 27 Jan 2022 00:48:26 GMT
Server: APISIX/2.12.0

Hello World

It can be seen from the example that the new route in Apache apisik can find the correct service address through Nacos service discovery and respond normally.

summary

This paper introduces the concept of registry and how Apache apifix cooperates with Nacos to implement routing agent based on service discovery. In the actual scenario, you need to see the specific business requirements and past technical architecture of how to use Apache apisid and Nacos together.

For more description and complete configuration information of nacos plug-in, please refer to the official website document: nacos.

The Apache apisik project is currently developing other plug-ins to support the integration of more services. If you are interested, you can GitHub Discussions Initiate discussion or adopt mailing list Communicate.

Topics: Back-end Nacos gateway apisix