Quick Start
Overview
This paper will describe how to quickly use gRPC customers to connect to rpc.
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 client.go
Reference configuration and content
- etc/greet-client.yaml
- client.go
Target: 127.0.0.1:8080
func main() {
var clientConf zrpc.RpcClientConf
conf.MustLoad("etc/client.yaml", &clientConf)
conn := zrpc.MustNewClient(clientConf)
client := greet.NewGreetClient(conn.Conn())
resp, err := client.Ping(context.Background(), &greet.Request{})
if err != nil {
log.Fatal(err)
return
}
log.Println(resp)
}
small trick
If you also find it troublesome to write the code, please try goctl scroll code generated, refer to goctl rpc
More gRPC connections (service register, service discovery, cluster direct, intermediate use etc.) can be consulted rpc service connection