initial import
This commit is contained in:
28
example/graceful/etcd/api/Dockerfile
Normal file
28
example/graceful/etcd/api/Dockerfile
Normal file
@@ -0,0 +1,28 @@
|
||||
FROM golang:alpine AS builder
|
||||
|
||||
LABEL stage=gobuilder
|
||||
|
||||
ENV CGO_ENABLED 0
|
||||
ENV GOOS linux
|
||||
|
||||
RUN apk update
|
||||
RUN apk add upx
|
||||
|
||||
WORKDIR $GOPATH/src/zero
|
||||
COPY . .
|
||||
RUN go build -ldflags="-s -w" -o /app/graceful example/graceful/etcd/api/graceful.go
|
||||
RUN upx /app/graceful
|
||||
|
||||
|
||||
FROM alpine
|
||||
|
||||
RUN apk update --no-cache
|
||||
RUN apk add --no-cache ca-certificates
|
||||
RUN apk add --no-cache tzdata
|
||||
ENV TZ Asia/Shanghai
|
||||
|
||||
WORKDIR /app
|
||||
COPY --from=builder /app/graceful /app/graceful
|
||||
COPY example/graceful/etcd/api/etc/graceful-api.json /app/etc/config.json
|
||||
|
||||
CMD ["./graceful", "-f", "etc/config.json"]
|
||||
13
example/graceful/etcd/api/Makefile
Normal file
13
example/graceful/etcd/api/Makefile
Normal file
@@ -0,0 +1,13 @@
|
||||
version := v$(shell /bin/date "+%y%m%d%H%M%S")
|
||||
|
||||
build:
|
||||
docker pull alpine
|
||||
docker pull golang:alpine
|
||||
cd $(GOPATH)/src/zero && docker build -t registry.cn-hangzhou.aliyuncs.com/xapp/graceful:$(version) . -f example/graceful/etcd/api/Dockerfile
|
||||
docker image prune --filter label=stage=gobuilder -f
|
||||
|
||||
push: build
|
||||
docker push registry.cn-hangzhou.aliyuncs.com/xapp/graceful:$(version)
|
||||
|
||||
deploy: push
|
||||
kubectl -n kevin set image deployment/graceful-deployment graceful=registry-vpc.cn-hangzhou.aliyuncs.com/xapp/graceful:$(version)
|
||||
11
example/graceful/etcd/api/config/config.go
Normal file
11
example/graceful/etcd/api/config/config.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"zero/ngin"
|
||||
"zero/rpcx"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
ngin.NgConf
|
||||
Rpc rpcx.RpcClientConf
|
||||
}
|
||||
12
example/graceful/etcd/api/etc/graceful-api.json
Normal file
12
example/graceful/etcd/api/etc/graceful-api.json
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"Name": "graceful-api",
|
||||
"Host": "0.0.0.0",
|
||||
"Port": 8888,
|
||||
"MaxConns": 1000000,
|
||||
"Rpc": {
|
||||
"Etcd": {
|
||||
"Hosts": ["etcd.discov:2379"],
|
||||
"Key": "rpcx"
|
||||
}
|
||||
}
|
||||
}
|
||||
11
example/graceful/etcd/api/graceful.api
Normal file
11
example/graceful/etcd/api/graceful.api
Normal file
@@ -0,0 +1,11 @@
|
||||
type Response {
|
||||
Host string `json:"host"`
|
||||
Time int64 `json:"time"`
|
||||
}
|
||||
|
||||
service graceful-api {
|
||||
@server(
|
||||
handler: GracefulHandler
|
||||
)
|
||||
get /api/graceful() returns(Response)
|
||||
}
|
||||
32
example/graceful/etcd/api/graceful.go
Normal file
32
example/graceful/etcd/api/graceful.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
|
||||
"zero/core/conf"
|
||||
"zero/example/graceful/etcd/api/config"
|
||||
"zero/example/graceful/etcd/api/handler"
|
||||
"zero/example/graceful/etcd/api/svc"
|
||||
"zero/ngin"
|
||||
"zero/rpcx"
|
||||
)
|
||||
|
||||
var configFile = flag.String("f", "etc/graceful-api.json", "the config file")
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
var c config.Config
|
||||
conf.MustLoad(*configFile, &c)
|
||||
|
||||
client := rpcx.MustNewClient(c.Rpc)
|
||||
ctx := &svc.ServiceContext{
|
||||
Client: client,
|
||||
}
|
||||
|
||||
engine := ngin.MustNewEngine(c.NgConf)
|
||||
defer engine.Stop()
|
||||
|
||||
handler.RegisterHandlers(engine, ctx)
|
||||
engine.Start()
|
||||
}
|
||||
42
example/graceful/etcd/api/graceful.yaml
Normal file
42
example/graceful/etcd/api/graceful.yaml
Normal file
@@ -0,0 +1,42 @@
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: graceful
|
||||
namespace: kevin
|
||||
spec:
|
||||
selector:
|
||||
app: graceful
|
||||
type: ClusterIP
|
||||
ports:
|
||||
- name: graceful-port
|
||||
port: 3333
|
||||
targetPort: 8888
|
||||
|
||||
---
|
||||
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: graceful-deployment
|
||||
namespace: kevin
|
||||
labels:
|
||||
app: graceful
|
||||
spec:
|
||||
replicas: 3
|
||||
selector:
|
||||
matchLabels:
|
||||
app: graceful
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: graceful
|
||||
spec:
|
||||
containers:
|
||||
- name: graceful
|
||||
image: registry-vpc.cn-hangzhou.aliyuncs.com/xapp/graceful:v191031145905
|
||||
imagePullPolicy: Always
|
||||
ports:
|
||||
- containerPort: 8888
|
||||
imagePullSecrets:
|
||||
- name: aliyun
|
||||
|
||||
49
example/graceful/etcd/api/handler/gracefulhandler.go
Normal file
49
example/graceful/etcd/api/handler/gracefulhandler.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"zero/core/executors"
|
||||
"zero/core/httpx"
|
||||
"zero/core/logx"
|
||||
"zero/example/graceful/etcd/api/svc"
|
||||
"zero/example/graceful/etcd/api/types"
|
||||
"zero/example/graceful/etcd/rpc/graceful"
|
||||
)
|
||||
|
||||
func gracefulHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
logger := executors.NewLessExecutor(time.Second)
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var resp types.Response
|
||||
|
||||
conn, ok := ctx.Client.Next()
|
||||
if !ok {
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
host, err := os.Hostname()
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusNotImplemented), http.StatusNotImplemented)
|
||||
return
|
||||
}
|
||||
|
||||
client := graceful.NewGraceServiceClient(conn)
|
||||
rp, err := client.Grace(context.Background(), &graceful.Request{From: host})
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
http.Error(w, http.StatusText(http.StatusBadGateway), http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
|
||||
resp.Host = rp.Host
|
||||
logger.DoOrDiscard(func() {
|
||||
fmt.Printf("%s from host: %s\n", time.Now().Format("15:04:05"), rp.Host)
|
||||
})
|
||||
httpx.OkJson(w, resp)
|
||||
}
|
||||
}
|
||||
19
example/graceful/etcd/api/handler/routes.go
Normal file
19
example/graceful/etcd/api/handler/routes.go
Normal file
@@ -0,0 +1,19 @@
|
||||
// DO NOT EDIT, generated by goctl
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"zero/example/graceful/etcd/api/svc"
|
||||
"zero/ngin"
|
||||
)
|
||||
|
||||
func RegisterHandlers(engine *ngin.Engine, ctx *svc.ServiceContext) {
|
||||
engine.AddRoutes([]ngin.Route{
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/api/graceful",
|
||||
Handler: gracefulHandler(ctx),
|
||||
},
|
||||
})
|
||||
}
|
||||
7
example/graceful/etcd/api/svc/servicecontext.go
Normal file
7
example/graceful/etcd/api/svc/servicecontext.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package svc
|
||||
|
||||
import "zero/rpcx"
|
||||
|
||||
type ServiceContext struct {
|
||||
Client *rpcx.RpcClient
|
||||
}
|
||||
7
example/graceful/etcd/api/types/types.go
Normal file
7
example/graceful/etcd/api/types/types.go
Normal file
@@ -0,0 +1,7 @@
|
||||
// DO NOT EDIT, generated by goctl
|
||||
package types
|
||||
|
||||
type Response struct {
|
||||
Host string `json:"host"`
|
||||
Time int64 `json:"time"`
|
||||
}
|
||||
Reference in New Issue
Block a user