123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- package main
- import (
- "context"
- "flag"
- "fmt"
- "log"
- "os"
- "sync"
- "time"
- "github.com/tal-tech/go-zero/core/conf"
- "github.com/tal-tech/go-zero/example/rpc/remote/unary"
- "github.com/tal-tech/go-zero/zrpc"
- "google.golang.org/grpc"
- )
- var configFile = flag.String("f", "etc/config.json", "the config file")
- type GreetServer struct {
- lock sync.Mutex
- alive bool
- downTime time.Time
- }
- func NewGreetServer() *GreetServer {
- return &GreetServer{
- alive: true,
- }
- }
- func (gs *GreetServer) Greet(ctx context.Context, req *unary.Request) (*unary.Response, error) {
- fmt.Println("=>", req)
- hostname, err := os.Hostname()
- if err != nil {
- return nil, err
- }
- return &unary.Response{
- Greet: "hello from " + hostname,
- }, nil
- }
- func main() {
- flag.Parse()
- var c zrpc.RpcServerConf
- conf.MustLoad(*configFile, &c)
- server := zrpc.MustNewServer(c, func(grpcServer *grpc.Server) {
- unary.RegisterGreeterServer(grpcServer, NewGreetServer())
- })
- interceptor := func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
- st := time.Now()
- resp, err = handler(ctx, req)
- log.Printf("method: %s time: %v\n", info.FullMethod, time.Since(st))
- return resp, err
- }
- server.AddUnaryInterceptors(interceptor)
- server.Start()
- }
|