initial import
This commit is contained in:
26
example/graceful/dns/api/Dockerfile
Normal file
26
example/graceful/dns/api/Dockerfile
Normal file
@@ -0,0 +1,26 @@
|
||||
FROM golang:1.13 AS builder
|
||||
|
||||
ENV CGO_ENABLED 0
|
||||
ENV GOOS linux
|
||||
|
||||
RUN apt-get update
|
||||
RUN apt-get install -y apt-utils upx
|
||||
|
||||
WORKDIR $GOPATH/src/zero
|
||||
COPY . .
|
||||
RUN go build -ldflags="-s -w" -o /app/graceful example/graceful/dns/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/dns/api/etc/graceful-api.json /app/etc/config.json
|
||||
|
||||
CMD ["./graceful", "-f", "etc/config.json"]
|
||||
11
example/graceful/dns/api/Makefile
Normal file
11
example/graceful/dns/api/Makefile
Normal file
@@ -0,0 +1,11 @@
|
||||
version := v$(shell /bin/date "+%y%m%d%H%M%S")
|
||||
|
||||
build:
|
||||
docker pull alpine
|
||||
cd $(GOPATH)/src/zero && docker build -t registry.cn-hangzhou.aliyuncs.com/xapp/graceful:$(version) . -f example/graceful/dns/api/Dockerfile
|
||||
|
||||
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/dns/api/config/config.go
Normal file
11
example/graceful/dns/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
|
||||
}
|
||||
9
example/graceful/dns/api/etc/graceful-api.json
Normal file
9
example/graceful/dns/api/etc/graceful-api.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"Name": "graceful-api",
|
||||
"Host": "0.0.0.0",
|
||||
"Port": 8888,
|
||||
"MaxConns": 1000000,
|
||||
"Rpc": {
|
||||
"Server": "dns:///gracefulrpc:3456"
|
||||
}
|
||||
}
|
||||
11
example/graceful/dns/api/graceful.api
Normal file
11
example/graceful/dns/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/dns/api/graceful.go
Normal file
32
example/graceful/dns/api/graceful.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
|
||||
"zero/core/conf"
|
||||
"zero/example/graceful/dns/api/config"
|
||||
"zero/example/graceful/dns/api/handler"
|
||||
"zero/example/graceful/dns/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/dns/api/graceful.yaml
Normal file
42
example/graceful/dns/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:v191022133857
|
||||
imagePullPolicy: Always
|
||||
ports:
|
||||
- containerPort: 8888
|
||||
imagePullSecrets:
|
||||
- name: aliyun
|
||||
|
||||
49
example/graceful/dns/api/handler/gracefulhandler.go
Normal file
49
example/graceful/dns/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/dns/api/svc"
|
||||
"zero/example/graceful/dns/api/types"
|
||||
"zero/example/graceful/dns/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/dns/api/handler/routes.go
Normal file
19
example/graceful/dns/api/handler/routes.go
Normal file
@@ -0,0 +1,19 @@
|
||||
// DO NOT EDIT, generated by goctl
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"zero/example/graceful/dns/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/dns/api/svc/servicecontext.go
Normal file
7
example/graceful/dns/api/svc/servicecontext.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package svc
|
||||
|
||||
import "zero/rpcx"
|
||||
|
||||
type ServiceContext struct {
|
||||
Client *rpcx.RpcClient
|
||||
}
|
||||
7
example/graceful/dns/api/types/types.go
Normal file
7
example/graceful/dns/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"`
|
||||
}
|
||||
22
example/graceful/dns/rpc/Dockerfile
Normal file
22
example/graceful/dns/rpc/Dockerfile
Normal file
@@ -0,0 +1,22 @@
|
||||
FROM golang:1.13 AS builder
|
||||
|
||||
ENV CGO_ENABLED 0
|
||||
ENV GOOS linux
|
||||
|
||||
WORKDIR $GOPATH/src/zero
|
||||
COPY . .
|
||||
RUN go build -ldflags="-s -w" -o /app/gracefulrpc example/graceful/dns/rpc/gracefulrpc.go
|
||||
|
||||
|
||||
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/gracefulrpc /app/gracefulrpc
|
||||
COPY example/graceful/dns/rpc/etc/config.json /app/etc/config.json
|
||||
|
||||
CMD ["./gracefulrpc", "-f", "etc/config.json"]
|
||||
11
example/graceful/dns/rpc/Makefile
Normal file
11
example/graceful/dns/rpc/Makefile
Normal file
@@ -0,0 +1,11 @@
|
||||
version := v$(shell /bin/date "+%y%m%d%H%M%S")
|
||||
|
||||
build:
|
||||
docker pull alpine
|
||||
cd $(GOPATH)/src/zero && docker build -t registry.cn-hangzhou.aliyuncs.com/xapp/gracefulrpc:$(version) . -f example/graceful/dns/rpc/Dockerfile
|
||||
|
||||
push: build
|
||||
docker push registry.cn-hangzhou.aliyuncs.com/xapp/gracefulrpc:$(version)
|
||||
|
||||
deploy: push
|
||||
kubectl -n kevin set image deployment/gracefulrpc-deployment gracefulrpc=registry-vpc.cn-hangzhou.aliyuncs.com/xapp/gracefulrpc:$(version)
|
||||
4
example/graceful/dns/rpc/etc/config.json
Normal file
4
example/graceful/dns/rpc/etc/config.json
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"Name": "rpc.grace",
|
||||
"ListenOn": "0.0.0.0:3456"
|
||||
}
|
||||
15
example/graceful/dns/rpc/graceful.proto
Normal file
15
example/graceful/dns/rpc/graceful.proto
Normal file
@@ -0,0 +1,15 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package graceful;
|
||||
|
||||
message Request {
|
||||
string from = 1;
|
||||
}
|
||||
|
||||
message Response {
|
||||
string host = 2;
|
||||
}
|
||||
|
||||
service GraceService {
|
||||
rpc grace(Request) returns(Response);
|
||||
}
|
||||
159
example/graceful/dns/rpc/graceful/graceful.pb.go
Normal file
159
example/graceful/dns/rpc/graceful/graceful.pb.go
Normal file
@@ -0,0 +1,159 @@
|
||||
// Code generated by protoc-gen-go.
|
||||
// source: graceful.proto
|
||||
// DO NOT EDIT!
|
||||
|
||||
/*
|
||||
Package graceful is a generated protocol buffer package.
|
||||
|
||||
It is generated from these files:
|
||||
graceful.proto
|
||||
|
||||
It has these top-level messages:
|
||||
Request
|
||||
Response
|
||||
*/
|
||||
package graceful
|
||||
|
||||
import proto "github.com/golang/protobuf/proto"
|
||||
import fmt "fmt"
|
||||
import math "math"
|
||||
|
||||
import (
|
||||
context "golang.org/x/net/context"
|
||||
grpc "google.golang.org/grpc"
|
||||
)
|
||||
|
||||
// Reference imports to suppress errors if they are not otherwise used.
|
||||
var _ = proto.Marshal
|
||||
var _ = fmt.Errorf
|
||||
var _ = math.Inf
|
||||
|
||||
// This is a compile-time assertion to ensure that this generated file
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||
|
||||
type Request struct {
|
||||
From string `protobuf:"bytes,1,opt,name=from" json:"from,omitempty"`
|
||||
}
|
||||
|
||||
func (m *Request) Reset() { *m = Request{} }
|
||||
func (m *Request) String() string { return proto.CompactTextString(m) }
|
||||
func (*Request) ProtoMessage() {}
|
||||
func (*Request) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
|
||||
|
||||
func (m *Request) GetFrom() string {
|
||||
if m != nil {
|
||||
return m.From
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
Host string `protobuf:"bytes,2,opt,name=host" json:"host,omitempty"`
|
||||
}
|
||||
|
||||
func (m *Response) Reset() { *m = Response{} }
|
||||
func (m *Response) String() string { return proto.CompactTextString(m) }
|
||||
func (*Response) ProtoMessage() {}
|
||||
func (*Response) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} }
|
||||
|
||||
func (m *Response) GetHost() string {
|
||||
if m != nil {
|
||||
return m.Host
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func init() {
|
||||
proto.RegisterType((*Request)(nil), "graceful.Request")
|
||||
proto.RegisterType((*Response)(nil), "graceful.Response")
|
||||
}
|
||||
|
||||
// Reference imports to suppress errors if they are not otherwise used.
|
||||
var _ context.Context
|
||||
var _ grpc.ClientConn
|
||||
|
||||
// This is a compile-time assertion to ensure that this generated file
|
||||
// is compatible with the grpc package it is being compiled against.
|
||||
const _ = grpc.SupportPackageIsVersion4
|
||||
|
||||
// Client API for GraceService service
|
||||
|
||||
type GraceServiceClient interface {
|
||||
Grace(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error)
|
||||
}
|
||||
|
||||
type graceServiceClient struct {
|
||||
cc *grpc.ClientConn
|
||||
}
|
||||
|
||||
func NewGraceServiceClient(cc *grpc.ClientConn) GraceServiceClient {
|
||||
return &graceServiceClient{cc}
|
||||
}
|
||||
|
||||
func (c *graceServiceClient) Grace(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error) {
|
||||
out := new(Response)
|
||||
err := grpc.Invoke(ctx, "/graceful.GraceService/grace", in, out, c.cc, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Server API for GraceService service
|
||||
|
||||
type GraceServiceServer interface {
|
||||
Grace(context.Context, *Request) (*Response, error)
|
||||
}
|
||||
|
||||
func RegisterGraceServiceServer(s *grpc.Server, srv GraceServiceServer) {
|
||||
s.RegisterService(&_GraceService_serviceDesc, srv)
|
||||
}
|
||||
|
||||
func _GraceService_Grace_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(Request)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(GraceServiceServer).Grace(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: "/graceful.GraceService/Grace",
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(GraceServiceServer).Grace(ctx, req.(*Request))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
var _GraceService_serviceDesc = grpc.ServiceDesc{
|
||||
ServiceName: "graceful.GraceService",
|
||||
HandlerType: (*GraceServiceServer)(nil),
|
||||
Methods: []grpc.MethodDesc{
|
||||
{
|
||||
MethodName: "grace",
|
||||
Handler: _GraceService_Grace_Handler,
|
||||
},
|
||||
},
|
||||
Streams: []grpc.StreamDesc{},
|
||||
Metadata: "graceful.proto",
|
||||
}
|
||||
|
||||
func init() { proto.RegisterFile("graceful.proto", fileDescriptor0) }
|
||||
|
||||
var fileDescriptor0 = []byte{
|
||||
// 134 bytes of a gzipped FileDescriptorProto
|
||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0xe2, 0x4b, 0x2f, 0x4a, 0x4c,
|
||||
0x4e, 0x4d, 0x2b, 0xcd, 0xd1, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0x80, 0xf1, 0x95, 0x64,
|
||||
0xb9, 0xd8, 0x83, 0x52, 0x0b, 0x4b, 0x53, 0x8b, 0x4b, 0x84, 0x84, 0xb8, 0x58, 0xd2, 0x8a, 0xf2,
|
||||
0x73, 0x25, 0x18, 0x15, 0x18, 0x35, 0x38, 0x83, 0xc0, 0x6c, 0x25, 0x39, 0x2e, 0x8e, 0xa0, 0xd4,
|
||||
0xe2, 0x82, 0xfc, 0xbc, 0xe2, 0x54, 0x90, 0x7c, 0x46, 0x7e, 0x71, 0x89, 0x04, 0x13, 0x44, 0x1e,
|
||||
0xc4, 0x36, 0xb2, 0xe3, 0xe2, 0x71, 0x07, 0x19, 0x15, 0x9c, 0x5a, 0x54, 0x96, 0x99, 0x9c, 0x2a,
|
||||
0xa4, 0xc7, 0xc5, 0x0a, 0x36, 0x5a, 0x48, 0x50, 0x0f, 0x6e, 0x25, 0xd4, 0x7c, 0x29, 0x21, 0x64,
|
||||
0x21, 0x88, 0x99, 0x49, 0x6c, 0x60, 0xf7, 0x18, 0x03, 0x02, 0x00, 0x00, 0xff, 0xff, 0x81, 0x87,
|
||||
0xc8, 0xc1, 0xa1, 0x00, 0x00, 0x00,
|
||||
}
|
||||
51
example/graceful/dns/rpc/gracefulrpc.go
Normal file
51
example/graceful/dns/rpc/gracefulrpc.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"zero/core/conf"
|
||||
"zero/example/graceful/dns/rpc/graceful"
|
||||
"zero/rpcx"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
var configFile = flag.String("f", "etc/config.json", "the config file")
|
||||
|
||||
type GracefulServer struct{}
|
||||
|
||||
func NewGracefulServer() *GracefulServer {
|
||||
return &GracefulServer{}
|
||||
}
|
||||
|
||||
func (gs *GracefulServer) Grace(ctx context.Context, req *graceful.Request) (*graceful.Response, error) {
|
||||
fmt.Println("=>", req)
|
||||
|
||||
time.Sleep(time.Millisecond * 10)
|
||||
|
||||
hostname, err := os.Hostname()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &graceful.Response{
|
||||
Host: hostname,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
var c rpcx.RpcServerConf
|
||||
conf.MustLoad(*configFile, &c)
|
||||
|
||||
server := rpcx.MustNewServer(c, func(grpcServer *grpc.Server) {
|
||||
graceful.RegisterGraceServiceServer(grpcServer, NewGracefulServer())
|
||||
})
|
||||
defer server.Stop()
|
||||
server.Start()
|
||||
}
|
||||
46
example/graceful/dns/rpc/gracefulrpc.yaml
Normal file
46
example/graceful/dns/rpc/gracefulrpc.yaml
Normal file
@@ -0,0 +1,46 @@
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: gracefulrpc
|
||||
namespace: kevin
|
||||
spec:
|
||||
selector:
|
||||
app: gracefulrpc
|
||||
type: ClusterIP
|
||||
clusterIP: None
|
||||
ports:
|
||||
- name: gracefulrpc-port
|
||||
port: 3456
|
||||
|
||||
---
|
||||
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: gracefulrpc-deployment
|
||||
namespace: kevin
|
||||
labels:
|
||||
app: gracefulrpc
|
||||
spec:
|
||||
replicas: 3
|
||||
selector:
|
||||
matchLabels:
|
||||
app: gracefulrpc
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: gracefulrpc
|
||||
spec:
|
||||
containers:
|
||||
- name: gracefulrpc
|
||||
image: registry-vpc.cn-hangzhou.aliyuncs.com/xapp/gracefulrpc:v191022143425
|
||||
imagePullPolicy: Always
|
||||
ports:
|
||||
- containerPort: 3456
|
||||
env:
|
||||
- name: POD_IP
|
||||
valueFrom:
|
||||
fieldRef:
|
||||
fieldPath: status.podIP
|
||||
imagePullSecrets:
|
||||
- name: aliyun
|
||||
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"`
|
||||
}
|
||||
4
example/graceful/etcd/discov/discov-namespace.yaml
Normal file
4
example/graceful/etcd/discov/discov-namespace.yaml
Normal file
@@ -0,0 +1,4 @@
|
||||
apiVersion: v1
|
||||
kind: Namespace
|
||||
metadata:
|
||||
name: discov
|
||||
319
example/graceful/etcd/discov/etcd.yaml
Normal file
319
example/graceful/etcd/discov/etcd.yaml
Normal file
@@ -0,0 +1,319 @@
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: etcd
|
||||
namespace: discov
|
||||
spec:
|
||||
ports:
|
||||
- name: etcd-port
|
||||
port: 2379
|
||||
protocol: TCP
|
||||
targetPort: 2379
|
||||
selector:
|
||||
app: etcd
|
||||
|
||||
---
|
||||
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
labels:
|
||||
app: etcd
|
||||
etcd_node: etcd0
|
||||
name: etcd0
|
||||
namespace: discov
|
||||
spec:
|
||||
containers:
|
||||
- command:
|
||||
- /usr/local/bin/etcd
|
||||
- --name
|
||||
- etcd0
|
||||
- --initial-advertise-peer-urls
|
||||
- http://etcd0:2380
|
||||
- --listen-peer-urls
|
||||
- http://0.0.0.0:2380
|
||||
- --listen-client-urls
|
||||
- http://0.0.0.0:2379
|
||||
- --advertise-client-urls
|
||||
- http://etcd0:2379
|
||||
- --initial-cluster
|
||||
- etcd0=http://etcd0:2380,etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380,etcd4=http://etcd4:2380
|
||||
- --initial-cluster-state
|
||||
- new
|
||||
image: quay.io/coreos/etcd:latest
|
||||
name: etcd0
|
||||
ports:
|
||||
- containerPort: 2379
|
||||
name: client
|
||||
protocol: TCP
|
||||
- containerPort: 2380
|
||||
name: server
|
||||
protocol: TCP
|
||||
restartPolicy: Always
|
||||
|
||||
---
|
||||
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
labels:
|
||||
etcd_node: etcd0
|
||||
name: etcd0
|
||||
namespace: discov
|
||||
spec:
|
||||
ports:
|
||||
- name: client
|
||||
port: 2379
|
||||
protocol: TCP
|
||||
targetPort: 2379
|
||||
- name: server
|
||||
port: 2380
|
||||
protocol: TCP
|
||||
targetPort: 2380
|
||||
selector:
|
||||
etcd_node: etcd0
|
||||
|
||||
---
|
||||
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
labels:
|
||||
app: etcd
|
||||
etcd_node: etcd1
|
||||
name: etcd1
|
||||
namespace: discov
|
||||
spec:
|
||||
containers:
|
||||
- command:
|
||||
- /usr/local/bin/etcd
|
||||
- --name
|
||||
- etcd1
|
||||
- --initial-advertise-peer-urls
|
||||
- http://etcd1:2380
|
||||
- --listen-peer-urls
|
||||
- http://0.0.0.0:2380
|
||||
- --listen-client-urls
|
||||
- http://0.0.0.0:2379
|
||||
- --advertise-client-urls
|
||||
- http://etcd1:2379
|
||||
- --initial-cluster
|
||||
- etcd0=http://etcd0:2380,etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380,etcd4=http://etcd4:2380
|
||||
- --initial-cluster-state
|
||||
- new
|
||||
image: quay.io/coreos/etcd:latest
|
||||
name: etcd1
|
||||
ports:
|
||||
- containerPort: 2379
|
||||
name: client
|
||||
protocol: TCP
|
||||
- containerPort: 2380
|
||||
name: server
|
||||
protocol: TCP
|
||||
restartPolicy: Always
|
||||
|
||||
---
|
||||
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
labels:
|
||||
etcd_node: etcd1
|
||||
name: etcd1
|
||||
namespace: discov
|
||||
spec:
|
||||
ports:
|
||||
- name: client
|
||||
port: 2379
|
||||
protocol: TCP
|
||||
targetPort: 2379
|
||||
- name: server
|
||||
port: 2380
|
||||
protocol: TCP
|
||||
targetPort: 2380
|
||||
selector:
|
||||
etcd_node: etcd1
|
||||
|
||||
---
|
||||
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
labels:
|
||||
app: etcd
|
||||
etcd_node: etcd2
|
||||
name: etcd2
|
||||
namespace: discov
|
||||
spec:
|
||||
containers:
|
||||
- command:
|
||||
- /usr/local/bin/etcd
|
||||
- --name
|
||||
- etcd2
|
||||
- --initial-advertise-peer-urls
|
||||
- http://etcd2:2380
|
||||
- --listen-peer-urls
|
||||
- http://0.0.0.0:2380
|
||||
- --listen-client-urls
|
||||
- http://0.0.0.0:2379
|
||||
- --advertise-client-urls
|
||||
- http://etcd2:2379
|
||||
- --initial-cluster
|
||||
- etcd0=http://etcd0:2380,etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380,etcd4=http://etcd4:2380
|
||||
- --initial-cluster-state
|
||||
- new
|
||||
image: quay.io/coreos/etcd:latest
|
||||
name: etcd2
|
||||
ports:
|
||||
- containerPort: 2379
|
||||
name: client
|
||||
protocol: TCP
|
||||
- containerPort: 2380
|
||||
name: server
|
||||
protocol: TCP
|
||||
restartPolicy: Always
|
||||
|
||||
---
|
||||
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
labels:
|
||||
etcd_node: etcd2
|
||||
name: etcd2
|
||||
namespace: discov
|
||||
spec:
|
||||
ports:
|
||||
- name: client
|
||||
port: 2379
|
||||
protocol: TCP
|
||||
targetPort: 2379
|
||||
- name: server
|
||||
port: 2380
|
||||
protocol: TCP
|
||||
targetPort: 2380
|
||||
selector:
|
||||
etcd_node: etcd2
|
||||
|
||||
---
|
||||
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
labels:
|
||||
app: etcd
|
||||
etcd_node: etcd3
|
||||
name: etcd3
|
||||
namespace: discov
|
||||
spec:
|
||||
containers:
|
||||
- command:
|
||||
- /usr/local/bin/etcd
|
||||
- --name
|
||||
- etcd3
|
||||
- --initial-advertise-peer-urls
|
||||
- http://etcd3:2380
|
||||
- --listen-peer-urls
|
||||
- http://0.0.0.0:2380
|
||||
- --listen-client-urls
|
||||
- http://0.0.0.0:2379
|
||||
- --advertise-client-urls
|
||||
- http://etcd3:2379
|
||||
- --initial-cluster
|
||||
- etcd0=http://etcd0:2380,etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380,etcd4=http://etcd4:2380
|
||||
- --initial-cluster-state
|
||||
- new
|
||||
image: quay.io/coreos/etcd:latest
|
||||
name: etcd3
|
||||
ports:
|
||||
- containerPort: 2379
|
||||
name: client
|
||||
protocol: TCP
|
||||
- containerPort: 2380
|
||||
name: server
|
||||
protocol: TCP
|
||||
restartPolicy: Always
|
||||
|
||||
---
|
||||
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
labels:
|
||||
etcd_node: etcd3
|
||||
name: etcd3
|
||||
namespace: discov
|
||||
spec:
|
||||
ports:
|
||||
- name: client
|
||||
port: 2379
|
||||
protocol: TCP
|
||||
targetPort: 2379
|
||||
- name: server
|
||||
port: 2380
|
||||
protocol: TCP
|
||||
targetPort: 2380
|
||||
selector:
|
||||
etcd_node: etcd3
|
||||
|
||||
---
|
||||
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
labels:
|
||||
app: etcd
|
||||
etcd_node: etcd4
|
||||
name: etcd4
|
||||
namespace: discov
|
||||
spec:
|
||||
containers:
|
||||
- command:
|
||||
- /usr/local/bin/etcd
|
||||
- --name
|
||||
- etcd4
|
||||
- --initial-advertise-peer-urls
|
||||
- http://etcd4:2380
|
||||
- --listen-peer-urls
|
||||
- http://0.0.0.0:2380
|
||||
- --listen-client-urls
|
||||
- http://0.0.0.0:2379
|
||||
- --advertise-client-urls
|
||||
- http://etcd4:2379
|
||||
- --initial-cluster
|
||||
- etcd0=http://etcd0:2380,etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380,etcd4=http://etcd4:2380
|
||||
- --initial-cluster-state
|
||||
- new
|
||||
image: quay.io/coreos/etcd:latest
|
||||
name: etcd4
|
||||
ports:
|
||||
- containerPort: 2379
|
||||
name: client
|
||||
protocol: TCP
|
||||
- containerPort: 2380
|
||||
name: server
|
||||
protocol: TCP
|
||||
restartPolicy: Always
|
||||
|
||||
---
|
||||
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
labels:
|
||||
etcd_node: etcd4
|
||||
name: etcd4
|
||||
namespace: discov
|
||||
spec:
|
||||
ports:
|
||||
- name: client
|
||||
port: 2379
|
||||
protocol: TCP
|
||||
targetPort: 2379
|
||||
- name: server
|
||||
port: 2380
|
||||
protocol: TCP
|
||||
targetPort: 2380
|
||||
selector:
|
||||
etcd_node: etcd4
|
||||
|
||||
24
example/graceful/etcd/rpc/Dockerfile
Normal file
24
example/graceful/etcd/rpc/Dockerfile
Normal file
@@ -0,0 +1,24 @@
|
||||
FROM golang:alpine AS builder
|
||||
|
||||
LABEL stage=gobuilder
|
||||
|
||||
ENV CGO_ENABLED 0
|
||||
ENV GOOS linux
|
||||
|
||||
WORKDIR $GOPATH/src/zero
|
||||
COPY . .
|
||||
RUN go build -ldflags="-s -w" -o /app/gracefulrpc example/graceful/etcd/rpc/gracefulrpc.go
|
||||
|
||||
|
||||
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/gracefulrpc /app/gracefulrpc
|
||||
COPY example/graceful/etcd/rpc/etc/graceful-rpc.json /app/etc/config.json
|
||||
|
||||
CMD ["./gracefulrpc", "-f", "etc/config.json"]
|
||||
13
example/graceful/etcd/rpc/Makefile
Normal file
13
example/graceful/etcd/rpc/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/gracefulrpc:$(version) . -f example/graceful/etcd/rpc/Dockerfile
|
||||
docker image prune --filter label=stage=gobuilder -f
|
||||
|
||||
push: build
|
||||
docker push registry.cn-hangzhou.aliyuncs.com/xapp/gracefulrpc:$(version)
|
||||
|
||||
deploy: push
|
||||
kubectl -n kevin set image deployment/gracefulrpc-deployment gracefulrpc=registry-vpc.cn-hangzhou.aliyuncs.com/xapp/gracefulrpc:$(version)
|
||||
8
example/graceful/etcd/rpc/etc/graceful-rpc.json
Normal file
8
example/graceful/etcd/rpc/etc/graceful-rpc.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"Name": "rpc.grace",
|
||||
"ListenOn": "0.0.0.0:3456",
|
||||
"Etcd": {
|
||||
"Hosts": ["etcd.discov:2379"],
|
||||
"Key": "rpcx"
|
||||
}
|
||||
}
|
||||
15
example/graceful/etcd/rpc/graceful.proto
Normal file
15
example/graceful/etcd/rpc/graceful.proto
Normal file
@@ -0,0 +1,15 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package graceful;
|
||||
|
||||
message Request {
|
||||
string from = 1;
|
||||
}
|
||||
|
||||
message Response {
|
||||
string host = 2;
|
||||
}
|
||||
|
||||
service GraceService {
|
||||
rpc grace(Request) returns(Response);
|
||||
}
|
||||
159
example/graceful/etcd/rpc/graceful/graceful.pb.go
Normal file
159
example/graceful/etcd/rpc/graceful/graceful.pb.go
Normal file
@@ -0,0 +1,159 @@
|
||||
// Code generated by protoc-gen-go.
|
||||
// source: graceful.proto
|
||||
// DO NOT EDIT!
|
||||
|
||||
/*
|
||||
Package graceful is a generated protocol buffer package.
|
||||
|
||||
It is generated from these files:
|
||||
graceful.proto
|
||||
|
||||
It has these top-level messages:
|
||||
Request
|
||||
Response
|
||||
*/
|
||||
package graceful
|
||||
|
||||
import proto "github.com/golang/protobuf/proto"
|
||||
import fmt "fmt"
|
||||
import math "math"
|
||||
|
||||
import (
|
||||
context "golang.org/x/net/context"
|
||||
grpc "google.golang.org/grpc"
|
||||
)
|
||||
|
||||
// Reference imports to suppress errors if they are not otherwise used.
|
||||
var _ = proto.Marshal
|
||||
var _ = fmt.Errorf
|
||||
var _ = math.Inf
|
||||
|
||||
// This is a compile-time assertion to ensure that this generated file
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||
|
||||
type Request struct {
|
||||
From string `protobuf:"bytes,1,opt,name=from" json:"from,omitempty"`
|
||||
}
|
||||
|
||||
func (m *Request) Reset() { *m = Request{} }
|
||||
func (m *Request) String() string { return proto.CompactTextString(m) }
|
||||
func (*Request) ProtoMessage() {}
|
||||
func (*Request) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
|
||||
|
||||
func (m *Request) GetFrom() string {
|
||||
if m != nil {
|
||||
return m.From
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
Host string `protobuf:"bytes,2,opt,name=host" json:"host,omitempty"`
|
||||
}
|
||||
|
||||
func (m *Response) Reset() { *m = Response{} }
|
||||
func (m *Response) String() string { return proto.CompactTextString(m) }
|
||||
func (*Response) ProtoMessage() {}
|
||||
func (*Response) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} }
|
||||
|
||||
func (m *Response) GetHost() string {
|
||||
if m != nil {
|
||||
return m.Host
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func init() {
|
||||
proto.RegisterType((*Request)(nil), "graceful.Request")
|
||||
proto.RegisterType((*Response)(nil), "graceful.Response")
|
||||
}
|
||||
|
||||
// Reference imports to suppress errors if they are not otherwise used.
|
||||
var _ context.Context
|
||||
var _ grpc.ClientConn
|
||||
|
||||
// This is a compile-time assertion to ensure that this generated file
|
||||
// is compatible with the grpc package it is being compiled against.
|
||||
const _ = grpc.SupportPackageIsVersion4
|
||||
|
||||
// Client API for GraceService service
|
||||
|
||||
type GraceServiceClient interface {
|
||||
Grace(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error)
|
||||
}
|
||||
|
||||
type graceServiceClient struct {
|
||||
cc *grpc.ClientConn
|
||||
}
|
||||
|
||||
func NewGraceServiceClient(cc *grpc.ClientConn) GraceServiceClient {
|
||||
return &graceServiceClient{cc}
|
||||
}
|
||||
|
||||
func (c *graceServiceClient) Grace(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error) {
|
||||
out := new(Response)
|
||||
err := grpc.Invoke(ctx, "/graceful.GraceService/grace", in, out, c.cc, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Server API for GraceService service
|
||||
|
||||
type GraceServiceServer interface {
|
||||
Grace(context.Context, *Request) (*Response, error)
|
||||
}
|
||||
|
||||
func RegisterGraceServiceServer(s *grpc.Server, srv GraceServiceServer) {
|
||||
s.RegisterService(&_GraceService_serviceDesc, srv)
|
||||
}
|
||||
|
||||
func _GraceService_Grace_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(Request)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(GraceServiceServer).Grace(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: "/graceful.GraceService/Grace",
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(GraceServiceServer).Grace(ctx, req.(*Request))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
var _GraceService_serviceDesc = grpc.ServiceDesc{
|
||||
ServiceName: "graceful.GraceService",
|
||||
HandlerType: (*GraceServiceServer)(nil),
|
||||
Methods: []grpc.MethodDesc{
|
||||
{
|
||||
MethodName: "grace",
|
||||
Handler: _GraceService_Grace_Handler,
|
||||
},
|
||||
},
|
||||
Streams: []grpc.StreamDesc{},
|
||||
Metadata: "graceful.proto",
|
||||
}
|
||||
|
||||
func init() { proto.RegisterFile("graceful.proto", fileDescriptor0) }
|
||||
|
||||
var fileDescriptor0 = []byte{
|
||||
// 134 bytes of a gzipped FileDescriptorProto
|
||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0xe2, 0x4b, 0x2f, 0x4a, 0x4c,
|
||||
0x4e, 0x4d, 0x2b, 0xcd, 0xd1, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0x80, 0xf1, 0x95, 0x64,
|
||||
0xb9, 0xd8, 0x83, 0x52, 0x0b, 0x4b, 0x53, 0x8b, 0x4b, 0x84, 0x84, 0xb8, 0x58, 0xd2, 0x8a, 0xf2,
|
||||
0x73, 0x25, 0x18, 0x15, 0x18, 0x35, 0x38, 0x83, 0xc0, 0x6c, 0x25, 0x39, 0x2e, 0x8e, 0xa0, 0xd4,
|
||||
0xe2, 0x82, 0xfc, 0xbc, 0xe2, 0x54, 0x90, 0x7c, 0x46, 0x7e, 0x71, 0x89, 0x04, 0x13, 0x44, 0x1e,
|
||||
0xc4, 0x36, 0xb2, 0xe3, 0xe2, 0x71, 0x07, 0x19, 0x15, 0x9c, 0x5a, 0x54, 0x96, 0x99, 0x9c, 0x2a,
|
||||
0xa4, 0xc7, 0xc5, 0x0a, 0x36, 0x5a, 0x48, 0x50, 0x0f, 0x6e, 0x25, 0xd4, 0x7c, 0x29, 0x21, 0x64,
|
||||
0x21, 0x88, 0x99, 0x49, 0x6c, 0x60, 0xf7, 0x18, 0x03, 0x02, 0x00, 0x00, 0xff, 0xff, 0x81, 0x87,
|
||||
0xc8, 0xc1, 0xa1, 0x00, 0x00, 0x00,
|
||||
}
|
||||
30
example/graceful/etcd/rpc/gracefulrpc-env.yaml
Normal file
30
example/graceful/etcd/rpc/gracefulrpc-env.yaml
Normal file
@@ -0,0 +1,30 @@
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: gracefulrpc-deployment
|
||||
namespace: kevin
|
||||
labels:
|
||||
app: gracefulrpc
|
||||
spec:
|
||||
replicas: 9
|
||||
selector:
|
||||
matchLabels:
|
||||
app: gracefulrpc
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: gracefulrpc
|
||||
spec:
|
||||
containers:
|
||||
- name: gracefulrpc
|
||||
image: registry-vpc.cn-hangzhou.aliyuncs.com/xapp/gracefulrpc:v191031144304
|
||||
imagePullPolicy: Always
|
||||
ports:
|
||||
- containerPort: 3456
|
||||
env:
|
||||
- name: POD_IP
|
||||
valueFrom:
|
||||
fieldRef:
|
||||
fieldPath: status.podIP
|
||||
imagePullSecrets:
|
||||
- name: aliyun
|
||||
41
example/graceful/etcd/rpc/gracefulrpc-headless.yaml
Normal file
41
example/graceful/etcd/rpc/gracefulrpc-headless.yaml
Normal file
@@ -0,0 +1,41 @@
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: gracefulrpc
|
||||
namespace: kevin
|
||||
spec:
|
||||
selector:
|
||||
app: gracefulrpc
|
||||
type: ClusterIP
|
||||
clusterIP: None
|
||||
ports:
|
||||
- name: gracefulrpc-port
|
||||
port: 3456
|
||||
|
||||
---
|
||||
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: gracefulrpc-deployment
|
||||
namespace: kevin
|
||||
labels:
|
||||
app: gracefulrpc
|
||||
spec:
|
||||
replicas: 9
|
||||
selector:
|
||||
matchLabels:
|
||||
app: gracefulrpc
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: gracefulrpc
|
||||
spec:
|
||||
containers:
|
||||
- name: gracefulrpc
|
||||
image: registry-vpc.cn-hangzhou.aliyuncs.com/xapp/gracefulrpc:v191031144304
|
||||
imagePullPolicy: Always
|
||||
ports:
|
||||
- containerPort: 3456
|
||||
imagePullSecrets:
|
||||
- name: aliyun
|
||||
51
example/graceful/etcd/rpc/gracefulrpc.go
Normal file
51
example/graceful/etcd/rpc/gracefulrpc.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"zero/core/conf"
|
||||
"zero/example/graceful/etcd/rpc/graceful"
|
||||
"zero/rpcx"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
var configFile = flag.String("f", "etc/config.json", "the config file")
|
||||
|
||||
type GracefulServer struct{}
|
||||
|
||||
func NewGracefulServer() *GracefulServer {
|
||||
return &GracefulServer{}
|
||||
}
|
||||
|
||||
func (gs *GracefulServer) Grace(ctx context.Context, req *graceful.Request) (*graceful.Response, error) {
|
||||
fmt.Println("=>", req)
|
||||
|
||||
time.Sleep(time.Millisecond * 10)
|
||||
|
||||
hostname, err := os.Hostname()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &graceful.Response{
|
||||
Host: hostname,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
var c rpcx.RpcServerConf
|
||||
conf.MustLoad(*configFile, &c)
|
||||
|
||||
server := rpcx.MustNewServer(c, func(grpcServer *grpc.Server) {
|
||||
graceful.RegisterGraceServiceServer(grpcServer, NewGracefulServer())
|
||||
})
|
||||
defer server.Stop()
|
||||
server.Start()
|
||||
}
|
||||
25
example/graceful/etcd/rpc/gracefulrpc.yaml
Normal file
25
example/graceful/etcd/rpc/gracefulrpc.yaml
Normal file
@@ -0,0 +1,25 @@
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: gracefulrpc-deployment
|
||||
namespace: kevin
|
||||
labels:
|
||||
app: gracefulrpc
|
||||
spec:
|
||||
replicas: 9
|
||||
selector:
|
||||
matchLabels:
|
||||
app: gracefulrpc
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: gracefulrpc
|
||||
spec:
|
||||
containers:
|
||||
- name: gracefulrpc
|
||||
image: registry-vpc.cn-hangzhou.aliyuncs.com/xapp/gracefulrpc:v191031144304
|
||||
imagePullPolicy: Always
|
||||
ports:
|
||||
- containerPort: 3456
|
||||
imagePullSecrets:
|
||||
- name: aliyun
|
||||
Reference in New Issue
Block a user