{"componentChunkName":"component---src-templates-best-practice-detail-tsx","path":"/best-practice/2021-03-01-clb-serverless","result":{"data":{"currentBlog":{"id":"7687b641-edc1-5b3d-9825-bd3729e47978","frontmatter":{"thumbnail":"https://main.qcloudimg.com/raw/d32d37dcb538d5804ede488d392bac3d.jpg","authors":["陈涛"],"categories":["best-practice"],"date":"2021-03-01T00:00:00.000Z","title":"如何快速迁移传统 LB 公网业务到 Serverless?","description":"使用 Serverless 云函数负载均衡（Cloud Load Balancer，CLB）触发方式的优势、典型应用场景和使用指引","authorslink":null,"translators":null,"translatorslink":null,"tags":["Serverless","CLB"],"keywords":null,"outdated":null},"wordCount":{"words":158,"sentences":36,"paragraphs":36},"fileAbsolutePath":"/opt/build/repo/content/best-practice/2021-03-01-clb-serverless.md","fields":{"slug":"/best-practice/2021-03-01-clb-serverless/","keywords":["serverless","云函数","Serverless","函数","场景","服务"]},"html":"<p>Serverless 云函数触发困难？函数触发太复杂不会配 ？无法平移传统 LB 公网业务？现在云函数已全面支持负载均衡（Cloud Load Balancer，CLB） 触发方式。提供服务级访问函数方案，适用于企业节点较多，有历史服务在CVM、容器、自建机房、且服务较重访问量较多的场景。</p>\n<p>通过 CLB 触发器可以深度对接 Serverless 函数公网访问服务，帮助开发者平滑迁移传统架构到 Serverless，提供理解成本更低，更易操作，更加便捷的公网接入及 Web 访问体验。</p>\n<h2 id=\"clb-触发器的优势及特点\"><a href=\"#clb-%E8%A7%A6%E5%8F%91%E5%99%A8%E7%9A%84%E4%BC%98%E5%8A%BF%E5%8F%8A%E7%89%B9%E7%82%B9\" aria-label=\"clb 触发器的优势及特点 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>CLB 触发器的优势及特点</h2>\n<ol>\n<li>海量调用函数场景下，对企业开发者较为友好，相较于其他方式更具性价比。　</li>\n<li>支持 IP 维度的 Serverless 型服务管理，可结合域名分地域解析能力，帮助用户实现不同地域触发不同函数执行。提升函数服务的整体可用性。</li>\n<li>服务级封装，单个 LB 可以同时封装 CVM，容器与 Serverless 服务， Serverless 服务可以更加方便的切入业务系统的核心服务，可以通过CLB触发器的方式直接路由到函数做支线能力，无需再次绑定域名，解构服务。</li>\n<li>WAF防护，CLB可以直接对接WAF产品对非法请求做拦截，提供更加专业的WEB应用服务防护。</li>\n<li>支持SSL自有证书，通过CLB可以实现 Serverless 应用层的证书服务，支持 SNI 多域名证书绑定。</li>\n</ol>\n<h2 id=\"clb-触发器的典型应用场景\"><a href=\"#clb-%E8%A7%A6%E5%8F%91%E5%99%A8%E7%9A%84%E5%85%B8%E5%9E%8B%E5%BA%94%E7%94%A8%E5%9C%BA%E6%99%AF\" aria-label=\"clb 触发器的典型应用场景 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>CLB 触发器的典型应用场景</h2>\n<p><strong>典型场景一：秒杀/抢购活动</strong></p>\n<p>秒杀 &#x26; 抢购活动对整体资源的应用弹性的要求比较高，而且和业务的主干场景联系较为紧密。一般是业务系统中较为独立的模块，便于迁移和改造。且可以通过 CLB 的能力无缝支持到云函数，整体计费相对于按调用次数的收费场景要友好很多，迁移成本会比较低。同域名下也可以轻松解决 CORS 跨域问题。</p>\n<p><img src=\"https://main.qcloudimg.com/raw/00ad6ae630ae7649cbab9d4a183fb6f6.png\"></p>\n<p><strong>典型场景二：辅助系统架构</strong></p>\n<p>如企业的非主干 WEB 业务，订单系统，采集系统，BI分析等对削峰填谷比较敏感的非主干场景，整体迁移成本会比较底 低且迁移收益大。</p>\n<p><img src=\"https://main.qcloudimg.com/raw/754eb7b524ed5c110e58ded89bebb391.png\"></p>\n<p><strong>典型场景三：动静态业务分离</strong></p>\n<p>当业务请求量较大时，可以通过区分网站的静态和动态请求，有针对性的对其进行分发处理，有效减少后端负载压力。其中动态请求可以通过单独部署负载均衡及关联 Serverless 服务进行处理；静态内容可以通过接入 CDN 服务，通过对象存储进行优化，显著提升加载速度。</p>\n<p><img src=\"https://main.qcloudimg.com/raw/bb932260ab32349d3fec006f7e9cfd2d.png\"></p>\n<p><strong>典型场景四：同域名，地域级访问服务</strong></p>\n<p>业务对地域要求较高时，可以通过CLB对函数做地域级访问划分。</p>\n<h2 id=\"clb-触发器配置及使用指引\"><a href=\"#clb-%E8%A7%A6%E5%8F%91%E5%99%A8%E9%85%8D%E7%BD%AE%E5%8F%8A%E4%BD%BF%E7%94%A8%E6%8C%87%E5%BC%95\" aria-label=\"clb 触发器配置及使用指引 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>CLB 触发器配置及使用指引</h2>\n<ol>\n<li>在CLB控制台新建”负载均衡“实例及”监听器”资源</li>\n</ol>\n<p><img src=\"https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/2b7539adc5304cd9966de68740f2c2c2~tplv-k3u1fbpfcp-zoom-1.image\" alt=\"img\"></p>\n<p><img src=\"https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/7438f9138fad43cb9d93909c39d12fd1~tplv-k3u1fbpfcp-zoom-1.image\" alt=\"img\"></p>\n<ol start=\"2\">\n<li>在CLB控制台或函数控制台绑定需要访问的函数（暂支持单函数单URL绑定）</li>\n</ol>\n<p><img src=\"https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/44b953433c5b42d58b161e2e8d99bf22~tplv-k3u1fbpfcp-zoom-1.image\" alt=\"img\"></p>\n<p><img src=\"https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/8cfe70e893f1487e843867d950321c87~tplv-k3u1fbpfcp-zoom-1.image\" alt=\"img\"></p>\n<ol start=\"3\">\n<li>编辑函数代码 （需要按照特定响应集成格式返回，详见<a href=\"https://cloud.tencent.com/document/product/583/52635\">产品文档</a>）</li>\n</ol>\n<p><img src=\"https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/dbf08c6706a14b0eb75390c3a55fca0f~tplv-k3u1fbpfcp-zoom-1.image\" alt=\"img\"></p>\n<p>响应集成演示代码：</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">  # -*- coding:utf-8 -*-\n  def main_handler(event, context):\n      html=&quot;CLB Runing Now.&quot;\n      return {\n          &quot;isBase64Encoded&quot;: False,\n          &quot;statusCode&quot;: 200,\n          &quot;headers&quot;: {&#39;Content-Type&#39;: &#39;ccc&#39;},\n          &quot;body&quot;: (html)\n          # &quot;body&quot;: (&#39;%s%s&#39;  %(html,event))\n      }</code></pre></div>\n<h2 id=\"使用说明\"><a href=\"#%E4%BD%BF%E7%94%A8%E8%AF%B4%E6%98%8E\" 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>CLB 触发器目前处于灰度测试阶段，您可点击 <a href=\"https://cloud.tencent.com/apply/p/h2r3ix3s5vs\">申请链接</a> 进行申请。</p>\n<p>CLB 账户分为标准账户类型和传统账户类型。传统账户类型不支持绑定 SCF ，建议升级为标准账户类型。详情可参见 <a href=\"https://cloud.tencent.com/document/product/1199/49090\">账户类型升级说明</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=\"/best-practice/2021-03-01-clb-serverless/#clb-%E8%A7%A6%E5%8F%91%E5%99%A8%E7%9A%84%E4%BC%98%E5%8A%BF%E5%8F%8A%E7%89%B9%E7%82%B9\">CLB 触发器的优势及特点</a></li>\n<li><a href=\"/best-practice/2021-03-01-clb-serverless/#clb-%E8%A7%A6%E5%8F%91%E5%99%A8%E7%9A%84%E5%85%B8%E5%9E%8B%E5%BA%94%E7%94%A8%E5%9C%BA%E6%99%AF\">CLB 触发器的典型应用场景</a></li>\n<li><a href=\"/best-practice/2021-03-01-clb-serverless/#clb-%E8%A7%A6%E5%8F%91%E5%99%A8%E9%85%8D%E7%BD%AE%E5%8F%8A%E4%BD%BF%E7%94%A8%E6%8C%87%E5%BC%95\">CLB 触发器配置及使用指引</a></li>\n<li><a href=\"/best-practice/2021-03-01-clb-serverless/#%E4%BD%BF%E7%94%A8%E8%AF%B4%E6%98%8E\">使用说明</a></li>\n</ul>"},"previousBlog":{"id":"cbaa9f36-8aa0-5048-9fb3-c61234bb5e50","frontmatter":{"thumbnail":"https://main.qcloudimg.com/raw/cb8be2ca0ef4971973d7e3bde82687f6.jpg","authors":["April"],"categories":["best-practice"],"date":"2021-03-02T00:00:00.000Z","title":"Serverless Wordpress 系列建站教程（一）","description":"一种基于 Serverless 架构的 WordPress 全新部署方式","authorslink":null,"translators":null,"translatorslink":null,"tags":["Serverless","WordPress"],"keywords":null,"outdated":null},"wordCount":{"words":158,"sentences":25,"paragraphs":25},"fileAbsolutePath":"/opt/build/repo/content/best-practice/2021-03-02-wp.md","fields":{"slug":"/best-practice/2021-03-02-wp/","keywords":["serverless","云函数","Serverless","WordPress","部署","tencent","应用","document","serverless"]}},"nextBlog":{"id":"3717c513-52f7-54cd-9f34-e7f2a52dd587","frontmatter":{"thumbnail":"https://main.qcloudimg.com/raw/238c88435e45de91ad4475f6e085c2a6.jpg","authors":["李啸川"],"categories":["best-practice"],"date":"2021-02-08T00:00:00.000Z","title":"解锁长时重计算：云函数首创异步执行模式","description":"可用于 2K4K 音视频处理、ETL 数据批处理、机器学习及 AI 推理等单任务重计算等场景","authorslink":null,"translators":null,"translatorslink":null,"tags":["Serverless","异步执行"],"keywords":null,"outdated":null},"wordCount":{"words":117,"sentences":35,"paragraphs":35},"fileAbsolutePath":"/opt/build/repo/content/best-practice/2021-02-08-asynchronous-execution.md","fields":{"slug":"/best-practice/2021-02-08-asynchronous-execution/","keywords":["serverless","云函数","云原生","函数","执行","异步","同步","serverless","模式","调用"]}}},"pageContext":{"isCreatedByStatefulCreatePages":false,"blogId":"7687b641-edc1-5b3d-9825-bd3729e47978","previousBlogId":"cbaa9f36-8aa0-5048-9fb3-c61234bb5e50","nextBlogId":"3717c513-52f7-54cd-9f34-e7f2a52dd587"}}}