{"componentChunkName":"component---src-templates-best-practice-detail-tsx","path":"/best-practice/2021-11-18-springboot-scf","result":{"data":{"currentBlog":{"id":"f228879d-ecaf-5cb5-8ea0-e3f7d8bba8f5","frontmatter":{"thumbnail":"https://qcloudimg.tencent-cloud.cn/raw/49bb9d642f89800cd3d6fd8de7905300.jpg","authors":["颜松柏"],"categories":["best-practice"],"date":"2021-11-18T00:00:00.000Z","title":"SpringBoot + SCF 最佳实践：实现待办应用","description":"本文将介绍如何通过 Serverless 云函数 的 Web 函数使用 SpringBoot 搭建一个待办应用。","authorslink":null,"translators":null,"translatorslink":null,"tags":["Serverless","云函数"],"keywords":null,"outdated":null},"wordCount":{"words":239,"sentences":46,"paragraphs":46},"fileAbsolutePath":"/opt/build/repo/content/best-practice/2021-11-18-springboot-scf.md","fields":{"slug":"/best-practice/2021-11-18-springboot-scf/","keywords":["java","serverless","云函数","待办","函数","tencent","scf","模板","Serverless"]},"html":"<p>SpringBoot 是由 Pivotal 团队提供的框架，用来简化新 Spring 应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置，从而使开发人员不再需要定义样板化的配置。Serverless 对于微服务的价值在于：</p>\n<ol>\n<li>每个微服务 API 被调用的频率不一样，可以利用 Serverless 精准管理成本和弹性。</li>\n<li>不用担心一个 API 调用量大而需要扩容整个服务，Serverless 可以自动扩缩容。</li>\n<li>不需要去运维每个服务背后部署多少个容器，多少个服务器，不用做负载均衡。</li>\n<li>屏蔽了 K8s 等容器编排的复杂学习成本。</li>\n<li>Serverless 这种无状态的特性也非常符合微服务使用 Restful API 的特性。</li>\n</ol>\n<p>本文将介绍如何通过 Serverless 云函数 的 Web 函数使用 SpringBoot 搭建一个待办应用。</p>\n<h3 id=\"01-前提条件\"><a href=\"#01-%E5%89%8D%E6%8F%90%E6%9D%A1%E4%BB%B6\" aria-label=\"01 前提条件 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>01. 前提条件</h3>\n<p>请参考云函数 JAVA 开发指南准备开发环境和工具。</p>\n<h3 id=\"02-创建待办应用\"><a href=\"#02-%E5%88%9B%E5%BB%BA%E5%BE%85%E5%8A%9E%E5%BA%94%E7%94%A8\" aria-label=\"02 创建待办应用 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>02. 创建待办应用</h3>\n<p>SCF 提供模板函数，按照如下流程操作可快速创建一个待办应用并体验待办事项的增删改查功能。注意：本模板仅作为示例提供，待办事项数据实际存储在实例内存中，不作为持久化存储。</p>\n<ol>\n<li>登录云函数 SCF 控制台；</li>\n<li>云函数控制台地址：<a href=\"https://console.cloud.tencent.com/scf\">https://console.cloud.tencent.com/scf</a></li>\n<li>选择函数服务 -> 新建 -> 模板创建 -> 搜索关键词<code class=\"language-text\">springboot</code>，在查询结果中选择「SpringBoot 待办应用」并单击「下一步」，保持默认配置，完成函数创建。</li>\n</ol>\n<img src=\"https://qcloudimg.tencent-cloud.cn/raw/77c32b86948dd322c896b10dda7aab29.png\" width=\"700\">\n<ol start=\"3\">\n<li>切换到「函数代码」页签，按照如下流程操作，通过测试模板发起模拟请求体验待办应用增删改查功能：</li>\n<li>查询待办列表：</li>\n</ol>\n<p>请求方式选择 GET，path 填写 /todos，点击「测试」后，在响应 Body 中可以查看到当前的待办事项。</p>\n<img src=\"https://qcloudimg.tencent-cloud.cn/raw/2aab8fe20d41811eca39b2f35fe7bddd.png\" width=\"700\">\n<ul>\n<li>增加待办事项：</li>\n</ul>\n<p>请求方式选择 POST，path 填写/todos，body 填写{\"key\":\"3\",\"content\":\"Third todo\",\"done\":false}，点击「测试」增加一个待办事项。</p>\n<img src=\"https://qcloudimg.tencent-cloud.cn/raw/2aab8fe20d41811eca39b2f35fe7bddd.png\" width=\"700\">\n<ul>\n<li>删除待办事项：</li>\n</ul>\n<p>请求方式选择 DELETE，以删除 key 为 2 的待办事项为例，path 填写 /todos/2，点击「测试」。</p>\n<img src=\"https://qcloudimg.tencent-cloud.cn/raw/4254f7b614e0c92d9e6ab8018e9b20f9.png\" width=\"700\">\n<ul>\n<li>修改待办事项：</li>\n</ul>\n<p>请求方式选择 PUT，以将 key 为 3 的待办事项由未完成改为完成为例，path 填写 /todos/3，body 填写 {\"key\":\"3\",\"content\":\"Third todo\",\"done\":true}，点击「测试」。</p>\n<img src=\"https://qcloudimg.tencent-cloud.cn/raw/627657aa8983da727cbf4f2d3ec3072e.png\" width=\"700\">\n<p><strong>03. 代码示例</strong></p>\n<p>在 「02.创建待办应用」的第二步模板选择页面，点击模板卡片右上角的「查看详情」，在展开的页面中单击「点击下载模板函数」即可获取模板函数源码。</p>\n<p>原生 SpringBoot 项目迁移到 Web 几乎没有改造成本，只需要：</p>\n<ol>\n<li>确保 Spring 监听端口为 9000（SCF Web 函数指定监听端口）</li>\n</ol>\n<img src=\"https://qcloudimg.tencent-cloud.cn/raw/3abc1423c4df3f9cd7304af5c3383822.png\" width=\"700\">\n<ol start=\"2\">\n<li>编译 JAR 包</li>\n</ol>\n<p>下载代码之后，在目录<code class=\"language-text\">Webfunc-Java8-SpringBoot</code>下运行编译命令：</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">gradle build</code></pre></div>\n<p>编译完成后可在<code class=\"language-text\">build/libs</code>目录下获取到打包完成的 jar 包，选择后缀为<code class=\"language-text\">-all</code>的 jar 包。</p>\n<ol start=\"3\">\n<li>准备一个可执行文件 <code class=\"language-text\">scf_bootstrap</code> 用于启动 Web Server，文件内容可参考下文：</li>\n</ol>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">#!/bin/bash/var/lang/java8/bin/java -Dserver.port=9000 -jar scf-springboot-java8-0.0.2-SNAPSHOT-all.jar</code></pre></div>\n<p>注意：在 scf<em>bootstrap 文件所在目录执行`chmod 755 scf</em>bootstrap`来保证scf_bootstrap 文件具有可执行权限。</p>\n<ol start=\"4\">\n<li>将 <code class=\"language-text\">scf_bootstrap</code>文件与生成的 jar 包一起打包为 zip 部署到云函数。</li>\n<li>登录云函数 SCF 控制台；</li>\n<li>\n<ul>\n<li>云函数控制台地址：<a href=\"https://console.cloud.tencent.com/scf\">https://console.cloud.tencent.com/scf</a></li>\n</ul>\n</li>\n<li>选择函数服务->新建->自定义创建；</li>\n<li>函数类型：web 函数</li>\n<li>运行环境：Java8</li>\n<li>提交方法：本地上传 zip 包</li>\n<li>单击上传选择打包好的 zip 文件</li>\n<li>其他保持默认配置，单击「完成」即可完成函数创建。</li>\n</ol>\n<img src=\"https://qcloudimg.tencent-cloud.cn/raw/3945aa8512d78c7e80a0ebaf9609a605.png\" width=\"700\">\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-11-18-springboot-scf/#01-%E5%89%8D%E6%8F%90%E6%9D%A1%E4%BB%B6\">01. 前提条件</a></li>\n<li><a href=\"/best-practice/2021-11-18-springboot-scf/#02-%E5%88%9B%E5%BB%BA%E5%BE%85%E5%8A%9E%E5%BA%94%E7%94%A8\">02. 创建待办应用</a></li>\n</ul>"},"previousBlog":{"id":"3d5372ca-347f-5bcd-9214-91d1cc3f006b","frontmatter":{"thumbnail":"https://qcloudimg.tencent-cloud.cn/raw/c084af1f92e17639fd79c78ef0485ce5.jpg","authors":["刘传传"],"categories":["best-practice"],"date":"2021-12-02T00:00:00.000Z","title":"花小钱办大事：云函数+云开发撬动央视晚会的电商大促弹性架构实践","description":"基于云函数和云开发的小程序应用实践","authorslink":null,"translators":null,"translatorslink":null,"tags":["Serverless","云函数"],"keywords":null,"outdated":null},"wordCount":{"words":279,"sentences":51,"paragraphs":51},"fileAbsolutePath":"/opt/build/repo/content/best-practice/2021-12-02-tcb-scf.md","fields":{"slug":"/best-practice/2021-12-02-tcb-scf/","keywords":["serverless","函数计算","云函数","云原生","Serverless","电商","大促","开发","函数","并发","程序","活动","客户"]}},"nextBlog":{"id":"978ee7ac-8c44-57c9-b367-607d45c92c15","frontmatter":{"thumbnail":"https://qcloudimg.tencent-cloud.cn/raw/8e6a2b65fc9ed0e36db1f77bed64e999.jpg","authors":["腾讯云事件总线"],"categories":["best-practice"],"date":"2021-11-16T00:00:00.000Z","title":"「EB + iPaaS + X 计划」系列一：告警信息实时推送","description":"快速构建各种业务场景，帮您以配置化的方式轻松实现无服务器事件驱动架构的搭建。","authorslink":null,"translators":null,"translatorslink":null,"tags":["Serverless","云函数"],"keywords":null,"outdated":null},"wordCount":{"words":390,"sentences":81,"paragraphs":81},"fileAbsolutePath":"/opt/build/repo/content/best-practice/2021-11-16-eb-ipaas.md","fields":{"slug":"/best-practice/2021-11-16-eb-ipaas/","keywords":["python","serverless","无服务器","云函数","tencent","width"]}}},"pageContext":{"isCreatedByStatefulCreatePages":false,"blogId":"f228879d-ecaf-5cb5-8ea0-e3f7d8bba8f5","previousBlogId":"3d5372ca-347f-5bcd-9214-91d1cc3f006b","nextBlogId":"978ee7ac-8c44-57c9-b367-607d45c92c15"}}}