Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up gRPC client - server correctly

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 {}
like image 408
vardius Avatar asked Feb 08 '18 07:02

vardius


People also ask

Can a gRPC server also be a client?

Yes, you can definitely do that.

Can gRPC server handle multiple clients?

Multiple gRPC clients can be created from a channel, including different types of clients. A channel and clients created from the channel can safely be used by multiple threads. Clients created from the channel can make multiple simultaneous calls.

How do I host a gRPC service?

Hosting pacakge to host a . net core console application by using the HostBuilder API to start building gRPC host and setting it up. By doing this, the generic host will automatically run StartAsync on our hosted service, which in turn will call StartAsync on the Server instance, essentially start the gRPC server.


1 Answers

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"

like image 97
Tommaso Barbugli Avatar answered Oct 16 '22 08:10

Tommaso Barbugli