Quick Start
Overview
This paper will describe how to quickly develop a rpc server
Sample
Preparation
We do the following execution to generate a proto file and pb file.
# Create demo service directory
$ mkdir demo && cd demo
# go mod initializing
$ go mod init demo
# Generate greet. roto file
$ goctl rpc -o greet.proto
# pun pb.go file
$ protoc greet.proto --go_out=. --go-grpc_out=.
# Create client directory
$ mkdir client && cd client
# Add profile
$ mkdir etc && cd etc
$ touch gret-client. aml
# Add client.go file
$ touch server.go
yaml configuration content and server.go code below
- etc/greet-server.yaml
- server.go
Name: greet.rpc
ListenOn: 127.0.0.1:8080
func main() {
func main() {
var serverConf zrpc.RpcServerConf
conf.MustLoad("etc/greet-server.yaml", &serverConf)
s := zrpc.MustNewServer(serverConf, func(server *grpc.Server) {
greet.RegisterGreetServer(server, &exampleServer{})
})
defer s.Stop()
s.Start()
}
}
type exampleServer struct {
greet.UnimplementedGreetServer
}
func (e *exampleServer) Ping(ctx context.Context, request *greet.Request) (*greet.Response, error) {
// fill your logic here
return &greet.Response{}, nil
}
small trick
If you also find it troublesome to write the code, please try goctl scroll code generated, refer to goctl rpc
More gRPC usage (service register, service discovery, intermediate use etc.) can be consulted rpc service registration