proxy.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "github.com/tal-tech/go-zero/core/logx"
  6. "github.com/tal-tech/go-zero/core/service"
  7. "github.com/tal-tech/go-zero/example/rpc/remote/unary"
  8. "github.com/tal-tech/go-zero/zrpc"
  9. "google.golang.org/grpc"
  10. )
  11. var (
  12. listen = flag.String("listen", "0.0.0.0:3456", "the address to listen on")
  13. server = flag.String("server", "dns:///unaryserver:3456", "the backend service")
  14. )
  15. type GreetServer struct {
  16. *zrpc.RpcProxy
  17. }
  18. func (s *GreetServer) Greet(ctx context.Context, req *unary.Request) (*unary.Response, error) {
  19. conn, err := s.TakeConn(ctx)
  20. if err != nil {
  21. return nil, err
  22. }
  23. remote := unary.NewGreeterClient(conn)
  24. return remote.Greet(ctx, req)
  25. }
  26. func main() {
  27. flag.Parse()
  28. proxy := zrpc.MustNewServer(zrpc.RpcServerConf{
  29. ServiceConf: service.ServiceConf{
  30. Log: logx.LogConf{
  31. Mode: "console",
  32. },
  33. },
  34. ListenOn: *listen,
  35. }, func(grpcServer *grpc.Server) {
  36. unary.RegisterGreeterServer(grpcServer, &GreetServer{
  37. RpcProxy: zrpc.NewProxy(*server),
  38. })
  39. })
  40. proxy.Start()
  41. }