You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/example/rpc/proxy/proxy.go

51 lines
956 B
Go

4 years ago
package main
import (
"context"
"flag"
"zero/core/logx"
"zero/core/service"
"zero/example/rpc/remote/unary"
"zero/rpcx"
"google.golang.org/grpc"
)
var (
listen = flag.String("listen", "0.0.0.0:3456", "the address to listen on")
server = flag.String("server", "dns:///unaryserver:3456", "the backend service")
)
type GreetServer struct {
*rpcx.RpcProxy
}
func (s *GreetServer) Greet(ctx context.Context, req *unary.Request) (*unary.Response, error) {
conn, err := s.TakeConn(ctx)
if err != nil {
return nil, err
}
remote := unary.NewGreeterClient(conn)
return remote.Greet(ctx, req)
}
func main() {
flag.Parse()
proxy := rpcx.MustNewServer(rpcx.RpcServerConf{
ServiceConf: service.ServiceConf{
Log: logx.LogConf{
Mode: "console",
},
},
ListenOn: *listen,
}, func(grpcServer *grpc.Server) {
unary.RegisterGreeterServer(grpcServer, &GreetServer{
RpcProxy: rpcx.NewRpcProxy(*server),
})
})
proxy.Start()
}