server.go 979 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/tracing/remote/user"
  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 UserServer struct {
  16. lock sync.Mutex
  17. alive bool
  18. downTime time.Time
  19. }
  20. func NewUserServer() *UserServer {
  21. return &UserServer{
  22. alive: true,
  23. }
  24. }
  25. func (gs *UserServer) GetGrade(ctx context.Context, req *user.UserRequest) (*user.UserResponse, error) {
  26. fmt.Println("=>", req)
  27. hostname, err := os.Hostname()
  28. if err != nil {
  29. return nil, err
  30. }
  31. return &user.UserResponse{
  32. Response: "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. user.RegisterUserServer(grpcServer, NewUserServer())
  41. })
  42. server.Start()
  43. }