go-zero/example/graceful/etcd/rpc/gracefulrpc.go

51 lines
973 B
Go
Raw Normal View History

2020-07-26 17:09:05 +08:00
package main
import (
"context"
"flag"
"fmt"
"os"
"time"
2020-08-08 16:40:10 +08:00
"github.com/tal-tech/go-zero/core/conf"
"github.com/tal-tech/go-zero/example/graceful/etcd/rpc/graceful"
2020-09-18 11:41:52 +08:00
"github.com/tal-tech/go-zero/zrpc"
2020-07-26 17:09:05 +08:00
"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()
2020-09-18 11:41:52 +08:00
var c zrpc.RpcServerConf
2020-07-26 17:09:05 +08:00
conf.MustLoad(*configFile, &c)
2020-09-18 11:41:52 +08:00
server := zrpc.MustNewServer(c, func(grpcServer *grpc.Server) {
2020-07-26 17:09:05 +08:00
graceful.RegisterGraceServiceServer(grpcServer, NewGracefulServer())
})
defer server.Stop()
server.Start()
}