Publish APIs by Service
After designing, developing, and deploying APIs, you can publish them in API7 Enterprise for accessibility. You can publish to test, staging, and production environments, or to multiple regions. Since APIs are typically organized by backend service, they are managed by service in API7 Enterprise. The APIs for a particular backend share upstream configurations and get updated together when backend changes occur.
This tutorial describes how to publish Swagger Petstore
APIs to a mocked test
environment.
Prerequisites
- Install API7 Enterprise.
- Obtain a user account with Super Admin or API Provider role.
- Rename the default gateway group to
Test Group
and configure the network. This gateway group acts as the API gateway for yourtest
environment. - Add at least one gateway instance in the
Test Group
.
Add Service and Its Routes
- Add Manually
- Import OpenAPI 3.0 Specification
- Select Services from the side navigation bar, then click Add Service.
- Select Add Manually.
- In the Name field, enter
Swagger Petstore
. - Click Add.
- Inside the service, click Add Route.
- From the Add Route dialog box, do the following:
- In the Name field, enter
getPetById
. - In the Path field, enter
/pet/*
. - In the Methods field, choose
GET
.
- In the Name field, enter
- Click Add.
- Select Services from the side navigation bar, then click Add Service.
- Select Import OpenAPI.
- Upload your YAML/JSON file and choose
HTTP
in the Upstream Scheme field. - Click Next.
- Confirm the following information and then click Next:
- Name:the
title
field in OpenAPI Specification. - Labels:the
tag
field in OpenAPI Specification. - Description:the
description
field in OpenAPI Specification. - Routes:the
Paths
field in OpenAPI Specification.
- Name:the
- Click Add.
Publish Service Using Upstream Nodes
- Publish Single Service
- Publish Batch Services
- Select Services from the side navigation bar and then select
Swagger Petstore
. - Click Publish Now.
- Select
Test Group
and then click Next. From the dialog box, do the following:
In the New Version field, enter
1.0.0
.In the How to find the upstream field, choose
Use Nodes
.- Click Add Node. From the dialog box, do the following:
- In the Host and Port fields, enter your backend node address in the
test
environment or the mock server address. - In the Weight field, use the default value
100
. - Click Add.
- In the Host and Port fields, enter your backend node address in the
- Confirm the service information and then click Publish.
- Select Services from the side navigation bar and then click Batch Publish Services.
- In the Gateway Group field, choose
Test Group
and then click Next. Click Add Service. From the dialog box, do the following:
- In the Service field, choose
Swagger Petstore
. - In the New Version field, enter
1.0.0
. Click Add.
- In the Service field, choose
Confirm the service information and then click Next.
- In the How to find the upstream field, choose
Use Nodes
. - Click Add Node. From the dialog box, do the following:
- In the Host and Port fields, enter your backend node address in the
test
environment or the mock server address. - In the Weight field, use the default value
100
. - Click Add.
- In the Host and Port fields, enter your backend node address in the
- Confirm the service information and then click Publish.
Publish Service Using Service Discovery
Service discovery mechanisms like Consul, Eureka, Nacos or Kubernetes Service Discovery can be used to dynamically detect upstream nodes.
After publishing, a service cannot directly switch between using defined upstream nodes vs using service discovery. However, switching between upstream nodes and service discovery can be done through canary.
- Kubernetes
- Nacos
- Select Gateway Groups from the side navigation bar, then choose
Test Group
. - Select Service Registries from the side navigation bar and then clickAdd Service Registry Connection.
- From the Add Route dialog box, do the following:
- In the Name field, enter
Registry for Test
. - In the Discovery Type field, choose
Kubernetes
. - Fill in the API Server Address and Token Valuefield.
- Click Add.
- In the Name field, enter
- Wait to make sure the status of the service registry is
Healthy
. - Select Services from the side navigation bar, then click Publish Now for the
Swagger Petstore
service. - Choose the
Test Group
gateway group and then click Next. - From the Publish dialog box, do the following:
- In the New Version field, enter
1.0.0
. - In the How to find the upstream field, choose
Use Service Discovery
. - In the Service Registry field, choose
Registry for Test
, then choose the Namespace and Service Name. - Confirm the service information, then click Publish.
- In the New Version field, enter
- Select Gateway Groups from the side navigation bar, then choose
Test Group
. - Select Service Registries from the side navigation bar and then clickAdd Service Registry Connection.
- From the Add Route dialog box, do the following:
- In the Name field, enter
Registry for Test
. - In the Discovery Type field, choose
Nacos
. - In the Hosts field, fill in the host address and port.
- In the How to Get Token field, choose a way to get the token and configure related parameters.
- Click Add.
- In the Name field, enter
- Wait to make sure the status of the service registry is
Healthy
. - Select Services from the side navigation bar, then click Publish Now for the
Swagger Petstore
service. - Choose the
Test Group
gateway group and then click Next. - From the Publish dialog box, do the following:
- In the New Version field, enter
1.0.0
. - In the How to find the upstream field, choose
Use Service Discovery
. - In the Service Registry field, choose
Registry for Test
, then choose the Namespace, Group and Service Name. - Confirm the service information, then click Publish.
- In the New Version field, enter
Validate APIs
curl "http://127.0.0.1:9080/pet/1"
You should see the following output:
{
"name": "Dog",
"photoUrls": [
"https://example.com/dog-1.jpg",
"https://example.com/dog-2.jpg"
],
"id": 1,
"category": {
"id": 1,
"name": "pets"
},
"tags": [
{
"id": 1,
"name": "friendly"
},
{
"id": 2,
"name": "smart"
}
],
"status": "available"
}