Publish APIs by Service
After your APIs are designed, developed, and deployed, you can use API7 Enterprise to publish them.
Typically, APIs are published first in test and staging environments before publishing in production environments. API7 Enterprise manages this separation through Gateway Groups, where an API is a Service with a shared Upstream.
This tutorial guides you in publishing the httpbin API to a test environment on API7 Enterprise. You will learn how to:
- Create a service manually and through an OpenAPI Specification file.
- Publish services by configuring upstream nodes and by using service discovery mechanisms.
Prerequisites
- Install API7 Enterprise and complete the Launch Your First API tutorial.
- Rename the
default
gateway group toTest Group
and configure the network.Test Group
will be the API gateway for your test environment. - Have at least one Gateway Instance in the
Test Group
.
Add a Service with 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
httpbin API
. - Click Add.
- Inside the service, click Add Route.
- From the Add Route dialog box, do the following:
- In the Name field, enter
getting-started-anything
. - In the Path field, enter
/anything/*
. - In the Methods field, choose
GET
.
- In the Name field, enter
- Click Add.
API7 Enterprise supports OpenAPI v3.0 and above. First, define your API in a YAML/JSON file as shown below:
openapi: 3.1.0
info:
title: httpbin API
description: "httpbin API for the API7 Enterprise Getting Started guides."
version: 1.0.0
paths:
"/anything/*":
get:
tags:
- Anything
summary: Returns anything that is passed into the request.
operationId: getting-started-anything
responses:
"200":
description: Successful Response
content:
application/json:
schema:
type: string
tags:
- name: Anything
description: Return anything that is passed in on the request.
Then, use it in API7 Enterprise:
- 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 the Service Using Upstream Nodes
- Publish a Single Service
- Publish Batch Services
- Select Services from the side navigation bar and then select
httpbin API
. - 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. For this tutorial, use
httpbin.org
as the host and80
as the port. - In the Weight field, use the default value
100
. - Click Add.
- In the Host and Port fields, enter your backend node address in the test environment. For this tutorial, use
- In the New Version field, enter
- 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
httpbin API
. - 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. For this tutorial, use
httpbin.org
as the host and80
as the port. - In the Weight field, use the default value
100
. - Click Add.
- In the Host and Port fields, enter your backend node address in the test environment. For this tutorial, use
- Confirm the service information and then click Publish.
Publish the Service Using Service Discovery
Instead of configuring the upstream directly, service discovery mechanisms like Consul, Eureka, Nacos or Kubernetes Service Discovery can be used to dynamically detect upstream nodes.
Once published, a service cannot directly switch between configured upstream nodes and service discovery. Instead, you have to configure this through a canary deployment.
- Kubernetes
- Nacos
- Select Gateway Groups from the side navigation bar, then choose
Test Group
. - Select Service Registries from the side navigation bar and then click Add 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 Value field.
- 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
httpbin API
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 click Add 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
httpbin API
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
Use ADC to Publish the API
Alternatively, use ADC to configure API7 Enterprise declaratively instead of the dashboard. The complete configuration is below:
services:
- name: httpbin API
upstream:
name: default
scheme: http
nodes:
- host: httpbin.org
port: 80
weight: 100
routes:
- uris:
- /anything/*
name: getting-started-anything
description: Return anything that is passed in on the request.
methods:
- GET
Synchronize the configuration to API7 Enterprise:
adc sync -f adc.yaml
Validate the API
curl "http://127.0.0.1:9080/anything/publish"
You should see the following output:
{
"args": {},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Host": "localhost",
"User-Agent": "curl/7.88.1",
"X-Amzn-Trace-Id": "Root=1-664cc6d6-10fe9f740ab1629e19cf85a2",
"X-Forwarded-Host": "localhost"
},
"json": null,
"method": "GET",
"origin": "152.15.0.1, 116.212.249.196",
"url": "http://localhost/anything/publish"
}