到阿里云云函数

阿里云云函数简介

阿里云的 serverless 产品 https://fcnext.console.aliyun.com/overview

部署到阿里云云函数

如果要将应用程序部署到阿里云云函数(Function Compute),需要进行以下步骤:

1. 创建函数计算项目

首先,确保已经安装了阿里云函数计算的 CLI 工具 fun

windows

yarn global add @serverless-devs/s

linux or macos

curl -o- -L http://cli.so/install.sh | bash

执行以下命令,验证是否安装成功。

s -v

2. 配置账号

>s config add
? Please select a provider:
> Alibaba Cloud (alibaba)
  AWS (aws)
  Azure (azure)
  Baidu Cloud (baidu)
  Google Cloud (google)
  Huawei Cloud (huawei)
  Tencent Cloud (tencent)

在项目根目录创建 Funfile,定义如何构建和部署应用:

# 使用官方的 Java8 runtime
RUNTIME java8

# 安装依赖
RUN apt-get update && apt-get install -y maven

# 复制项目文件
COPY . /code

# 设置工作目录
WORKDIR /code

# 打包应用程序
RUN mvn clean package -DskipTests -Pproduction

3. 创建 template.yml

在项目根目录创建 template.yml 文件,定义函数计算的服务和函数:

ROH: "2018-04-03"
Transform: "Aliyun::Serverless::Transform::Function::Service"
Resources:
  TioBootWebHelloService:
    Type: "Aliyun::Serverless::Service"
    Properties:
      Description: "Tio Boot Web Hello Service"
    TioBootWebHelloFunction:
      Type: "Aliyun::Serverless::Function"
      Properties:
        Handler: "example.App::handleRequest"
        Runtime: "java8"
        CodeUri: "./target/tio-boot-web-hello-1.0.0.jar"
        MemorySize: 1024
        Timeout: 30
      Events:
        httpTrigger:
          Type: "HTTP"
          Properties:
            AuthType: "ANONYMOUS"
            Methods: ["GET", "POST"]

4. 部署到函数计算

使用 s deploy -y 命令将应用部署到阿里云函数计算:

s deploy -y

这一步包括:

  • 构建环境:根据 Funfile 构建运行环境。
  • 打包应用:将应用打包并上传到函数计算。
  • 创建资源:根据 template.yml 创建服务和函数资源。
  • 部署应用:将打包好的应用部署到阿里云函数计算中。