this post will show you how to deploy a Sendtweet golang function in a OpenFaas Plaform.
installation
Openfaas supports Docker swarm, Docker Playground, but here i used Kubernetes environment as example
after you deployed your kubernetes, you can start to deploy Openfaas
Openfaas CLI
faas-cli
used to manage all of your functions
> curl -sL https://cli.openfaas.com | sudo sh
> faas-cli version
___ _____ ____
/ _ \ _ __ ___ _ __ | ___|_ _ __ _/ ___|
| | | | '_ \ / _ \ '_ \| |_ / _` |/ _` \___ \
| |_| | |_) | __/ | | | _| (_| | (_| |___) |
\___/| .__/ \___|_| |_|_| \__,_|\__,_|____/
|_|
CLI:
commit: xxxxxxxxxxxxxx
version: 0.8.3
Deploy OpenFaas
> git clone https://github.com/openfaas/faas-netes
> kubectl apply -f ./faas-netes/yaml
> kubectl get pods -n openfaas
NAME READY STATUS RESTARTS AGE
alertmanager-5df696f54d-ts8jd 1/1 Running 3 1h
gateway-7f4dc7ff46-xzwc5 2/2 Running 12 1h
nats-765f98bd59-n9g85 1/1 Running 3 1h
prometheus-5dfc76b657-w9q9f 1/1 Running 3 1h
queue-worker-7b48d787bb-fhbd8 1/1 Running 10 1h
if there’s nothing wrong, open http://localhost:31112/ you will see the OpenFaas Portal.
Deploy Grafana monitor
> kubectl -n openfaas run --image=stefanprodan/faas-grafana:4.6.3 --port=3000 grafana
> kubectl get pods -n openfaas grafana-595c55957b-7gc54
NAME READY STATUS RESTARTS AGE
grafana-595c55957b-7gc54 1/1 Running 1 26h
> kubectl -n openfaas expose deployment grafana --type=NodePort --name=grafana
> echo http://localhost:$(kubectl -n openfaas get svc grafana -o jsonpath="{.spec.ports[0].nodePort}")
http://localhost:31380 # grafana panel
user/pass: admin
Golang function
faas-cli
can generates many different languages template
> faas-cli new --lang go sendtweet
Folder: sendtweet created.
___ ___ ___
/ _ \ _ __ ___ _ __ | ___|_ _ __ _/ ___|
| | | | '_ \ / _ \ '_ \| |_ / _` |/ _` \___ \
| |_| | |_) | __/ | | | _| (_| | (_| |___) |
\___/| .__/ \___|_| |_|_| \__,_|\__,_|___ /
|_|
Function created in folder: sendtweet
Stack file written: sendtweet.yml
so i wrote a function that use to send tweet
package function
import (
"encoding/json"
"errors"
"github.com/dghubble/go-twitter/twitter"
oa1 "github.com/dghubble/oauth1"
)
var (
errValNotExists = errors.New("Consumer key/secret and Access token/secret required")
errInvalidData = errors.New("Invalid request data")
)
type req struct {
ConsumerKey string `json:"consumer_key"`
ConsumerSecret string `json:"consumer_secret"`
AccessToken string `json:"access_token"`
AccessSecret string `json:"access_secret"`
Msg string `json:"msg"`
}
func (r *req) getClient() (*twitter.Client, error) {
if r.ConsumerKey == "" || r.ConsumerSecret == "" || r.AccessToken == "" || r.AccessSecret == "" {
return nil, errValNotExists
}
config := oa1.NewConfig(r.ConsumerKey, r.ConsumerSecret)
token := oa1.NewToken(r.AccessToken, r.AccessSecret)
httpClient := config.Client(oa1.NoContext, token)
return twitter.NewClient(httpClient), nil
}
// Handle a serverless request
func Handle(r []byte) string {
var request = &req{}
if ok := json.Unmarshal(r, request); ok != nil {
return errInvalidData.Error()
}
c, err := request.getClient()
if err != nil {
return err.Error()
}
_, _, err = c.Statuses.Update(request.Msg, nil)
if err != nil {
return err.Error()
}
return "succssed"
}
deploy it with dependency
> dep init -v # go get -u github.com/golang/dep/cmd/dep
> faas-cli build -f sendtweet.yml
> faas-cli deploy -f sendtweet.yml
> kubectl get pods -n openfaas-fn # `openfaas-fn` all of your functions will running on this namespace
NAME READY STATUS RESTARTS AGE
sendtweet-89d4f59f6-jwtjp 1/1 Running 1 1h
Invokes
for synchronized invoke follow http://{domainname}:{port}/function/{function name}
to invoke your functions
for synchronized invoke, you have to set a callback url in your request headers, then follow http://{domainname}:{port}/async-function/{function name}
to starts your invoke
POST /async-function/sendtweet HTTP/1.1
Host: localhost:31112
X-Callback-Url: http://localhost:3001/callback
{
"consumer_key": "xxxxx",
"consumer_secret": "xxxx",
"access_token": "xxxxx",
"access_secret": "xxxx",
"msg": "Hello for testing asynchronous task"
}
左边tab的动态线条蛮有意思的,鼠标放到上面还有反馈
这个是什么东西弄出来的啊
Canvas-Nest.js 希望能帮到你
文章不错支持一下吧
文章不错非常喜欢
Hello friends, fastidious post and nice arguments commented at this place, I am genuinely enjoying by these.
进错地儿了 完全看不懂