zoukankan      html  css  js  c++  java
  • 使用 serverless 在腾讯云部署第一个函数

    Serverless 是各大云服务商提供出来的一种无服务的计算资源。为什么叫无服务呢,因为如果你使用 serverless,你只需要关注应用层,而无需关心底层基础设施,无需运维。简而言之,serverless 并不是真的无服务,而是关于有服务的不归你管,云服务商帮你搞定,比如 GoogleAWS 或者 TencentCloud

    关注点分离,好呀好!有了 serverless 以后只需要也只能关心业务了,这也不知是喜是忧。但你也无需过于担心,这是对已有并且成熟的开发模式的挑战,解决痛点有限,因此很多团队对于替换为 serverless 也动力不足。

    但是我仍然建议你学习 serverless,毕竟各大云厂商对于 serverless 有很多免费额度可以让你薅羊毛,对于个人开发者利好。

    Serverless Framework

    serverless 是基于各大云服务商的产品,每一个云厂商对于 serverless 都有一套自己的 API。为了能够兼容这些 API,为了让你的代码 Write Once, Run Everywhere,于是 serverless framework 诞生了。

    通常认为 serverless = faas + baas,然而 serverless framework 只兼容到了 faas,对于 baas,如各家提供的数据存储服务,要做到兼容还是很难。

    快速开始

    serverless framework 与腾讯云的云函数来开始一个 hello world 吧!

    $ npm install -g serverless
    
    $ mkdir hello
    
    $ cd hello
    
    $ serverless create --template tencent-nodejs --name hello
    
    Serverless: Generating boilerplate...
    _______                             __
    |   _   .-----.----.--.--.-----.----|  .-----.-----.-----.
    |   |___|  -__|   _|  |  |  -__|   _|  |  -__|__ --|__ --|
    |____   |_____|__|  \___/|_____|__| |__|_____|_____|_____|
    |   |   |             The Serverless Application Framework
    |       |                           serverless.com, v1.67.0
    -------'
    
    Serverless: Successfully generated boilerplate for template: "tencent-nodejs"
    

    此时在 hello 目录自动生成了关于 serverless 在腾讯云的 hello, world 版。由于缺少关于腾讯云的 plugin 需要首先装包

    $ npm i
    

    简述

    serverless.yaml

    serverless.yamlserverless framework 的核心,是一个 sls 服务的资源配置文件。如果把 sls 等同于 faas + baas,那么 faasbaas 的配置都在这里。

    service: hello
    
    # 云厂商的信息,如 aws/google/aliyun/tencent
    provider:
      name: tencent
      runtime: Nodejs8.9    # Nodejs 版本号
      credentials: ~/credentials
    
    plugins:
      - serverless-tencent-scf  # 腾讯云对 sls 的适配
    
    functions:
      hello_world:          # 函数名
        handler: index.main_handler # 该函数所调用的函数
    

    index.js

    exports.main_handler = (event, context, callback) => {
      callback(null, 'Hello World');
    };
    

    index.js 中是 faas 中的核心,function。在 callback 中来回调你所需的数据。

    部署

    使用 sls deploy 打包资源并部署到腾讯云,此时需要你在腾讯云的凭证信息。你可以通过与腾讯云绑定的微信扫码授权,相比其他厂商需要手动维护凭证信息,还是很方便的。

    # 其中 sls 是 serverless 的简写
    $ sls deploy
    Serverless: Packaging service...
    Serverless: Excluding development dependencies...
    Serverless: Uploading service package to cos[sls-cloudfunction-ap-guangzhou]. hello-dev-KamjFZ-2020-04-15-21-47-11.zip
    Serverless: Uploaded package successful /Users/xiange/Documents/hello/.serverless/hello.zip
    Serverless: Creating function hello-dev-hello_world
    Serverless: Updating code...
    Serverless: Updating configure...
    Serverless: Created function hello-dev-hello_world
    Serverless: Setting tags for function hello-dev-hello_world
    Serverless: Creating trigger for function hello-dev-hello_world
    Serverless: Deployed function hello-dev-hello_world successful
    Serverless: Service Information
    
    service: hello
    stage: dev
    region: ap-guangzhou
    stack: hello-dev
    resources: 1
    functions:   hello_world: hello-dev-hello_world
    
    # 如果需要部署到生产环境
    $ sls deploy --stage  production
    

    稍等一分钟,就可以看到部署成功的信息。

    函数调用

    本地函数可以很简单地通过调用函数名来执行,serverless 也可以通过 sls invoke 来调用函数。

    $ sls invoke --function hello_world
    
    Serverless:
    
    "Hello World"
    
    ----------
    Log:
    START RequestId: 69ffc57f-0afb-471b-865d-c7289e16f2ac
    Event RequestId: 69ffc57f-0afb-471b-865d-c7289e16f2ac
    
    END RequestId: 69ffc57f-0afb-471b-865d-c7289e16f2ac
    Report RequestId: 69ffc57f-0afb-471b-865d-c7289e16f2ac Duration:64ms Memory:128MB MemUsage:21.8125MB
    

    日志与监控

    serverless 号称 noops,很大程度上是由于少了 logmetrics 的基础设施搭建。使用 sls logssls metrics 可以获取相关信息,但是丰富度及可定制化就完全不如 kubernetes 运维了。

    $ sls logs
    Serverless: {
      "FunctionName": "hello-dev-hello_world",
      "RetMsg": ""Hello World"",
      "RequestId": "fc72271f-eede-4dbb-8315-f24045597db7",
      "StartTime": "2020-04-15 21:48:55",
      "RetCode": 0,
      "InvokeFinished": 1,
      "Duration": 1,
      "BillDuration": 1,
      "MemUsage": 74870780,
      "Log": "START RequestId: fc72271f-eede-4dbb-8315-f24045597db7
    Event RequestId: fc72271f-eede-4dbb-8315-f24045597db7
    2020-04-15T13:48:55.344Z	fc72271f-eede-4dbb-8315-f24045597db7	{}
     
    END RequestId: fc72271f-eede-4dbb-8315-f24045597db7
    Report RequestId: fc72271f-eede-4dbb-8315-f24045597db7 Duration:1ms Memory:128MB MemUsage:71.402344MB",
      "Level": "",
      "Source": ""
    }
    Serverless: {
      "FunctionName": "hello-dev-hello_world",
      "RetMsg": ""Hello World"",
      "RequestId": "62b5760e-5545-4316-bef6-423d4b568396",
      "StartTime": "2020-04-15 21:48:45",
      "RetCode": 0,
      "InvokeFinished": 1,
      "Duration": 2,
      "BillDuration": 2,
      "MemUsage": 74870780,
      "Log": "START RequestId: 62b5760e-5545-4316-bef6-423d4b568396
    Event RequestId: 62b5760e-5545-4316-bef6-423d4b568396
    2020-04-15T13:48:47.995Z	62b5760e-5545-4316-bef6-423d4b568396	{}
     
    END RequestId: 62b5760e-5545-4316-bef6-423d4b568396
    Report RequestId: 62b5760e-5545-4316-bef6-423d4b568396 Duration:2ms Memory:128MB MemUsage:71.402344MB",
      "Level": "",
      "Source": ""
    }
    
    $ sls metrics
    Serverless: Service wide metrics
    2020-04-14 22:09:45 - 2020-04-15 22:09:45
    
    Service:
      Invocations: 5
      Outflows: 0
      Errors: 0
      Duration(avg.): 7.3 ms
    
    Functions:
      hello-dev-hello_world:
        Invocations: 5
        Outflows: 0
        Errors: 0
        Duration(avg.): 7.3 ms
    

    小结

    从本篇文章,可以大概知道如何在腾讯云初建一个 serverless 函数,并且知道了如何执行并且调用它。而关于 sls 的原理,如服务编排以及容器化,可以再继续深入学习。

    One More Thing

    3 秒你能做什么?喝一口水,看一封邮件,还是 —— 部署一个完整的 Serverless 应用?

    复制链接至 PC 浏览器访问:https://serverless.cloud.tencent.com/deploy/express

    3 秒极速部署,立即体验史上最快的 Serverless HTTP 实战开发!

    传送门:

    欢迎访问:Serverless 中文网,您可以在 最佳实践 里体验更多关于 Serverless 应用的开发!


    推荐阅读:《Serverless 架构:从原理、设计到项目实战》

  • 相关阅读:
    如何在DOS中枚举PCI设备
    [Color]深入学习YCbCr色彩模型
    [Imm]Imm API学习笔记——输入法属性
    VBE_INFO(获取VBE信息)
    用VB写高效的图像处理程序 V2.0(2006524)
    ANSI环境下支持多语言输入的单行文本编辑器 V0.01
    分析外星人计算Pi的程序
    位运算模块mBit.bas
    [FileFormat]用VB写的高速GIF、JPEG 编码/解码 程序
    ANTLR笔记3 ANTLRWorks
  • 原文地址:https://www.cnblogs.com/serverlesscloud/p/13182790.html
Copyright © 2011-2022 走看看