In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to use grpc in Golang, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
1. Grpc installation
Pip install-upgrade pip
Pip install grpcio-user
Pip install protobuf-user
Pip install grpcio-tools-user
Sudo yum-y install protobuf-compiler protobuf-static protobuf protobuf-devel
Dnf install protobuf-compiler.x86_64
Pip install googleapis-common-protos-user
two。 Import grpc package
Go get-u google.golang.org/grpcgo get-u github.com/golang/protobuf/protoc-gen-go
3. Write test.proto files
/ / specified version
Syntax = "proto3"
Option objc_class_prefix = "HLW"
/ / define the package name
Package demo
/ / define services
Service ServerBase {
/ / define the interface
/ / method
Rpc MakeMD5 (Request) returns (Response) {}
Rpc SayHello (HelloRequest) returns (HelloReplay) {}
}
/ / structure of the request
Message HelloRequest {
/ / Type field = identification number
String name = 1
}
/ / returned structure
Message HelloReplay {
String message = 1
}
/ / define the request structure
Message Request {
String Data = 1
}
/ / define the return interface body data
Message Response {
String Msg = 1
}
4. Generate .go file
Protoc-go_out=plugins=grpc:. Test.proto
When the directory is low, the test.pb.go file will be generated automatically.
5. Simply write server.go
Package main
Import (
"crypto/md5"
"errors"
"fmt"
"github.com/srlemon/note/grpc_"
"golang.org/x/net/context"
"google.golang.org/grpc"
"log"
"net"
)
Const (
PORT = ": 5003"
)
Func main () {
/ / enable monitoring
Lis, err: = net.Listen ("tcp", PORT)
If err! = nil {
Log.Fatal (err)
}
/ / new
S: = grpc.NewServer ()
/ / Registration service
Demo.RegisterServerBaseServer (s, & Serve {})
Log.Println ("rpc service is enabled")
S.Serve (lis)
}
/ / Serve server
Type Serve struct {
}
/ / MakeMD5 method
Func (s * Serve) MakeMD5 (ctx context.Context, req * demo.Request) (ret * demo.Response, err error) {
If req = = nil {
Err = errors.New ("request data is empty")
Return
}
Md: = md5.New ()
Data: = md.Sum ([] byte (req.Data))
Ret = new (demo.Response)
Ret.Msg = fmt.Sprintf ("% x", data)
Return
}
/ / SayHello method
Func (s * Serve) SayHello (ctc context.Context, req * demo.HelloRequest) (ret * demo.HelloReplay, err error) {
If req = = nil {
Err = errors.New ("request data is empty")
Return
}
Ret = new (demo.HelloReplay)
Ret.Message = req.Name
Return
}
Go run server.go enables rpc service
6. Write client.go file
Package main
Import (
"fmt"
Demo "github.com/srlemon/note/grpc_"
"golang.org/x/net/context"
"google.golang.org/grpc"
"log"
)
Const (
Addr = "127.0.0.1purl 5003"
)
Func main () {
Ctx: = context.Background ()
/ / connect to the agent
Conn, err: = grpc.Dial (addr, grpc.WithInsecure ())
If err! = nil {
Log.Fatal ("did no connect", err)
}
Defer conn.Close ()
/ / generate a client
Client: = demo.NewServerBaseClient (conn)
Var (
Data * demo.Request
Res * demo.Response
)
Data = new (demo.Request)
Data.Data = ", and black"
/ / use the server method
If res, err = client.MakeMD5 (ctx, data); err! = nil {
Log.Fatal ("sssssssssss", err)
}
/ / output the encrypted information
Fmt.Println (string (res.Msg))
/ / use the server method
_ d, _: = client.SayHello (ctx, & demo.HelloRequest {Name: "ha"})
/ / output what you want to say
Fmt.Println (_ d.Message)
}
Go run client.go
Terminal output:
The above is all the contents of the article "how to use grpc in Golang". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.