IT Panda Blog

Life is fantastic


  • Home

  • Tags

  • Categories

  • Archives

OpenFaaS 101 - 3:Hello World

Posted on 2020-05-03 Edited on 2020-05-04 In serverless , faas , openfaas

第一个列子,当然是 Hello World 了… 下面使用 Python, Go, Java 分别实现…

首先准备好目录:

1
2
3
4
└── helloworld
├── go
├── java
└── python

Python

使用CLI创建 function faas-cli new --lang python hello-world, 会自动生成以下文件

1
2
3
4
5
6
7
8
9
└── helloworld
├── go
├── java
└── python
├── hello-world
│ ├── handler.py
│ └── requirements.txt #如果有dependency的话,就放进这里
├── hello-world.yml
└── template #会有很多template,暂时先ignore

其中,handler.py 就是处理 request的 handler,这里我们先实现 hello world的逻辑

1
2
3
4
def handle(req):
resp='Hello Rex, ' + req
print(resp)
return resp

之后使用配置文件hello-world.yml来build func,

1
2
3
4
5
6
7
8
9
version: 1.0
provider:
name: openfaas #唯一的provider,value只能为openfaas
gateway: http://<your-ip>:31112
functions:
hello-world:
lang: python
handler: ./hello-world
image: openfaas-hello-world:latest

运行faas-cli build -f ./hello-world.yml之后,openfaas就将这个funct打包成一个新的image

1
2
$ docker image ls | grep hello
openfaas-hello-world latest 3 minutes ago 87.1MB

接下来就是部署和测试了,faas-cli deploy --image openfaas-hello-world --name hello-world-python

当然如果需要将新func upload到 Registry/Artifactory上,faas-cli push -f ./hello-world.yml,这之前需要配置secret

Golang

类似的,使用faas-cli new --lang go hello-world创建新function;同样的,openfaas会创建一下几个文件

1
2
3
4
5
6
└── helloworld
├── go
│ ├── hello-world
│ │ └── handler.go
│ ├── hello-world.yml
│ └── template

添加handler实现

1
2
3
4
5
6
7
8
9
package function

import (
"fmt"
)

func Handle(req []byte) string {
return fmt.Sprintf("Hello Rex: %s", string(req))
}

部署并测试function

1
2
faas-cli build -f ./hello-world.yml
faas-cli deploy --image openfaas-hello-world-go --name hello-world-go

Java

同样的套路,只不过java有几个版本可以选,java8还是11之类的,faas-cli new --lang java8 hello-world

Java自动生成的目录结构较Python和Go有很大不同,起码使用gradlebuild

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
java
├── hello-world
│ ├── build.gradle
│ ├── gradle
│ │ └── wrapper
│ │ ├── gradle-wrapper.jar
│ │ └── gradle-wrapper.properties
│ ├── settings.gradle
│ └── src
│ ├── main
│ │ └── java
│ │ └── com
│ │ └── openfaas
│ │ └── function
│ │ └── Handler.java
│ └── test
│ └── java
│ └── HandlerTest.java
├── hello-world.yml
└── template

直奔Handler,java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.openfaas.function;

import com.openfaas.model.IHandler;
import com.openfaas.model.IResponse;
import com.openfaas.model.IRequest;
import com.openfaas.model.Response;

public class Handler implements com.openfaas.model.IHandler {

public IResponse Handle(IRequest req) {
Response res = new Response();
res.setBody("Hello Rex, " + req.getBody());
return res;
}
}

之后就是部署并测试

1
2
faas-cli build -f ./hello-world.yml
faas-cli deploy --image openfaas-hello-world-java8 --name hello-world-java

这样,基本算是hello world级别的了解了openfaas;那下一篇就深入的了解下openfaas

最后,Java主语言的我,在尝试OpenFaaS的HelloWorld的时候,很是不爽…

1
2
3
4
$ docker image ls | grep hello
openfaas-hello-world-go latest 25.1MB
openfaas-hello-world-python latest 87.1MB
openfaas-hello-world-java8 latest 141MB
serverless faas service mesh openfaas cloud computing
OpenFaaS 101 - 2 : 安装 OpenFaaS 以及第一个 Function
OpenFaaS 101 - 4:Design & Architecture
  • Table of Contents
  • Overview
Rex

Rex

25 posts
26 categories
49 tags
Links
  • GitHub
  1. 1. Python
  2. 2. Golang
  3. 3. Java
© 2019 – 2020 作者拥有版权,转载请注明出处