Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use grpc Framework under golang

2024-07-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today, I would like to share with you the relevant knowledge points about how to use the grpc framework under golang. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

1. What are grpc and protobuf1.1 grpc?

GRPC is a high-performance, open source and general-purpose RPC framework designed for mobile and HTTP/2.

Currently available in C, Java and Go languages are:

Grpc,grpc-java,grpc-go. The C version supports C

Python.jsPhoneObjectiveWhich is supported by PHP and C #.

Grpc follows the HTTP/2 protocol and is a binary protocol.

Grpc is the same as http, the underlying layer is tcp connection, following the socket socket

RPC refers to the remote procedure call, two servers A _ Magi B. A (client) calls the method on B (server). Because it is not in the same memory space, it cannot be called directly and needs to be called through the network.

1.2 protobuf

Protocol Buffer is a protocol.

Protocol Buffer is actually a lightweight & efficient structured data storage format produced by Google, and its performance is really 2-100 times better than Json and XML!

Protobuf has gone through protobuf2 and protobuf3,pb3, which is much simpler than pb2. The current mainstream version is pb3.

Advantages of protobuf:

1. Good performance:

Compression performance

Serialization and sending serialization is faster than Json and XML.

The transmission speed is fast.

two。 Good convenience:

Easy to use and automatically generate serialization and deserialization code

Low maintenance cost, only maintain proto files

Backward compatibility without breaking the old format

Good encryption

3. Cross-language, cross-platform, support for all kinds of languages

Disadvantages of protobuf:

1. Poor versatility, json can be supported by any language, but protobuf needs a special parsing library

two。 Due to poor self-interpretation, the data structure can only be understood through proto files.

Download the protobuf tool on the grpc2.1 official website under 2.go

Official website: https://github.com/protocolbuffers/protobuf/releases

2.2 download go's dependency package go get github.com/golang/protobuf/protoc-gen-go2.3 to write the proto file

Option go_package = ". /; proto"

Explanation:

. /;: the path to the generated file

Proto: the package name of the generated file

Hello.proto

Syntax = "proto3"; package services;option go_package = ". /; proto"; service Greeter {rpc SayHello (HelloRequest) returns (HelloReply);} message HelloRequest {string name = 1;} message HelloReply {string message = 1;} 2.4 generate hello.pb.proto file cd to proto directory command: protoc-I. Hello.proto-go_out=plugins=grpc:. Command interpretation: protoc-I.: find the hello.proto file under the current path-- go_out=plugins=grpc:. Generate proto files for go language under the current path

2.5 write server side code

Package mainimport ("context"file_test/grpc_go/proto"net"google.golang.org/grpc") type Server struct {} / / Business logic func (s * Server) SayHello (ctx context.Context, request * proto.HelloRequest) (* proto.HelloReply, error) {res: = & proto.HelloReply {Message: "hello" + request.Name,} return res Nil} / / start rpc's server service func start () {/ / 1. Instantiate server g: = grpc.NewServer () / / 2. Register the logic to the server proto.RegisterGreeterServer (gjinghold Server {}) / / 3. Start server lis,err:=net.Listen ("tcp") "127.0.0.1 if err 8081") if err! = nil {panic ("listening error:" + err.Error ())} err = g.Serve (lis) if err! = nil {panic ("Startup error:" + err.Error ())}} func main () {start ()} 2.6 client The code package mainimport ("context"file_test/grpc_go/proto"fmt"google.golang.org/grpc") / / rpc calls func clientRpc (body map [string] string) (res * proto.HelloReply Err error) {name: = body ["name"] conn, err: = grpc.Dial ("127.0.0.1 defer conn.Close 8081", grpc.WithInsecure ()) if err! = nil {return nil,err} defer conn.Close () rpc: = proto.NewGreeterClient (conn) response, err: = rpc.SayHello (context.Background () & proto.HelloRequest {Name: name}) if erratic error nil {return nil,err} return response,nil} / / business code func start () {body: = make (mapping [string] string) body ["name"] = "jeff" response,err: = clientRpc (body) if erratic accounnil {fmt.Println ("rpc call failed:" Err) return} fmt.Println (response.Message)} func main () {start ()} / / result: practice of hello jeff2.7 python and go calling each other (cross-language calls)

Proto files are shared.

1. Open the server side of python and the client side test of go.

two。 Open the server side of go and test the Clint side of python.

These are all the contents of this article entitled "how to use the grpc Framework under golang". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report