API 网关(API Gateway)是 API 托管服务,提供 API 的完整生命周期管理,包括创建、维护、发布、运行、下线等。您可使用 API Gateway 封装自身业务,将您的数据、业务逻辑或功能安全可靠的开放出来,用以实现自身系统集成、以及与合作伙伴的业务连接。
腾讯云API网关产品文档:https://cloud.tencent.com/document/product/628/11755
实际使用过腾讯云API网关后,对于工程师而言,并不友好。文档和实践不一致、物理层拓扑对逻辑层应用不能完全透明。但从成本角度、低成本运维角度,又不得不使用它。因此,本文给出相关实践。
由于我们希望所有的测试环境的域名都绑定到同一个域名上。根据不同的PATH路由到不同的网站和接口上。例如:大数据相关的,路由到https://buzzfrog.cn/data/ ;业务系统A相关的,路由到https://buzzfrog.cn/A/ ;业务系统B相关的,路由到https://buzzfrog.cn/B/ ;业务系统C相关的,路由到https://buzzfrog.cn/C/ 等等。为此,我们将buzzfrog.cn域名绑定到一个API网关(service-xxxxxxxx,公网访问地址为:http://service-xxxxxxxx-130xxxxxxxx.sh.apigw.tencentcs.com:80,https://service-xxxxxxxx-130xxxxxxxx.sh.apigw.tencentcs.com:443 )上。绑定步骤:
也可以通过serverless.yml脚本方式配置(配置方法:https://github.com/serverless-components/tencent-apigateway/tree/master/docs/configure.md ),脚本片段如下:
stage: dev
component: apigateway
name: buzzfrog.cn
inputs:
serviceId: service-xxxxxxxx
serviceName: buzzfrog_cn
region: ap-shanghai
protocols:
- http
- https
environment: release
customDomains:
- domain: buzzfrog.cn
# 如要添加https,需先行在腾讯云-SSL证书进行认证获取certificateId
certificateId: skXXXXXX
isForcedHttps: true # 是否强制https,如果为true,必须配置 certificateId (SSL证书 ID)
# 如要设置自定义路径映射,请设置为 false
isDefaultMapping: false
pathMappingSet:
- path: /
environment: release
protocols:
- http
- https
完成buzzfrog和service-xxxxxx绑定后,我们再看后端endpoints的配置。
这里以大数据对外出口为例。详见下图:
serverless.yml片段如下:
- path: =/data/api-docs
protocol: HTTP
method: ANY
serviceType: HTTP
apiName: data-api-docs
serviceConfig:
url: 'http://service-xxxxxxxx-130xxxxxxxx.sh.apigw.tencentcs.com:80'
method: ANY
path: /api-docs
- path: /data
protocol: HTTP
method: ANY
serviceType: HTTP
apiName: data
serviceConfig:
url: 'http://service-xxxxxxxx-130xxxxxxxx.sh.apigw.tencentcs.com:80'
method: ANY
path: /test/data
一些Bad Case:
1) url: 'http://service-xxxxxxxx-130xxxxxxxx.sh.apigw.tencentcs.com:80/' url结尾不能有 /
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。