Integrate ZooKeeper with API Gateway for service discovery so that API Gateway can automatically route requests to backend microservices registered in ZooKeeper.
Overview
This topic covers 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
How it works
ZooKeeper stores data in a tree structure similar to a file system. Each node has a unique access path, such as /provider/service. Service providers register microservices instance information with ZooKeeper service nodes. API Gateway listens to these nodes, parses IP addresses and ports, and distributes client requests to backend microservices instances based on load balancing policies.
-
Service providers register microservices instances with ZooKeeper service nodes.
-
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.
-
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.
-
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.
-
When another client request is received, API Gateway routes and forwards the request based on the updated information.
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.
Step 1: Create a VPC integration instance
API Gateway communicates with ZooKeeper and microservices applications over internal networks. Conventional dedicated instances do not support custom internal networks, so the VPC integration instance was introduced to enable direct communication between API Gateway and user VPCs. For more information about how to create a VPC integration instance, see VPC-integrated dedicated instance.
Step 2: Deploy the Zookeeper service and register a microservices application in ZooKeeper
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.
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.
For more information about how to deploy a ZooKeeper service by using MSE, see Create a ZooKeeper engine.
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
}
]
}
}
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.
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.
Step 3: Create an API group
APIs are managed in groups. Create an API group before you create an API.
-
Log on to the API Gateway console.
-
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.
After the group is created, click the group name in the group list to go to the group details page, where you can bind a domain name, modify basic information, and change the instance type.
API Gateway automatically assigns a public second-level domain name to every API group. This domain name is for debugging only. Direct API calls using this domain name are limited to 1,000 per day in regions in the Chinese mainland and 100 per day in the China (Hong Kong) region and other regions outside the Chinese mainland. We recommend that you bind an independent domain name to your API group. For more information, see Bind a custom domain name to an API group.
Step 4: Create a backend service
To create an API with Service discovery as the backend service type, first create a backend service.
-
Log on to the API Gateway console.
-
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. Create a backend service in the environment before you publish an API. On the right side of the page, click Create to go to the Define Backend Service page.
Enter the following ZooKeeper and microservices information on the Define Backend Service page:
-
Service URL: the internal endpoint of ZooKeeper. Required.
-
Namespace: the name of the parent node that stores microservices application information. Required.
-
Service Name: the name of the microservices application. Required.
The namespace and service name together form the access path of a service node. For example, if the node path is "/provider/service", set the namespace to "provider" and the service name to "service". The service provider can register microservices instance information in the node at /provider/service and its child nodes.
Step 5: 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.
-
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.
You can configure the Security Authentication and AppCode Authentication parameters based on your requirements. This example only provides a reference configuration.
-
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.
-
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.
-
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.
-
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.
Step 6: Create an application and authorize it to call the API
An application is the identity that an API caller uses to call an API. In this example, Alibaba Cloud App was selected for Security Authentication when the API was created. After the API is published, create an application and authorize it to call the API. For more information, see Authorization management.
Step 7: Debug the API
API Gateway supports online debugging. Use this feature to verify that an API is correctly configured before making it available to clients.
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, enter different values to verify that the API works as expected.
When debugging, make sure you use the authorized application and that the debugging environment matches the one in which the application is authorized. Otherwise, debugging may fail.