Open Service APIs For Aliyun API Gateway
1. Experiment
1.1 Knowledge points
In this experiment, Alibaba Cloud API Gateway is used. This experiment demonstrates the process of connecting services to API Gateway. API Gateway provides high-performance and high-availability API hosting services to help expose your applications that are deployed on Alibaba Cloud products, such as ECS and Container Service. API Gateway allows you to publish and manage APIs, and maintain their lifecycles. You can perform simple operations to make data or services public rapidly, cost-effectively, and safely.
1.2 Experiment process
- Deploy Django.
- Create API Gateway.
1.3 Required cloud resources
1.4 Prerequisites
- If you are using your own Alibaba Cloud account instead of the account provided by this lab for this experiment, be sure to choose the same Ubuntu 16.04 operating system for your ECS in order to run the experiment smoothly.
- Before starting the experiment, confirm that the previous experiment has been normally closed and exited.
2. Start the experiment environment
Click Start Lab in the upper-right corner of the page to start the experiment.
.
After the experiment environment is started, the system has deployed resources required by this experiment in the background, including the ECS instance, RDS instance, Server Load Balancer (SLB) instance, and OSS bucket. An account consisting of the username and password for logging on to the Alibaba Cloud console is also provided.
After the experiment environment is started and related resources are deployed, an experiment countdown starts. You have two hours to perform experimental operations. After the countdown ends, the experiment stops and related resources are released. During the experiment, pay attention to the remaining time and arrange your time wisely. Use the username and password provided by the system to log on to the Alibaba Cloud console and view related resources:
Go to the logon page of the Alibaba Cloud console.
Enter the sub-user account and click Next.
Enter the sub-user password and click Log on.
After you log on to the console, the following page appears.
3. Deploy Django
3.1 Log on to an ECS instance
Click Elastic Compute Service, as shown in the following figure.
We can see one running ECS instance in the US West 1 region.
Copy this ECS instance’s Internet IP address and remotely log on to this ECS (Ubuntu system) instance. For details of remote login, refer to login。
The default account name and password of the ECS instance:
Account name: root
Password: nkYHG890..
3.2 Install Django
After you log on, run the following command to update the APT installation source:
apt update
Run the following command to install Django:
apt install -y python3-pip && pip3 install Django==2.2.7
Run the following command to create a Django project named Labex:
django-admin startproject Labex
Run the following command to create an app named aliyun in the Labex project:
cd Labex && django-admin startapp aliyun
Run the following command to configure the default database for the Labex project:
python3 manage.py migrate
Run the vim Labex/settings.py
command to open the configuration file of the Labex project and modify the settings, as shown in the following figure. Save the modifications and exit.
Run the vim aliyun/views.py
command, delete the source content in the file, and copy the following content to the file. Save the modifications and exit.
This file defines the functions that are used to receive and process client requests.
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def show(request):
return HttpResponse("<h1>this is aliyun app</h1>")
def add(request):
a = request.GET['a']
b = request.GET['b']
c = int(a) + int(b)
return HttpResponse(str(c))
def subtract(request):
a = request.GET.get('a','none_a')
b = request.POST.get('b','none_b')
c = int(a) - int(b)
return HttpResponse(str(c))
Run the vim Labex/urls.py
command to open the URL file of the Labex project and modify the file, as shown in the following figure. Save the modifications and exit.
from aliyun import views
path('', views.show),
path('add/', views.add, name='add'),
path('subtract/', views.subtract, name='subtract'),
Run the following command to start Django:
python3 manage.py runserver 0.0.0.0:80
4. Create API Gateway
4.1 Create an API group
Return to the Alibaba Cloud console and access the API Gateway console, as shown in the following figure.
Create an API group, as shown in the following figure.
Set Group Name and click Confirm.
Click OK.
4.2 Create a VPC access
Create a Virtual Private Cloud (VPC) access, as shown in the following figure.
Set VPC Access Name, VPC Id, Instance Id Or IP, and Instance Port, and click Confirm, as shown in the following figure.
Click VPC instances, as shown in the preceding figure. On the page that appears, obtain the VPC ID, as shown in the following figure.
Obtain the ECS ID, as shown in the following figure.
Create success.
4.3 Create an API
Create an API to receive parameters in the GET request, as shown in the following figure.
Set Group, API Name, and Security Certification, and then click Next.
Set the frontend request path and parameters of the API, and then click Next.
Set the backend request path and parameters of the API, enter the name of the created VPC access, and click Next.
Click Create.
Click Confirm.
You can see that the API has been created. Then, click Publish.
Select Test, enter test online in the Enter Change Remarks field, and click Publish to publish the API in the test environment, as shown in the following figure.
You can see that the API has been published in the test environment.
Create the second API to receive parameters in both the GET and POST requests.
Click Create API.
Set API Name and click Next.
Set the frontend request path and parameters, with one parameter as the Query parameter and the other as the Body parameter. Set HTTP Method to POST and click Next.
Set the backend request path and parameters.
Click Next.
Click Create.
Publish api2 in the test environment with the same method.
4.4 Authorize an app
To test whether the APIs can be normally used, create an app.
Set APP Name and click Confirm.
Click Confirm.
You can access the corresponding API only after authorizing the app.
Authorize labex_app to use api1 first.
Select Test, enter the keyword in the search box, click Search, and then click Add. api1 is published in the test environment. If api1 is published in the production environment, select Release.
After the app is added, click Confirm.
Use the same method to authorize labex_app to use api2.
4.5 Call APIs
After the app is authorized to use both APIs, click the app name to go to the APP details page.
You can see both APIs. Click Debug API for api1.
Enter any two numbers in the text boxes of the Query parameters and click Send Request, as shown in the following figure.
You can see that the response failed.
Return to the CLI of ECS instances. The following errors occur on the Django server. Note the IP address in the following error information, which needs to be added to the Django configuration file later.
Press Ctrl+C to stop the service.
Run the vim Labex/settings.py
command to open the Django configuration file, and add the noted IP address to the file, as shown in the following figure. Save the modification and exit.
Run the following command to start Django:
python3 manage.py runserver 0.0.0.0:80
Return to the Alibaba Cloud console and click Send Request again. You can see a normal response this time.
<font color='red'>The user can cut off the above result picture when doing the experiment and send it to the teacher, indicating that the part of the current chapter has been completed.</font>
Click Debug API for api2.
Set the Query and Body parameters and click Send Request. You can see the response.
Now, the APIs have been successfully called.
<font color='red'>Users can cut off the above result picture when they are doing the experiment and send it to the teacher, indicating that the current experiment has been completed.</font>
Tips:
Before you leave this lab, remember to log out from your Alibaba RAM account before clicking the stop button of your lab. Otherwise, you will encounter some issues when opening a new lab session in the same browser.
5. Experiment summary
This experiment demonstrates the process of connecting services to API Gateway. Alibaba Cloud API Gateway uses a distributed deployment and automatic scaling model to respond to a large number of API access requests with very low latency. It provides highly secure and efficient gateway functions for your backend services. API Gateway can also handle complex transactions, such as API reference maintenance, SDK maintenance, and API version management. This greatly reduces daily maintenance costs.