duanchi4184
2018-02-08 07:11
浏览 510
已采纳

如何正确设置gRPC客户端-服务器

I have a gRPC client and a server, when I run them they seem to be running fine, but when I try to dial a server with a client I get an error:

"Error": {
    "code": 14,
    "message": "all SubConns are in TransientFailure"
},

No idea what is it. I tried to find a solution with google, no success there.

Any ideas? Here is my server code:

lis, err := net.Listen("tcp", fmt.Sprintf("%s:%d", cfg.Host, cfg.Port))
if err != nil {
    logger.Critical(ctx, "failed to listen: %v", err)
} else {
    logger.Info(ctx, "[userserver] running at %s:%d", cfg.Host, cfg.Port)
}

grpcServer := grpc.NewServer()
userServer := userserver.New()
pb.RegisterDomainServer(grpcServer, userServer)
rpcErr := grpcServer.Serve(lis)

if rpcErr != nil {
    logger.Critical(ctx, "failed to serve: %v", rpcErr)
}

btw the server here shows log:

2018/02/08 07:03:37.603287 INFO: [userserver] running at localhost:3001

and client:

conn, err := grpc.Dial(c.serverAddr, grpc.WithInsecure())
if err != nil {
    return err
}
defer conn.Close()

client := pb.NewDomainClient(conn)
_, err = client.Dispatch(ctx, &pb.Command{
    Name:    command,
    Payload: payload,
})

and this is port buff

service Domain {
  rpc Dispatch(Command) returns (Response);
}

message Command {
  string name = 1;
  bytes payload = 2;
}

message Response {}
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douba6365 2018-02-08 15:33
    已采纳

    Is the server listening to that port?

    // Most linux 
    lsof -i :3001
    
    // OSX
    lsof -iTCP -sTCP:LISTEN -P
    

    Are you connecting the client to the right address?

    c.serverAddr should be "127.0.0.1:3001"

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doubingjian2006 2018-02-08 07:52

    This error means the connection to your server is down for some reason.

    You're printing that your server is running at localhost:3001, but don't check whether the server has any errors serving on that address.

    Replace your last line in the server with:

    if err := grpcServer.Serve(lis); err != nil {
        log.Fatalf("failed to serve: %v", err)
    }
    

    Also where does the context ctx in the server startup come from?


    I'm assuming your proto definition looks something like that - otherwise there might be other issues:

    package ...;
    
    service Domain {
       rpc Dispatch(Command) returns (...) {}
    }
    
    message Command {
       ... Name
       ... Payload    
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题