All Products
Search
Document Center

API Gateway:Zookeeper

Last Updated:Mar 20, 2024

This topic describes how ZooKeeper is integrated into API Gateway and how you can create, publish, and call an API whose backend service type is Service discovery.

1. Overview

This topic consists of the following sections:

  • How it works

  • Create a VPC integration dedicated instance (only VPC integration instances support ZooKeeper)

  • Deploy the ZooKeeper service and register a microservices application

  • Create an API group

  • Create a backend service

  • Create an API

  • Create an application and authorize it to call the API

  • Debug the API

2. How it works

ZooKeeper stores data in a tree structure that is similar to a file system. Each node in the tree has a unique access path, such as /provider/service. Service providers can register the instance information of their microservices applications with the service nodes of Zookeeper. API Gateway then listens to these nodes, parses service IP addresses and ports from the nodes, and then distributes client requests to backend microservices instances based on load balancing policies.

image

  1. Service providers register microservices instances with ZooKeeper service nodes.

  2. API Gateway listens to nodes and their child nodes based on their access paths, and parses microservices instance information that is stored in the nodes, such as the instance IP address and port number. IP addresses and port numbers are cached in API Gateway to reduce communication overhead.

  3. When a client request is received, API Gateway obtains an IP address and port number from its cache and forwards the request to the corresponding microservices server. Then, API Gateway receives a response from the server and forwards the response to the client.

  4. If the microservices application has an information change, for example, a microservices instance goes offline or a new instance is added, ZooKeeper synchronizes the change to API Gateway. API Gateway updates the information in its cache to ensure consistency between itself and ZooKeeper.

  5. When another client request is received, API Gateway routes and forwards the request based on the updated information.

Important

API Gateway communicates with ZooKeeper and microservices instances over virtual private clouds (VPCs). Therefore, you must configure an internal address as the service address of ZooKeeper when you create an API. Otherwise, API Gateway returns an invalid ZooKeeper address exception. For the same reason, internal URLs must be registered as the URLs of backend microservices applications in Zookeeper. If API Gateway obtains a public URL from ZooKeeper after receiving an API request, API Gateway cannot process the request. Instead, API Gateway returns error code I504IA. If API Gateway fails to obtain any microservices URL from ZooKeeper, API Gateway returns error code I504BA.

3. Create a VPC integration instance

API Gateway communicates with ZooKeeper and microservices applications registered in ZooKeeper over internal networks. Because custom internal networks cannot be defined on conventional dedicated instances, the API Gateway team develops a new instance type, namely the VPC integration instance, to allow API Gateway to directly communicate with user VPCs. For more information about how to create a VPC integration instance, see VPC integration instances.

4. Deploy the Zookeeper service and register a microservices application in ZooKeeper

4.1 Deploy the ZooKeeper service

ZooKeeper must be deployed in the same VPC, zone, and vSwitch as the VPC integration instance that you created. You can deploy the native ZooKeeper service on an Elastic Compute Service (ECS) instance on the vSwitch. This way, the access address of ZooKeeper can be set to Internal IP address of ECS instance: Port of ZooKeeper. When you create an ECS instance, set Network to VPC and select the same VPC, zone, and vSwitch as those you selected when you created the VPC integration instance.

Note

For more information about how to deploy ZooKeeper, see ZooKeeper Getting Started Guide. For more information about how to create an ECS instance, see Instructions for purchase.

You can also use Alibaba Cloud Microservice Engine (MSE) to deploy a ZooKeeper service. When you do so, set the network type to VPC and select the same VPC and vSwitch as the VPC integration instance that you created. After the ZooKeeper service is deployed, you can view its endpoint in the MSE instance list.

Note

For more information about how to deploy a ZooKeeper service by using MSE, see Create a ZooKeeper engine.

4.2 Register a microservices application

You must deploy the microservices application in the same VPC, zone, and vSwitch as the VPC integration instance that you created.. This ensures that the microservices application is registered in the ZooKeeper service and accessed by API Gateway. You can choose to deploy the microservices application on an ECS instance. The ECS instance must be created in the same vSwitch as the VPC integration instance that you created.

Use the following format for the information about the microservices application that is registered with ZooKeeper:

{
    "address":"localhost",
    "enabled":true,
    "id":"cb1467f745c64d9f8cbfa5c2bd217cb8",
    "name":"provider",
    "payload":{

    },
    "port":8080,
    "registrationTimeUTC":1699525871398,
    "serviceType":"DYNAMIC",
    "sslPort":443,
    "uriSpec":{
        "parts":[
            {
                "value":"scheme",
                "variable":true
            },
            {
                "value":"://",
                "variable":false
            },
            {
                "value":"address",
                "variable":true
            },
            {
                "value":":",
                "variable":false
            },
            {
                "value":"port",
                "variable":true
            }
        ]
    }
}
Note

For the structure of data stored on ZooKeeper nodes, see the org.apache.curator.x.discovery.ServiceInstance class provided by the open source project Apache Curator. Take note that uriSpec cannot be left empty. We recommend that you configure it as {scheme}://{address}:{port}. If you want to use the HTTPS protocol, configure the sslPort attribute.

Important

Invalid data formats may cause API Gateway to fail to parse the backend addresses of microservices instances. Microservices applications registered with Zookeeper must support HTTP calls.

5. Create an API group

APIs are managed in API groups. You must create an API group before you create an API.

  1. Log on to the API Gateway console.

  2. In the left-side navigation pane, choose Manage APIs > API Groups. In the upper-right corner of the page, click Create Group. In the dialog box that appears, select the created VPC integration, configure the Group Name and BasePath parameters, and click Confirm.

Note

After the group is created, click the group name in the group list to go to the group details page. On the details page, you can bind a domain name, modify the basic information, and change the instance type.

API Gateway automatically assigns a public second-level domain name to every API group. The second-level domain name is used only for debugging. If you directly make API calls by using this domain name, you can make a maximum of 1,000 calls per day in regions in the Chinese mainland and 100 calls per day in the China (Hong Kong) region and other regions outside the Chinese mainland. We recommend that you bind an independent domain name for your API group. For more information, see Bind a custom domain name to allow API calls.

6. Create a backend service

To create an API with Service discovery as the backend service type, you must first create a backend service.

  1. Log on to the API Gateway console.

  2. In the left-side navigation pane, choose Manage APIs > Backend Services. In the upper-right corner of the page, click Create Backend Service. In the dialog box that appears, enter the name of the backend service and set Type to Service discovery. After the backend service is created, you can find the created backend service in the backend service list and click the service name to go to the backend service details page.

In this example, the Production environment is selected. You must create a backend service in the environment before you can publish an API to an environment. On the right side of the page, click Create to go to the Define Backend Service page.

You must enter the following information about the ZooKeeper service and the microservices application on the Define Backend Service page:

  • Service URL: the internal endpoint of ZooKeeper. This parameter is required.

  • Namespace: the name of the parent node that stores the microservices application information. This parameter is required.

  • Service Name: the name of the microservices application. This parameter is required.

Note

The namespace and the service name combine to constitute the access path of a service node. For example, if the path of the node is "/provider/service", the namespace and the service name can be set to "provider" and "service", respectively. The service provider can register the information of the microservices instance in the node whose access path is /provider/service and its child nodes.

7. Create an API

In the left-side navigation pane of the API Gateway console, choose Manage APIs-APIs. In the upper-right corner of the page, click Create API.

  1. Configure basic information for the API

In this step, configure basic information for the API that you want to create, including the API group to which the API belongs and the name, authentication method, type, and description of the API. Select the API group that you created, select Alibaba Cloud App for Security Authentication, and select Enable AppCode Authentication (Header & Query) for AppCode Authentication. You can specify a custom API name and description.

Note

You can configure the Security Authentication and AppCode Authentication parameters based on your requirements. This topic only provides an example.

  1. Define API requests

In this step, define how a client, such as a browser, mobile app, or business system, requests your API. The parameters include the request type, protocol, request path, HTTP method, input parameter request mode, and input parameter definitions. You can specify the request path and HTTP method based on your business requirements.

  1. Define the backend service

In this step, define how API Gateway maps and processes request parameters after it receives a request. You can also specify to which backend address API Gateway connects. In this example, Use Existing Backend Service is selected for the Configuration Mode parameter and Service discovery is selected for the Backend Service Type parameter. You can set the other parameters based on your business requirements.

  1. Define responses for the API

In this step, configure response information to generate API documentation. The documentation helps API callers better understand APIs. You can also specify a sample success response and a sample failure response. In this example, this step is skipped. Click Create.

  1. Publish the API.

After the preceding configurations are saved, you must publish the API. All configurations you perform on an API can take effect only after you publish the API to an environment. In this example, the backend service is configured in the production environment. Therefore, the API is published to the production environment. If you want to publish the API to another environment, you must first configure the backend service in the destination environment. In this example, click Publish in the message that indicates successful API creation. In the Publish API dialog box, set the Stage parameter to Release, enter remarks, and then click Publish.

8. Create an application and authorize it to call the API

An application is an identity that an API caller assumes to call an API. In this example, Alibaba Cloud App was selected for the Security Authentication parameter when the API was created. Therefore, after the API is published, you must create an application and authorize the application to call the API that you created. For more information, see API authorization.

9. Debug the API

API Gateway supports online debugging. We recommend that you use this feature to check whether an API is correctly configured before you provide this API for clients to call.

On the APIs page, click the created API. In the left-side navigation pane of the API details page, click Debug API. If you defined request parameters for the API, you can enter different values for the request parameters to check whether the API works as expected.

When you debug the API, make sure that the authorized application is used and the environment for debugging is the one in which the application is authorized to call the API. Otherwise, the debugging may fail.