Top Related Projects
Go support for Google's protocol buffers
A simple RPC framework with protobuf service definitions
Best microservices framework in Go, like alibaba Dubbo, but with more features, Scale easily. Try it. Test it. If you feel it's better, use it! 𝐉𝐚𝐯𝐚有𝐝𝐮𝐛𝐛𝐨, 𝐆𝐨𝐥𝐚𝐧𝐠有𝐫𝐩𝐜𝐱! build for cloud!
A Go microservices framework
A standard library for microservices.
Quick Overview
gRPC-Go is the official Go implementation of gRPC, a high-performance, open-source universal RPC framework. It enables efficient communication between distributed systems using Protocol Buffers as the interface definition language and supports various programming languages.
Pros
- High performance and low latency due to its use of HTTP/2 for transport
- Strong typing and code generation for multiple languages
- Built-in support for authentication, load balancing, and streaming
- Excellent for microservices architecture and cloud-native applications
Cons
- Steeper learning curve compared to REST APIs
- Requires additional tooling and setup (Protocol Buffers compiler)
- Limited browser support without a proxy
- Potential compatibility issues with certain firewalls and proxies
Code Examples
- Defining a service in Protocol Buffers:
syntax = "proto3";
package example;
service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
message HelloRequest {
string name = 1;
}
message HelloReply {
string message = 1;
}
- Implementing the server:
package main
import (
"context"
"log"
"net"
"google.golang.org/grpc"
pb "path/to/generated/proto"
)
type server struct {
pb.UnimplementedGreeterServer
}
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
return &pb.HelloReply{Message: "Hello " + in.Name}, nil
}
func main() {
lis, err := net.Listen("tcp", ":50051")
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
pb.RegisterGreeterServer(s, &server{})
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
- Implementing the client:
package main
import (
"context"
"log"
"time"
"google.golang.org/grpc"
pb "path/to/generated/proto"
)
func main() {
conn, err := grpc.Dial("localhost:50051", grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
c := pb.NewGreeterClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := c.SayHello(ctx, &pb.HelloRequest{Name: "World"})
if err != nil {
log.Fatalf("could not greet: %v", err)
}
log.Printf("Greeting: %s", r.GetMessage())
}
Getting Started
-
Install the Protocol Buffers compiler:
brew install protobuf # macOS apt-get install protobuf-compiler # Ubuntu
-
Install the Go plugins for the protocol compiler:
go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28 go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2
-
Add
$GOPATH/bin
to yourPATH
:export PATH="$PATH:$(go env GOPATH)/bin"
-
Generate Go code from your
.proto
file:protoc --go_out=. --go_opt=paths=source_relative \ --go-grpc_out=. --go-grpc_opt=paths=source_relative \ path/to/your/service.proto
-
Implement your server and client using the
Competitor Comparisons
Go support for Google's protocol buffers
Pros of protobuf
- Lighter weight and more focused on Protocol Buffers serialization
- Easier to integrate into projects that don't require full gRPC functionality
- More straightforward API for basic Protocol Buffers usage
Cons of protobuf
- Lacks built-in gRPC support, requiring additional libraries for RPC functionality
- Less comprehensive documentation compared to grpc-go
- Fewer advanced features and optimizations specific to gRPC use cases
Code Comparison
protobuf:
import "google.golang.org/protobuf/proto"
// Serialize
data, err := proto.Marshal(message)
// Deserialize
err := proto.Unmarshal(data, message)
grpc-go:
import "google.golang.org/grpc"
// Create gRPC client
conn, err := grpc.Dial(address, grpc.WithInsecure())
client := pb.NewServiceClient(conn)
// Make RPC call
response, err := client.Method(context.Background(), request)
The protobuf library focuses on serialization and deserialization of Protocol Buffer messages, while grpc-go provides a complete gRPC implementation, including client-server communication, streaming, and more advanced features. protobuf is suitable for projects that only need Protocol Buffers functionality, while grpc-go is ideal for building full-fledged gRPC applications with robust RPC capabilities.
A simple RPC framework with protobuf service definitions
Pros of Twirp
- Simpler and more lightweight than gRPC-Go
- Easier to set up and use, with less boilerplate code
- Supports both JSON and Protobuf encoding out of the box
Cons of Twirp
- Less feature-rich compared to gRPC-Go (e.g., no streaming support)
- Smaller community and ecosystem
- Limited to HTTP/1.1, while gRPC-Go supports HTTP/2
Code Comparison
Twirp service definition:
service Haberdasher {
rpc MakeHat(Size) returns (Hat);
}
gRPC-Go service definition:
service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
Twirp client usage:
client := haberdasher.NewHaberdasherProtobufClient("http://localhost:8080", &http.Client{})
hat, err := client.MakeHat(ctx, &haberdasher.Size{Inches: 12})
gRPC-Go client usage:
conn, _ := grpc.Dial("localhost:50051", grpc.WithInsecure())
defer conn.Close()
c := pb.NewGreeterClient(conn)
r, err := c.SayHello(ctx, &pb.HelloRequest{Name: "world"})
Both Twirp and gRPC-Go use Protocol Buffers for service definitions, but Twirp's implementation is generally simpler and requires less setup. gRPC-Go offers more advanced features and better performance for complex scenarios, while Twirp focuses on simplicity and ease of use for basic RPC needs.
Best microservices framework in Go, like alibaba Dubbo, but with more features, Scale easily. Try it. Test it. If you feel it's better, use it! 𝐉𝐚𝐯𝐚有 𝐝𝐮𝐛𝐛𝐨, 𝐆𝐨𝐥𝐚𝐧𝐠有𝐫𝐩𝐜𝐱! build for cloud!
Pros of rpcx
- Simpler API and easier to use, with less boilerplate code
- Supports more serialization formats (JSON, MessagePack, Protobuf)
- Built-in service discovery and load balancing
Cons of rpcx
- Less widely adopted and smaller community compared to gRPC
- Fewer language implementations (primarily focused on Go)
- Less extensive documentation and ecosystem support
Code Comparison
rpcx example:
type Arith int
func (t *Arith) Multiply(args *Args, reply *int) error {
*reply = args.A * args.B
return nil
}
s := server.NewServer()
s.RegisterName("Arith", new(Arith))
s.Serve("tcp", "127.0.0.1:8972")
gRPC example:
type server struct{}
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
return &pb.HelloReply{Message: "Hello " + in.Name}, nil
}
s := grpc.NewServer()
pb.RegisterGreeterServer(s, &server{})
lis, _ := net.Listen("tcp", ":50051")
s.Serve(lis)
Both rpcx and gRPC-Go are RPC frameworks for Go, but they have different approaches. rpcx aims for simplicity and ease of use, while gRPC-Go offers a more standardized and cross-language compatible solution. The choice between them depends on project requirements, ecosystem compatibility, and development preferences.
A Go microservices framework
Pros of go-micro
- Higher-level abstraction, simplifying microservices development
- Built-in service discovery and load balancing
- Pluggable architecture for easy customization
Cons of go-micro
- Less widely adopted compared to gRPC
- May have a steeper learning curve for developers new to microservices
- Potentially slower performance due to additional abstraction layers
Code Comparison
go-micro example:
service := micro.NewService(
micro.Name("greeter"),
)
service.Init()
proto.RegisterGreeterHandler(service.Server(), new(Greeter))
service.Run()
grpc-go example:
s := grpc.NewServer()
pb.RegisterGreeterServer(s, &server{})
lis, _ := net.Listen("tcp", ":50051")
s.Serve(lis)
go-micro provides a more concise setup with built-in service registration, while grpc-go requires manual server creation and listening. go-micro abstracts away some lower-level details, making it easier to get started with microservices. However, grpc-go offers more fine-grained control over server configuration and may be preferred for performance-critical applications.
Both libraries support protocol buffers and offer similar RPC functionality, but go-micro includes additional features for microservices architecture out of the box. The choice between them depends on project requirements, team expertise, and desired level of abstraction.
A standard library for microservices.
Pros of kit
- More flexible and modular architecture, allowing for easier customization
- Supports multiple transport protocols beyond just gRPC
- Provides additional features like service discovery and circuit breaking
Cons of kit
- Steeper learning curve due to its more complex architecture
- Less optimized for gRPC-specific use cases
- Smaller community and ecosystem compared to grpc-go
Code Comparison
kit example:
type Service interface {
Hello(name string) (string, error)
}
func makeHelloEndpoint(svc Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(string)
greeting, err := svc.Hello(req)
return greeting, err
}
}
grpc-go example:
type server struct {
pb.UnimplementedGreeterServer
}
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
return &pb.HelloReply{Message: "Hello " + in.GetName()}, nil
}
The kit example shows its more flexible approach, allowing for custom service definitions and endpoint creation. The grpc-go example demonstrates its more straightforward, gRPC-specific implementation. kit offers more flexibility but requires more setup, while grpc-go provides a simpler, more focused approach for gRPC services.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
gRPC-Go
The Go implementation of gRPC: A high performance, open source, general RPC framework that puts mobile and HTTP/2 first. For more information see the Go gRPC docs, or jump directly into the quick start.
Prerequisites
Installation
Simply add the following import to your code, and then go [build|run|test]
will automatically fetch the necessary dependencies:
import "google.golang.org/grpc"
Note: If you are trying to access
grpc-go
from China, see the FAQ below.
Learn more
- Go gRPC docs, which include a quick start and API reference among other resources
- Low-level technical docs from this repository
- Performance benchmark
- Examples
FAQ
I/O Timeout Errors
The golang.org
domain may be blocked from some countries. go get
usually
produces an error like the following when this happens:
$ go get -u google.golang.org/grpc
package google.golang.org/grpc: unrecognized import path "google.golang.org/grpc" (https fetch: Get https://google.golang.org/grpc?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
To build Go code, there are several options:
-
Set up a VPN and access google.golang.org through that.
-
With Go module support: it is possible to use the
replace
feature ofgo mod
to create aliases for golang.org packages. In your project's directory:go mod edit -replace=google.golang.org/grpc=github.com/grpc/grpc-go@latest go mod tidy go mod vendor go build -mod=vendor
Again, this will need to be done for all transitive dependencies hosted on golang.org as well. For details, refer to golang/go issue #28652.
Compiling error, undefined: grpc.SupportPackageIsVersion
Please update to the latest version of gRPC-Go using
go get google.golang.org/grpc
.
How to turn on logging
The default logger is controlled by environment variables. Turn everything on like this:
$ export GRPC_GO_LOG_VERBOSITY_LEVEL=99
$ export GRPC_GO_LOG_SEVERITY_LEVEL=info
The RPC failed with error "code = Unavailable desc = transport is closing"
This error means the connection the RPC is using was closed, and there are many possible reasons, including:
- mis-configured transport credentials, connection failed on handshaking
- bytes disrupted, possibly by a proxy in between
- server shutdown
- Keepalive parameters caused connection shutdown, for example if you have configured your server to terminate connections regularly to trigger DNS lookups. If this is the case, you may want to increase your MaxConnectionAgeGrace, to allow longer RPC calls to finish.
It can be tricky to debug this because the error happens on the client side but the root cause of the connection being closed is on the server side. Turn on logging on both client and server, and see if there are any transport errors.
Top Related Projects
Go support for Google's protocol buffers
A simple RPC framework with protobuf service definitions
Best microservices framework in Go, like alibaba Dubbo, but with more features, Scale easily. Try it. Test it. If you feel it's better, use it! 𝐉𝐚𝐯𝐚有𝐝𝐮𝐛𝐛𝐨, 𝐆𝐨𝐥𝐚𝐧𝐠有𝐫𝐩𝐜𝐱! build for cloud!
A Go microservices framework
A standard library for microservices.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot