server.go 971 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "os"
  7. "sync"
  8. "time"
  9. "github.com/tal-tech/go-zero/core/conf"
  10. "github.com/tal-tech/go-zero/example/rpc/remote/unary"
  11. "github.com/tal-tech/go-zero/zrpc"
  12. "google.golang.org/grpc"
  13. )
  14. var configFile = flag.String("f", "etc/config.json", "the config file")
  15. type GreetServer struct {
  16. lock sync.Mutex
  17. alive bool
  18. downTime time.Time
  19. }
  20. func NewGreetServer() *GreetServer {
  21. return &GreetServer{
  22. alive: true,
  23. }
  24. }
  25. func (gs *GreetServer) Greet(ctx context.Context, req *unary.Request) (*unary.Response, error) {
  26. fmt.Println("=>", req)
  27. hostname, err := os.Hostname()
  28. if err != nil {
  29. return nil, err
  30. }
  31. return &unary.Response{
  32. Greet: "hello from " + hostname,
  33. }, nil
  34. }
  35. func main() {
  36. flag.Parse()
  37. var c zrpc.RpcServerConf
  38. conf.MustLoad(*configFile, &c)
  39. server := zrpc.MustNewServer(c, func(grpcServer *grpc.Server) {
  40. unary.RegisterGreeterServer(grpcServer, NewGreetServer())
  41. })
  42. server.Start()
  43. }