forked from zeromicro/go-zero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
49 lines (41 loc) · 1.03 KB
/
proxy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"context"
"flag"
"github.com/yilefreedom/go-zero/core/logx"
"github.com/yilefreedom/go-zero/core/service"
"github.com/yilefreedom/go-zero/example/rpc/remote/unary"
"github.com/yilefreedom/go-zero/zrpc"
"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 {
*zrpc.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 := zrpc.MustNewServer(zrpc.RpcServerConf{
ServiceConf: service.ServiceConf{
Log: logx.LogConf{
Mode: "console",
},
},
ListenOn: *listen,
}, func(grpcServer *grpc.Server) {
unary.RegisterGreeterServer(grpcServer, &GreetServer{
RpcProxy: zrpc.NewProxy(*server),
})
})
proxy.Start()
}