Using gRPC + Protocol Buffers in Golang to implement efficient RPC services

Using gRPC + Protocol Buffers in Golang to implement efficient RPC services

introduction

With the popularity of microservice architecture, remote procedure calls (RPC) are becoming more and more important in modern applications. gRPC is a high-performance, open source, and general RPC framework developed by Google. It uses HTTP/2 as the transmission protocol and supports multiple languages, including Go. This article will introduce in detail how to use gRPC in Golang to implement RPC services, including environment construction, basic concepts, code examples, and some best practices.

1. Environment Construction

1. Install Go

First, make sure you have installed and deployed the Go environment. You can check it with the following command:

go version

If you install the Go environment, you can see the current version information of Go as shown above. My current version isgo1.23.1, for the latest version, if you have not installed it yet, please visit Go Official Website Download and install according to your needs.

2. Install grpc and Protocol Buffers

Since gRPC defaults to Protocol Buffers Protocol for data transmission,Protocol Buffers It is a structured data serialization protocol open sourced by Google. We will use it later. Protocol Buffers Define and constrain our services, so next we need to install grpc and Protocol Buffers Related libraries and plugins.Protocol Buffers basic introduction and basic syntax .

  • Install gRPC and the Protocol Buffers Go plugin:

    go install google.golang.org/protobuf/cmd/protoc-gen-go@latest go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest

    go install The command will install the Go-related plugins in the bin directory under the GO installation directory. Here we need to make sure that protoc-gen-go and protoc-gen-go-grpc Add the executable file path to the system executable files $PATH middle.

  • Add Go's bin directory to the system executable directory /usr/local/bin:

    export PATH=$PATH:$(go env GOPATH)/bin

    You can add the above command to your ~/.bashrc or ~/.bash_profile file so that it is automatically loaded every time you log in.

  • Check whether the plugin is installed successfully:

    protoc-gen-go --version protoc-gen-go-grpc --version

    If the installation is successful, the corresponding plug-in version information will be output here.

3. Install Protocol Buffers Compiler

Then, download and install the Protocol Buffers Compiler (protoc). You can GitHub Releases Download the version appropriate for your operating system and add its path to $PATH.

  • Then use wget to download. Take version 28.2 as an example:

    wget https://github.com/protocolbuffers/protobuf/releases/download/v28.2/protoc-28.2-linux-x86_64.zip
  • Unzip the downloaded zip file:

    unzip protoc-28.2-linux-x86_64.zip -d protoc
  • Move the file and add it to the system executable directory /usr/local/bin:

    sudo mv protoc/bin/protoc /usr/local/bin/ sudo mv protoc/include/* /usr/local/include/
  • Check whether protoc is installed successfully:

    protoc --version

    If the output libprotoc 28.2, indicating that the installation is successful.

4. Create the project structure

Create a new Go project and set up the following directory structure:

grpc-example/
    ├── server/
    │   └── main.go
    ├── proto/
    │   └── example.proto
    └── client/
        └── main.go

Directory structure description

  • server: grpc server code directory
  • proto: grpc serviceProtocol Buffers
  • client: grpc client code directory

2. Define gRPC Service

exist proto/example.proto Define our gRPC service and message format in:

syntax = "proto3";

package example;

// define service
service Greeter {
    // define an RPC method
    rpc SayHello(HelloRequest) returns (HelloReply);
}

//  define Request 
message HelloRequest {
    string name = 1;
}

// define Response
message HelloReply {
    string message = 1;
}

Here we pass Protocol Buffers Defines our services and the structures of service requests and responses. Protocol Buffers We will introduce the relevant information and syntax in the next blog post.

1. Generate Go code

exist grpc-example In the directory, execute the following command to generate Go code through protoc:

protoc --go_out=. --go-grpc_out=. proto/example.proto

The above command will generate proto/example.pb.go and proto/example_grpc.pb.go The file contains our poroto The structure and methods of the service defined in the file.

3. Implementing gRPC Server

exist server/main.go Implementing the gRPC server in:

package main

import (
    "context"
    "log"
    "net"

    "google.golang.org/grpc"
    pb "grpc-example/proto"
)

// Server struct
type server struct {
    pb.UnimplementedGreeterServer
}

// Implementing the SayHello Method
func (s *server) SayHello(ctx context.Context, req *pb.HelloRequest) (*pb.HelloReply, error) {
    log.Printf("Received: %v", req.Name)
    return &pb.HelloReply{Message: "Hello " + req.Name}, nil
}

func main() {
    // Listening Port
    lis, err := net.Listen("tcp", ":50051")
    if err != nil {
        log.Fatalf("failed to listen: %v", err)
    }

    // Creating a gRPC Server
    grpcServer := grpc.NewServer()
    pb.RegisterGreeterServer(grpcServer, &server{})

    log.Println("Starting gRPC server on port :50051")
    if err := grpcServer.Serve(lis); err != nil {
        log.Fatalf("failed to serve: %v", err)
    }
}

Code Explanation

  • server structure: Achieved GreeterServer interface.
  • SayHello Method: Processes incoming requests and returns responses.
  • main function: Set up TCP listening and gRPC server and start them.

4. Implementing the gRPC Client

exist client/main.go Implement the gRPC client in:

package main

import (
    "context"
    "log"
    "time"

    "google.golang.org/grpc"
    pb "grpc-example/proto"
)

func main() {
    // Connecting to a gRPC server 

    conn, err := grpc.Dial("localhost:50051", grpc.WithInsecure())
    if err != nil {
        log.Fatalf("did not connect: %v", err)
    }
    defer conn.Close()

    // Creating a Client
    client := pb.NewGreeterClient(conn)

    // Create context, set timeout
    ctx, cancel := context.WithTimeout(context.Background(), time.Second)
    defer cancel()

    // Send Request
    response, err := client.SayHello(ctx, &pb.HelloRequest{Name: "World"})
    if err != nil {
        log.Fatalf("could not greet: %v", err)
    }

    log.Printf("Greeting: %s", response.Message)
}

Code Explanation

  • grpc.Dial: Connect to a gRPC server.
  • NewGreeterClient: Create a client.
  • SayHello: Call a remote procedure and get a response.

5. Run gRPC service

1. Start the gRPC server

exist grpc-example/server Run in the directory:

go run main.go

2. Start the gRPC client

exist grpc-example/client Run in the directory:

go run main.go

The client receives the following message:

Greeting: Hello World

VI. Best Practices

1. Use Protobuf for version control

Versioning is very important with Protobuf. Backward compatibility can be achieved by adding fields without removing existing fields.

2. Error handling

Make sure to handle errors appropriately in both the server and the client. gRPC provides some built-in error codes (such as NOT_FOUND,INVALID_ARGUMENT), which can be used to indicate different types of errors.

3. Middleware and interceptors

With gRPC middleware, you can add logic before and after requests, such as logging, monitoring, and authentication.

4. Use gRPC Gateway

If a RESTful interface is required, you can use gRPC Gateway to automatically convert gRPC to a REST API. This is especially useful in a microservice architecture.

VII. Conclusion

This article introduces how to use gRPC in Golang to implement RPC services, including environment construction, service definition, server and client implementation. gRPC has become a popular choice in modern microservice architecture with its high performance and ease of use. I hope this article can help you quickly get started with gRPC and apply it in actual projects. Through continuous learning and practice, you can have a deeper understanding of the functions and advantages of gRPC.

If you have any questions or suggestions, please leave a comment below. Thanks for reading!

No Comments

Send Comment Edit Comment

|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠(ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ°Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
Emoticons
Emoji
Little Dinosaur
flower!
Previous
Next