前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >腾讯云API网关的点滴实践

腾讯云API网关的点滴实践

原创
作者头像
buzzfrog
发布2023-03-28 09:59:39
3.5K0
发布2023-03-28 09:59:39
举报
文章被收录于专栏:云上修行云上修行

背景

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 )上。绑定步骤:

  1. 在腾讯云的DNSPOD中,将域名tiwater.cc的子域名dev用CNAME方式绑定到service-xxxxxxxx-130xxxxxxxx.sh.apigw.tencentcs.com
  1. 在腾讯云的API网关service-xxxxxxxx的自定义域名中,新建自定义域名buzzfrog.cn

也可以通过serverless.yml脚本方式配置(配置方法:https://github.com/serverless-components/tencent-apigateway/tree/master/docs/configure.md ),脚本片段如下:

代码语言:txt
复制
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片段如下:

代码语言:txt
复制
    - 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 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 背景
  • 一个栗子🌰
相关产品与服务
API 网关
腾讯云 API 网关(API Gateway)是腾讯云推出的一种 API 托管服务,能提供 API 的完整生命周期管理,包括创建、维护、发布、运行、下线等。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档