server.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "github.com/tal-tech/go-zero/core/conf"
  6. "github.com/tal-tech/go-zero/zrpc"
  7. "github.com/zeromicro/zero-examples/tracing/remote/portal"
  8. "github.com/zeromicro/zero-examples/tracing/remote/user"
  9. "google.golang.org/grpc"
  10. )
  11. var configFile = flag.String("f", "etc/config.json", "the config file")
  12. type (
  13. Config struct {
  14. zrpc.RpcServerConf
  15. UserRpc zrpc.RpcClientConf
  16. }
  17. PortalServer struct {
  18. userRpc zrpc.Client
  19. }
  20. )
  21. func NewPortalServer(client zrpc.Client) *PortalServer {
  22. return &PortalServer{
  23. userRpc: client,
  24. }
  25. }
  26. func (gs *PortalServer) Portal(ctx context.Context, req *portal.PortalRequest) (*portal.PortalResponse, error) {
  27. conn := gs.userRpc.Conn()
  28. greet := user.NewUserClient(conn)
  29. resp, err := greet.GetGrade(ctx, &user.UserRequest{
  30. Name: req.Name,
  31. })
  32. if err != nil {
  33. return &portal.PortalResponse{
  34. Response: err.Error(),
  35. }, nil
  36. } else {
  37. return &portal.PortalResponse{
  38. Response: resp.Response,
  39. }, nil
  40. }
  41. }
  42. func main() {
  43. flag.Parse()
  44. var c Config
  45. conf.MustLoad(*configFile, &c)
  46. client := zrpc.MustNewClient(c.UserRpc)
  47. server := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) {
  48. portal.RegisterPortalServer(grpcServer, NewPortalServer(client))
  49. })
  50. server.Start()
  51. }