gracefulrpc.go 973 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "os"
  7. "time"
  8. "github.com/tal-tech/go-zero/core/conf"
  9. "github.com/tal-tech/go-zero/example/graceful/etcd/rpc/graceful"
  10. "github.com/tal-tech/go-zero/zrpc"
  11. "google.golang.org/grpc"
  12. )
  13. var configFile = flag.String("f", "etc/config.json", "the config file")
  14. type GracefulServer struct{}
  15. func NewGracefulServer() *GracefulServer {
  16. return &GracefulServer{}
  17. }
  18. func (gs *GracefulServer) Grace(ctx context.Context, req *graceful.Request) (*graceful.Response, error) {
  19. fmt.Println("=>", req)
  20. time.Sleep(time.Millisecond * 10)
  21. hostname, err := os.Hostname()
  22. if err != nil {
  23. return nil, err
  24. }
  25. return &graceful.Response{
  26. Host: hostname,
  27. }, nil
  28. }
  29. func main() {
  30. flag.Parse()
  31. var c zrpc.RpcServerConf
  32. conf.MustLoad(*configFile, &c)
  33. server := zrpc.MustNewServer(c, func(grpcServer *grpc.Server) {
  34. graceful.RegisterGraceServiceServer(grpcServer, NewGracefulServer())
  35. })
  36. defer server.Stop()
  37. server.Start()
  38. }