Unable to connect to Cloud Run gRPC server

I'm trying to deploy a Cloud run gRPC server, by now I've accomplished the deployment but when I try to connect and call a gRPC function from any other service or external client I have the same error, every time. I 've tried modifying ports, permissions, code and everything I found on internet.

I believe the problem is in the network setting but can't find the way to connect and call a gRPC function from the server.

The error I received is:

 

 

 

rpc error: code = Unavailable desc = connection closed before server preface received

 

 

 

 

The main.go file with the server code is this:

 

 

 

func main() {
	port := os.Getenv("PORT")
	if port == "" {
		port = config.API_PORT
	}

	grpcEndpoint := fmt.Sprintf(":%s", port)
	log.Printf("gRPC endpoint [%s]", grpcEndpoint)

	/* Connecting client to all the necessary repositories */

	repo := repository.NewRepository(client)
	useCase := service.NewService(repo)

	// // Configure the net listener to the api port defined
	netListener, err := net.Listen("tcp", "0.0.0.0"+grpcEndpoint)
	if err != nil {
		log.Fatalf("Failed to listen: %v", err)
	}

	// Setting new Logger
	grpcLog := grpclog.NewLoggerV2(os.Stdout, os.Stderr, os.Stderr)
	grpclog.SetLoggerV2(grpcLog)

	grpcServer := grpc.NewServer()
	log.Printf("Server listening at %v", netListener.Addr())

	implementation := handler.NewImplementation(useCase)
	
	service.RegisterServiceServer(grpcServer, implementation)
	
	// Starting the server
	if err := grpcServer.Serve(netListener); err != nil {
		log.Fatalf("Failed to serve: %v", err)
	}
}

 

 

 

2 14 8,103
14 REPLIES 14

Experiencing the same problem. Have you found any solution yet?

Well I'm not sure it was that the solution, but I've changed the proto package and modified the way I was importing the server in the handler and it started working.

Do you deploy the container on default port 8080 and specify 443 when connecting from the client? Have you also enabled HTTP2?

HTTP2 was not necessary and yes I deployed it in 8080 but when requesting by grpcurl I do it with the 443 port. Look at this tutorial that helped me to succeed:
https://github.com/grpc-ecosystem/grpc-cloud-run-example/tree/master/golang

Got it up and running now. Our organisation's firewall has blocked the connection.

can you share what changes you made to proto files and server handler to get it working?

Hello, 

You may profit from following an appropriate example, for instance the "Getting started with API Gateway and Cloud Run for gRPC" page

Relevant information may be found as well on programmer forums such as stackoverflow, where programmers with relevant experience are ready to help. 

Obviously I did that, I even said it in the post. Thanks.

Hi @gsuceveanu,

I am having the same issue with Node.js gRPC server, I tried all the things mentioned here, nothing worked for me?

Followed everything mentioned on this code sample, it keeps giving me Error 14, service unavalable, tried both with Docker and cloudbuild, thought maybe connection between Docker is cloudrun had issues but nothing worked.

I appreciate your help, thanks!

This forum software caused me to lose my post twice, with internal server errors and losing my post during the account creation flow. Very frustrating. Also it doesn't support Go syntax highlighting. Here it is a third time:

I encountered this while testing with 

grpc.WithTransportCredentials(grpc.WithInsecure())

 This does not work because it causes gRPC to make the request using H2C (HTTP/2 without TLS), which fails at the Cloud Run ingress with a 503, never reaching the application.

Make sure you're running Cloud Run with HTTP/2 end to end if you require gRPC streaming (and better performance in general), and make sure you're providing some certificates. Either pinned or importing your CA store. Example:

	systemRoots, err := x509.SystemCertPool()
	if err != nil {
		panic(errors.Wrap(err, "cannot load root CA certs"))
	}
	creds := credentials.NewTLS(&tls.Config{
		RootCAs: systemRoots,
	})

	conn, err := grpc.DialContext(ctx, addr,
		grpc.WithTransportCredentials(creds),
	)
	if err != nil {
		panic(err)
	}



Dustin. Thank you so much for taking the time to type this out. It really helped me.

Thanks Dustin, this helped me solve it!

I am having the same issue with Node.js gRPC server, I tried all the things mentioned here, nothing worked for me?

Followed everything mentioned on this code sample, it keeps giving me Error 14, service unavalable, tried both with Docker and cloudbuild, thought maybe connection between Docker is cloudrun had issues but nothing worked.

I appreciate your help, thanks!

I encountered the same mistake @raphaelchaula.

1. My first mistake was using the full url in connecting to the gRPC server. You should drop the https:// 
2. My second was I'm using port 50051 in the client side. It should be port 443. It should look like this 0.0.0.0:443
3. Lastly, I'm connecting using grpc.credentials.createInsecure() method it should be 

createSsl() 

Now I'm able to connect in the server.