{"componentChunkName":"component---src-templates-blog-detail-tsx","path":"/blog/2020-07-10-produce-camp","result":{"data":{"currentBlog":{"id":"49220494-0cb8-53bc-8818-7c8c93384a6c","frontmatter":{"thumbnail":"https://img.serverlesscloud.cn/2020710/1594371160202-202129-200F6112157-1.jpg","authors":["厉辉"],"categories":["news"],"date":"2020-07-10T00:00:00.000Z","title":"腾讯云 Serverless 保障《创造营》硬糖少女 C 位出道","description":"Tencent Serverless 团队下的 API 网关产品，利用其高并发、高可用的技术特性，支撑了《创造营》节目投票环节顺利开展，保障小姐姐们 C 位出道！","authorslink":null,"translators":null,"translatorslink":null,"tags":["API 网关","创造营"],"keywords":"Serverless, API 网关, 创造营","outdated":null},"wordCount":{"words":468,"sentences":88,"paragraphs":88},"fileAbsolutePath":"/opt/build/repo/content/blog/2020-07-10-produce-camp.md","fields":{"slug":"/blog/2020-07-10-produce-camp/","keywords":["go","serverless","云函数","限流","压测","Serverless","网关","业务","投票","流量","服务"]},"html":"<p>15 位青春洋溢的女团候选成员，百万次全网观众投票，节目播出后迅速霸占热搜前十位.....</p>\n<p>在这激动人心的决赛之夜，Tencent Serverless 团队下的云 API 网关产品作为幕后英雄，利用其高并发、高可用的技术特性，支撑了节目投票环节顺利开展，面对全网粉丝狂热打 call 投票，顺利保障小姐姐们 C 位出道！</p>\n<p><img src=\"https://img.serverlesscloud.cn/202079/1594302081737-%E5%88%9B%E9%80%A0%E8%90%A5.jpg\"></p>\n<h2 id=\"不一般的投票\"><a href=\"#%E4%B8%8D%E4%B8%80%E8%88%AC%E7%9A%84%E6%8A%95%E7%A5%A8\" aria-label=\"不一般的投票 permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>不一般的投票</h2>\n<p>【投票】是一个很简单的功能，但是《创造营》的投票不一样。</p>\n<p>《创造营》是直播节目，投票时间非常短。海量全网粉丝将在同一时间瞬时涌入，瞬间的大流量和高并发，对系统的高可用性提出了极高的要求。</p>\n<p>《创造营》投票，将产生本届总冠军，是《创造营》决胜之夜的制胜环节，激动人心的时刻。投票系统的任何差池，都会对粉丝心理和节目效果造成重创。</p>\n<p>在投票的关键时刻，为了保证女团小姐姐顺利出道，技术小哥哥采用了什么样的技术设计，保证了这种特殊场景下的投票功能高可用呢？</p>\n<h2 id=\"serverless\"><a href=\"#serverless\" aria-label=\"serverless permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Serverless</h2>\n<p>Serverless 是一种云计算技术的新趋势，一方面它使开发者在构建和运行应用时无需管理服务器等基础设施，将构建应用的成本进一步降低，实现快速迭代、极速部署；同时，Serverless尤其适用于高并发场景，无需预估流量大小，而会根据流量情况自动的进行扩缩容，整个过程无需人工干预；值得一提的是，Serverless 还是按用量付费的模式，避免了无用的资源开销，大大降低了成本。</p>\n<p>为了让用户获得这些技术优势，Tencent Serverless 团队推出了云函数、API 网关、Serverless Framework、Serverless HTTP 等一系列 Serverless 产品。这些产品在诸多场景中得到了广泛的应用，例如最近超级火爆的《创造营》节目，背后也是有 Serverless 技术，默默为其提供支撑和服务。</p>\n<p>一个具体的例子，《创造营》的投票系统高可用的秘诀，就是基于腾讯云 Serverless 的重要组成部分 —— API 网关产品来打造的。Serverless 云 API 网关是如何保证投票系统的高可用性呢？接下来，将从这个点进行切入，从技术层面，为读者进行深度分析。</p>\n<h2 id=\"高可用之道\"><a href=\"#%E9%AB%98%E5%8F%AF%E7%94%A8%E4%B9%8B%E9%81%93\" aria-label=\"高可用之道 permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>高可用之道</h2>\n<ul>\n<li>产品策略：轻重逻辑分离、用户分流、功能简化</li>\n<li>客户端：重试、失败提示优化、客户端随机丢弃请求</li>\n<li>接入层：全局限流、服务降级、鉴权和 ACL</li>\n<li>逻辑层：识别热点对象和热点对象预处理、事务一致性以及事务回滚</li>\n<li>存储层：可靠性（主备/异地容灾/数据持久化）、一致性</li>\n<li>运维：灰度发布、故障演练、混沌工程</li>\n</ul>\n<p>本文主要是站在接入层的角度，浅析如何保障业务高可用。另一方面，灵活的全局限流以及服务降级功能，也是客户选择 API 网关的原因。</p>\n<p><img src=\"https://img.serverlesscloud.cn/2020710/1594374581476-c5.png\"></p>\n<p>上图是创造营小程序的简化架构图：</p>\n<ol>\n<li>小程序通过外网访问 API 网关，API 作为接入层</li>\n<li>为每个业务创建 restful API，转发到后端的不同业务</li>\n<li>扫码服务用于跳转到该小程序</li>\n<li>为小姐姐撑腰的功能是由投票服务提供</li>\n<li>抽奖和兑奖服务则分别提供抽奖以及兑换奖品的功能</li>\n</ol>\n<p>在云 API 网关创建的 API 如下图所示：</p>\n<p><img src=\"https://img.serverlesscloud.cn/2020710/1594374581495-c5.png\"></p>\n<ul>\n<li>不同业务分别建立相关 API 且配置相应后端 </li>\n<li>scan、vote、drawGift、getGift 分别对应扫码、投票、抽奖和领奖业务</li>\n<li>通常 restful API 是通过路径或者路径加方法进行匹配，不同业务建议使用不同路径，也可以根据需要某些业务 API 再进一步拆分</li>\n<li>API 方法建议选择 ANY方法</li>\n<li>不建议只创建一个 <code class=\"language-text\">/ + ANY</code> 的 API，这样便失去了 API 生命周期管理的意义</li>\n</ul>\n<h2 id=\"分析\"><a href=\"#%E5%88%86%E6%9E%90\" aria-label=\"分析 permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>分析</h2>\n<p>服务限流是接入层高可用必备条件，但限流设置为多少合适呢？普适的方案是需要根据业务场景压力预估值结合全链路压测得出的业务容量评估而出。</p>\n<p>结合上面的内容，本文主要会从以下几点保障业务高可用：</p>\n<ol>\n<li>全链路压测确定业务容量</li>\n<li>根据容量设定限流，避免高负载引起雪崩</li>\n<li>区分主次业务，优先保障核心业务，次要业务通过限流进行服务降级</li>\n</ol>\n<h2 id=\"高可用之术\"><a href=\"#%E9%AB%98%E5%8F%AF%E7%94%A8%E4%B9%8B%E6%9C%AF\" aria-label=\"高可用之术 permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>高可用之术</h2>\n<h3 id=\"压测\"><a href=\"#%E5%8E%8B%E6%B5%8B\" aria-label=\"压测 permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>压测</h3>\n<p>兵马未动，粮草先行；业务保障，压测先行。压测可以及时发现业务中的性能问题，也可以测算出业务容量，是保障业务高可用必不可少的一个环境。但压测有一些常见的注意点：</p>\n<ol>\n<li>做业务压力测试时，最好使用线上的数据和线上的环境。因为测试环境和线上环境往往存在各种各样的差异，影响压力测试结果。另一方面，为了不影响正常业务，压测时往往需要在业务低峰期压测，比如22点以后或者早上9点以前。</li>\n<li>压测测试时尽量使用线上流量或者模拟线上流量。因为线上业务的访问模型并不是平均的，无差别压测和模拟线上流量的模型的压测他们两者的结果往往会差异很大。</li>\n<li>不要从单一的服务器发起流量。一是压测时容易达到压测机器的瓶颈，影响压测结果；另一方面，由于网络链路中负载均衡、会话保持等功能的存在，单台机器压测往往会负载不均，也会影响压测结果。</li>\n</ol>\n<p>那么该选用哪个压测工具呢？ab 和 wrk 均是比较常用的开源压测工具。相较于ab，wrk的性能相较于ab性能更好，而且支持通过lua脚本构造特定的压测请求，所以wrk用的更为广泛。开源压测工具虽然胜在简便和免费，但是使用它们模拟线上流量非常复杂，故不考虑选用。</p>\n<p>基于以上压测注意事项，我们选用WeTest自研的压测大师。它可以很方便的根据线上流量模型模拟压测请求，让压测数据更为准确。</p>\n<p>主要是配合客户侧进行压测，依据线上流量模型压测结果如下：</p>\n<p><img src=\"https://img.serverlesscloud.cn/2020710/1594374581602-c5.png\"></p>\n<p>可以看到：</p>\n<ul>\n<li>业务全链路容量是 58K TPS，核心投票逻辑拉取榜单、获取投票状态以及投票容量分别可以达到18K、4K、13K TPS。</li>\n<li>时延、TPS以及成功率均符合预期（投票是由于投票业务本身的流控限制）</li>\n</ul>\n<h2 id=\"服务限流\"><a href=\"#%E6%9C%8D%E5%8A%A1%E9%99%90%E6%B5%81\" aria-label=\"服务限流 permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>服务限流</h2>\n<p>为了保障投票系统在过载的情况下不会出问题，我们需要服务限流。</p>\n<p>限流是一个非常常见的功能，比如在 nginx/openresty 等网关中，限流可以限制并发连接数（ngx<em>http</em>limit<em>conn</em>module 模块 / resty.limit.conn 库）或者 QPS （ngx<em>http</em>limit<em>req</em>module 模块 / resty.limit.req 库）；在数据库中，连接池也可以看作是限流的一种（golang 的 database/sql 库中 DB 对象维护着连接池）。</p>\n<p>在本文中讨论的是接入层网关，故限流指的是请求限流（比如QPS）。</p>\n<h3 id=\"限流业务场景\"><a href=\"#%E9%99%90%E6%B5%81%E4%B8%9A%E5%8A%A1%E5%9C%BA%E6%99%AF\" aria-label=\"限流业务场景 permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>限流业务场景</h3>\n<p>接入层限流在不同的业务场景下也有不同的职责。限流通常用于保护后端服务或者当前服务不被流量冲垮，保障服务的高可用性，常见的比如接入层网关的限流功能或者各种RPC/微服务框架的限流插件（比如 Sentinel 框架），这种情况下，服务的可用性更为重要，偶尔的限流不准是可以接受的；限流也可以用于调用计费，比如腾讯云云市场会将服务商的 API 以流量包(调用次数)的形式售卖给客户，针对 API 调用频次进行计费，因为涉及到钱，这种情况下 API 的调用次数就要求绝对精确，不容闪失。</p>\n<p>云 API 网关当前针对于这两种场景的限流场景均有限流策略，前者在 API 网关叫限流，可以从 API 维度、API 组维度以及用户维度(需要配合鉴权)对请求进行 QPS 限流；后者对应云 API 网关配额概念，该功能当前主要提供给云市场使用，用于精确计算调用次数。</p>\n<h3 id=\"限流算法\"><a href=\"#%E9%99%90%E6%B5%81%E7%AE%97%E6%B3%95\" aria-label=\"限流算法 permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>限流算法</h3>\n<p>限流算法分为单机限流和全局限流，对于业界常见的单机限流的算法对比如下：</p>\n<table>\n<thead>\n<tr>\n<th>限流算法</th>\n<th>优点</th>\n<th>缺点</th>\n<th align=\"center\">参考库</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>固定窗口</td>\n<td>实现简单</td>\n<td>临界问题，不适合周期长的限流</td>\n<td align=\"center\"></td>\n</tr>\n<tr>\n<td>滑动窗口</td>\n<td>实现简单，解决限流临界跳变</td>\n<td>限流算法精度有限、不够平滑（触发限流只能丢包）</td>\n<td align=\"center\"></td>\n</tr>\n<tr>\n<td>漏桶算法</td>\n<td>平滑流量，限流后通过流量恒定</td>\n<td>无法应对突发流量</td>\n<td align=\"center\">ngx<em>http</em>limit<em>req</em>module 模块</td>\n</tr>\n<tr>\n<td>令牌桶算法</td>\n<td>可以应对突发流量</td>\n<td>流量不平滑</td>\n<td align=\"center\"></td>\n</tr>\n</tbody>\n</table>\n<p> （当前业界主流应用层限流方法是令牌桶算法或漏桶算法。）</p>\n<p>实际接入层网关业务往往是分布式的，单机限流并不能满足需要，往往需要分布式限流。虽然可以通过预设，将限流值平均到每台机器上，但若负载流量不均匀、机器宕机或者临时扩容，均会严重影响分布式限流功能。</p>\n<p>通常，当前的生产级分布式限流均是集中式限流方案：</p>\n<ol>\n<li>全局限流状态在一个集中式节点/集群维护（比如redis等），定期向这个中心计数或者取令牌</li>\n<li>集中式节点/集群也存在可能性不可用，若不可用，则通常办法是将分布式限流退化成单机限流。</li>\n</ol>\n<p>云 API 网关当前是基于滑动窗口算法实现的单机限流，通过一个集中式节点维护全局限流状态（跟CLB的全局限流方案一致，后续会在TAPISIX的实践中优化掉）实现分布式限流。</p>\n<h3 id=\"配置方法\"><a href=\"#%E9%85%8D%E7%BD%AE%E6%96%B9%E6%B3%95\" aria-label=\"配置方法 permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>配置方法</h3>\n<p>该如何使用云 API 网关配置限流呢？</p>\n<p>上面压测表明业务整体容量在 58K 的 TPS上下，故按照后端50%最高负载保证业务稳定性，设置该整体业务的 API 组限流（或者说是服务限流）为 30K</p>\n<p>参考下图设置 API 组限流（服务限流）</p>\n<p><img src=\"https://img.serverlesscloud.cn/2020710/1594374581514-c5.png\"></p>\n<h3 id=\"服务降级\"><a href=\"#%E6%9C%8D%E5%8A%A1%E9%99%8D%E7%BA%A7\" aria-label=\"服务降级 permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>服务降级</h3>\n<p>服务降级本质是解决访问量过大与资源有限的矛盾，通过将一部分不重要的业务禁掉，来给核心业务分配更多的资源，保障核心业务以及整个系统平稳运行。</p>\n<h3 id=\"服务降级分类\"><a href=\"#%E6%9C%8D%E5%8A%A1%E9%99%8D%E7%BA%A7%E5%88%86%E7%B1%BB\" aria-label=\"服务降级分类 permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>服务降级分类</h3>\n<p>顾名思义，服务降级需要牺牲一些功能，通常有如下几种：</p>\n<table>\n<thead>\n<tr>\n<th>类型</th>\n<th>简介</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>次要功能禁用</td>\n<td>通过限流或者停止次要业务，保障核心业务更多的资源</td>\n</tr>\n<tr>\n<td>功能降级</td>\n<td>功能或业务本身简化处理逻辑或者简化返回内容</td>\n</tr>\n<tr>\n<td>一致性</td>\n<td>强一致性降级为最终一致性</td>\n</tr>\n</tbody>\n</table>\n<p>对于创造营小程序，是通过对次要 API 进行限流（调低限流值或者将限流值调0），限制次要业务，实现服务降级。</p>\n<h3 id=\"配置方法-1\"><a href=\"#%E9%85%8D%E7%BD%AE%E6%96%B9%E6%B3%95-1\" aria-label=\"配置方法 1 permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>配置方法</h3>\n<p>那么如何使用云 API 网关配置服务降级呢？</p>\n<ol>\n<li>区分核心和次要业务。创造营小程序的核心业务是投票以及扫码，次要业务是抽奖和兑奖功能。</li>\n<li>针对次要业务，使用 API 限流功能，对次要接口进行限流。故决赛当晚，对抽奖和兑奖 API 设置较低的 API 限流，保障投票等业务的资源不被抢占。</li>\n</ol>\n<p>参考下图对次要业务的 API 进行限流</p>\n<p><img src=\"https://img.serverlesscloud.cn/2020710/1594374581448-c5.png\"></p>\n<p>（如果请求被云 API 网关限流，会返回429错误码，客户端侧需要对该错误码作一定优化处理。例如，优化提示“投票业务繁忙，请稍后再试”或者内部重试）</p>\n<h2 id=\"结语\"><a href=\"#%E7%BB%93%E8%AF%AD\" aria-label=\"结语 permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>结语</h2>\n<p>经过如上一系列高可用准备，《创造营2020》决赛之夜中创造营小程序平稳度过多轮投票小高峰，顺利保障小姐姐 C 位出道。</p>\n<p>Serverless 作为云计算的新技术、新趋势，在越来越多的技术场景中，依靠其自身优势，充当着重要的角色。本文中的 API 网关就是承载着 Serverless 应用的 HTTP 服务接入层。除此之外，Serverless 还包括云函数、Serverless Framework 等众多重要组成部分，如果您对 Serverless 技术感兴趣，可以访问<a href=\"https://cloud.tencent.com/product/serverless-catalog\">腾讯云 Serverless</a> 了解更多信息。</p>\n<hr>\n<hr>\n<div id='scf-deploy-iframe-or-md'></div>\n<hr>\n<blockquote>\n<p><strong>传送门：</strong></p>\n<ul>\n<li>GitHub: <a href=\"https://github.com/serverless/serverless/blob/master/README_CN.md\">github.com/serverless</a></li>\n<li>官网：<a href=\"https://serverless.com/\">serverless.com</a></li>\n</ul>\n</blockquote>\n<p>欢迎访问：<a href=\"https://serverlesscloud.cn/\">Serverless 中文网</a>，您可以在 <a href=\"https://serverlesscloud.cn/best-practice\">最佳实践</a> 里体验更多关于 Serverless 应用的开发！</p>","tableOfContents":"<ul>\n<li><a href=\"/blog/2020-07-10-produce-camp/#%E4%B8%8D%E4%B8%80%E8%88%AC%E7%9A%84%E6%8A%95%E7%A5%A8\">不一般的投票</a></li>\n<li><a href=\"/blog/2020-07-10-produce-camp/#serverless\">Serverless</a></li>\n<li><a href=\"/blog/2020-07-10-produce-camp/#%E9%AB%98%E5%8F%AF%E7%94%A8%E4%B9%8B%E9%81%93\">高可用之道</a></li>\n<li><a href=\"/blog/2020-07-10-produce-camp/#%E5%88%86%E6%9E%90\">分析</a></li>\n<li>\n<p><a href=\"/blog/2020-07-10-produce-camp/#%E9%AB%98%E5%8F%AF%E7%94%A8%E4%B9%8B%E6%9C%AF\">高可用之术</a></p>\n<ul>\n<li><a href=\"/blog/2020-07-10-produce-camp/#%E5%8E%8B%E6%B5%8B\">压测</a></li>\n</ul>\n</li>\n<li>\n<p><a href=\"/blog/2020-07-10-produce-camp/#%E6%9C%8D%E5%8A%A1%E9%99%90%E6%B5%81\">服务限流</a></p>\n<ul>\n<li><a href=\"/blog/2020-07-10-produce-camp/#%E9%99%90%E6%B5%81%E4%B8%9A%E5%8A%A1%E5%9C%BA%E6%99%AF\">限流业务场景</a></li>\n<li><a href=\"/blog/2020-07-10-produce-camp/#%E9%99%90%E6%B5%81%E7%AE%97%E6%B3%95\">限流算法</a></li>\n<li><a href=\"/blog/2020-07-10-produce-camp/#%E9%85%8D%E7%BD%AE%E6%96%B9%E6%B3%95\">配置方法</a></li>\n<li><a href=\"/blog/2020-07-10-produce-camp/#%E6%9C%8D%E5%8A%A1%E9%99%8D%E7%BA%A7\">服务降级</a></li>\n<li><a href=\"/blog/2020-07-10-produce-camp/#%E6%9C%8D%E5%8A%A1%E9%99%8D%E7%BA%A7%E5%88%86%E7%B1%BB\">服务降级分类</a></li>\n<li><a href=\"/blog/2020-07-10-produce-camp/#%E9%85%8D%E7%BD%AE%E6%96%B9%E6%B3%95-1\">配置方法</a></li>\n</ul>\n</li>\n<li><a href=\"/blog/2020-07-10-produce-camp/#%E7%BB%93%E8%AF%AD\">结语</a></li>\n</ul>"},"previousBlog":{"id":"7a740ddd-e8dd-5a94-b4a7-f78a8175ccb5","frontmatter":{"thumbnail":"https://img.serverlesscloud.cn/2020720/1595232134658-1594123221943-%E4%BA%AC%E4%B8%9C%E6%99%BA%E8%BF%9E%E4%BA%91.jpg","authors":["朱琅"],"categories":["news"],"date":"2020-07-16T00:00:00.000Z","title":"京东智联云在 Serverless 的探索","description":"本文整理自 ServerlessDay · China 大会，讲师为京东智联云的 PaaS 产品负责⼈朱琅。","authorslink":null,"translators":null,"translatorslink":null,"tags":["ServerlessDays","Meetup"],"keywords":"Serverless, ServerlessDays, Serverless实践,","outdated":null},"wordCount":{"words":910,"sentences":64,"paragraphs":64},"fileAbsolutePath":"/opt/build/repo/content/blog/2020-07-16-serverlessdays-jingdong.md","fields":{"slug":"/blog/2020-07-16-serverlessdays-jingdong/","keywords":["serverless","spa","Serverless","服务","FaaS","京东","代码"]}},"nextBlog":{"id":"4f6a66f1-0167-5a7d-a0bf-70517b5cd51b","frontmatter":{"thumbnail":"https://img.serverlesscloud.cn/2020710/1594369778294-1594295165809-%E4%BC%81%E4%B8%9A%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_15942951126394.jpg","authors":["卢萌凯/董文枭/董文强/李垦"],"categories":["news"],"date":"2020-07-09T00:00:00.000Z","title":"Serverless 应用实践及典型案例解析","description":"本文整理自 ServerlessDay · China 大会 - 《Serverless 应用实践及典型案例解析》的分享。","authorslink":null,"translators":null,"translatorslink":null,"tags":["ServerlessDays","Meetup"],"keywords":"Serverless, ServerlessDays, Serverless实践","outdated":null},"wordCount":{"words":467,"sentences":51,"paragraphs":51},"fileAbsolutePath":"/opt/build/repo/content/blog/2020-07-09-serverlessdays-tencent-2.md","fields":{"slug":"/blog/2020-07-09-serverlessdays-tencent-2/","keywords":["go","serverless","单页应用","服务端渲染","云函数","云原生","Serverless","函数","serverlesscloud","转码","前端"]}},"recommendBlogs":{"edges":[{"node":{"id":"73576d26-e0ce-5f26-9330-64b4f3889157","frontmatter":{"thumbnail":"https://main.qcloudimg.com/raw/3cb7b20955d78ced738e0279bb3f6f41.jpg","authors":["AndreaPasswater"],"categories":["news","engineering-culture"],"date":"2018-03-09T00:00:00.000Z","title":"Serverless 数据解读：2018 报告","description":"Serverless Framework 使用统计数据：事件源、服务结构、运行时长等等。","authorslink":["https://serverless.com/author/andreapasswater/"],"translators":["Aceyclee"],"translatorslink":["https://www.zhihu.com/people/Aceyclee"],"tags":["事件源","服务结构"],"keywords":"Serverless 统计数据,Serverless 事件源,Serverless 服务结构","outdated":null},"wordCount":{"words":212,"sentences":45,"paragraphs":45},"fileAbsolutePath":"/opt/build/repo/content/blog/2018-03-09-serverless-by-the-numbers-2018-data-report.md","fields":{"slug":"/blog/2018-03-09-serverless-by-the-numbers-2018-data-report/","keywords":["go","serverless","无服务器","云函数","服务","使用率","Go","部署"]}}},{"node":{"id":"84876745-cbfb-5c1d-9f6c-7c74338d5d28","frontmatter":{"thumbnail":"https://s3-us-west-2.amazonaws.com/assets.site.serverless.com/images/champions/champions_banner.jpg","authors":["RupakGanguly"],"categories":["news","engineering-culture"],"date":"2018-04-10T00:00:00.000Z","title":"2018 年首批无服务器社区冠军介绍","description":"无服务器社区冠军是无服务器社区的领导者。快来认识一下我们 2018 年度的社区英雄吧！","authorslink":null,"translators":null,"translatorslink":null,"tags":null,"keywords":null,"outdated":null},"wordCount":{"words":194,"sentences":32,"paragraphs":32},"fileAbsolutePath":"/opt/build/repo/content/blog/2018-04-10-announcing-new-2018-serverless-champions.md","fields":{"slug":"/blog/2018-04-10-announcing-new-2018-serverless-champions/","keywords":["serverless","webpack","无服务器","无服务器架构","服务器","serverless","champions","架构","blog","github","框架"]}}},{"node":{"id":"1e1a90fc-3b65-540c-b726-755999e7912a","frontmatter":{"thumbnail":"https://main.qcloudimg.com/raw/8a0db1c9fd8b51c15d0b006291d52bf5.jpg","authors":["AndreaPasswater"],"categories":["news","engineering-culture"],"date":"2018-07-19T00:00:00.000Z","title":"2018 年社区调查：Serverless 使用率大幅增长","description":"我们曾在开发社区进行问卷调查，询问 Serverless 的使用情况。它的使用率增长连我们自己都惊讶不已，下面来看看数据。","authorslink":["https://serverless.com/author/andreapasswater/"],"translators":["Aceyclee"],"translatorslink":["https://www.zhihu.com/people/Aceyclee"],"tags":["Component","Serverless"],"keywords":"Serverless 社区调查,Serverless 使用情况,Serverless 数据","outdated":null},"wordCount":{"words":267,"sentences":50,"paragraphs":49},"fileAbsolutePath":"/opt/build/repo/content/blog/2018-07-19-2018-serverless-community-survey-huge-growth-usage.md","fields":{"slug":"/blog/2018-07-19-2018-serverless-community-survey-huge-growth-usage/","keywords":["go","serverless","无服务器","Serverless","serverless"]}}},{"node":{"id":"8311b008-2b15-5225-8adc-9b75e484177b","frontmatter":{"thumbnail":"https://img.serverlesscloud.cn/2020414/1586880819419-%E5%B0%81%E9%9D%A2%E5%9B%BE%20%286%29.png","authors":["serverless 社区"],"categories":["news"],"date":"2019-04-08T00:00:00.000Z","title":"邀您参加 | K8S&云原生技术开放日-北京站","description":"K8S&云原生技术开放日，将围绕K8S、Serverless等云原生相关技术，从最佳落地实践、不同场景技术改造、应用优化，到开源领域深度技术研究，和技术爱好者们一起探讨。","authorslink":["https://zhuanlan.zhihu.com/ServerlessGo"],"translators":null,"translatorslink":null,"tags":["云原生","Serverless"],"keywords":"serverless 入门,serverless 框架,腾讯云 serverless","outdated":null},"wordCount":{"words":36,"sentences":9,"paragraphs":9},"fileAbsolutePath":"/opt/build/repo/content/blog/2019-04-08-k8s.md","fields":{"slug":"/blog/2019-04-08-k8s/","keywords":["serverless","云原生","serverless","Serverless","技术","原生","serverlesscloud","github"]}}},{"node":{"id":"25f709db-cde0-59d7-81f4-944c46a6dd8a","frontmatter":{"thumbnail":"https://img.serverlesscloud.cn/2020414/1586880571672-%E5%B0%81%E9%9D%A2%E5%9B%BE%20%285%29.png","authors":["serverless 社区"],"categories":["news"],"date":"2019-04-04T00:00:00.000Z","title":"Serverless 技术风暴来袭，开发者该如何应对？","description":"Hello Serverless技术沙龙北京站，将围绕Serverless的应用场景，客户案例，FaaS+BaaS架构的实现方案等，针对未来的无服务形态进行交流和讨论，释放技术想象！","authorslink":["https://zhuanlan.zhihu.com/ServerlessGo"],"translators":null,"translatorslink":null,"tags":["应用场景","Serverless"],"keywords":"serverless 入门,serverless 框架,腾讯云 serverless","outdated":null},"wordCount":{"words":37,"sentences":8,"paragraphs":8},"fileAbsolutePath":"/opt/build/repo/content/blog/2019-04-04-technology-storm.md","fields":{"slug":"/blog/2019-04-04-technology-storm/","keywords":["serverless","Serverless","serverless","serverlesscloud","github","技术","架构","围绕"]}}},{"node":{"id":"4bc4d946-6187-5c3d-91c6-d98c4c4c1314","frontmatter":{"thumbnail":"https://img.serverlesscloud.cn/2020414/1586880172144-%E5%B0%81%E9%9D%A2%E5%9B%BE%20%284%29.png","authors":["serverless 社区"],"categories":["news"],"date":"2019-06-11T00:00:00.000Z","title":"一图读懂无服务器云函数","description":"本图将重要信息进行梳理和提炼，帮助读者更好的理解无服务器云函数的理念和作用","authorslink":["https://zhuanlan.zhihu.com/ServerlessGo"],"translators":null,"translatorslink":null,"tags":["云函数","Serverless"],"keywords":"serverless 入门,serverless 框架,腾讯云 serverless","outdated":null},"wordCount":{"words":31,"sentences":6,"paragraphs":6},"fileAbsolutePath":"/opt/build/repo/content/blog/2019-06-11-one-picture-serverless-cloud-function.md","fields":{"slug":"/blog/2019-06-11-one-picture-serverless-cloud-function/","keywords":["serverless","无服务器","无服务器云函数","云函数","serverless","serverlesscloud","Serverless","github","开发者","函数"]}}},{"node":{"id":"1cd76f95-14a9-5fd5-926b-456af189b7ed","frontmatter":{"thumbnail":"https://img.serverlesscloud.cn/2020326/1585217744291-%E6%91%84%E5%9B%BE%E7%BD%91_400730082_wx.jpg","authors":["朱峰 Ben"],"categories":["news"],"date":"2019-10-14T00:00:00.000Z","title":"由浅入深说 Serverless 之云函数的生命周期","description":"希望通过文章分享帮助大家更深入的了解 Serverless 背后的机制并掌握相关的最佳实践。","authorslink":["https://github.com/jiangliu5267"],"translators":null,"translatorslink":null,"tags":["云函数"],"keywords":"Serverless, Serverless前端开发, Serverless云函数","outdated":null},"wordCount":{"words":74,"sentences":29,"paragraphs":29},"fileAbsolutePath":"/opt/build/repo/content/blog/2019-10-14-serverless-cloud-function.md","fields":{"slug":"/blog/2019-10-14-serverless-cloud-function/","keywords":["faas","java","云函数","函数","触发","实例","事件","数据库","启动","连接","sql","代码"]}}},{"node":{"id":"4f16f8bc-1a35-5394-9029-f7abc2ad6137","frontmatter":{"thumbnail":"https://img.serverlesscloud.cn/qianyi/images/YHl6UWa9s629ucl0iaVcic8rL06uEBM5go4LSXiaHnibDJSBjLGAhGlWz3QZ1RZzd3NeCibjJxOyUApDO7TaPYcwxsw.jpg","authors":["Serverless 中文网"],"categories":["news"],"date":"2019-10-26T00:00:00.000Z","title":"Hello Serverless 实战沙龙回顾","description":"2019 年 10 月 26 日，Hello Serverless 沙龙活动在广州市海珠区腾讯众创空间成功举办，一起看看我们都为大家准备了哪些精彩的演讲吧！","authorslink":["https://github.com/jiangliu5267"],"translators":null,"translatorslink":null,"tags":["Meetup"],"keywords":"Serverless, Serverless技术沙龙,Hello Serverless","outdated":null},"wordCount":{"words":64,"sentences":13,"paragraphs":13},"fileAbsolutePath":"/opt/build/repo/content/blog/2019-10-26-hello-serverless-meetup.md","fields":{"slug":"/blog/2019-10-26-hello-serverless-meetup/","keywords":["go","serverless","云函数","云原生","Serverless","存储","腾讯","serverlesscloud"]}}}],"totalCount":46}},"pageContext":{"isCreatedByStatefulCreatePages":false,"blogId":"49220494-0cb8-53bc-8818-7c8c93384a6c","previousBlogId":"7a740ddd-e8dd-5a94-b4a7-f78a8175ccb5","nextBlogId":"4f6a66f1-0167-5a7d-a0bf-70517b5cd51b","categories":["news"]}}}