gRPC demo code generation
Overview
After completing the goctl installation, we can create a minimal gRPC service to get an overview of goctl's go-zero gRPC service.
Task Targets
- Learn how to create a minimized gRPC service using goctl
- Preliminary understanding of the project structure of go-zero
- Preliminary method of gRPC local debugging
Preparing
Code Generation
# Create workspaces and enter the directory
$ mkdir -p ~/workspace/rpc && cd ~/workspace/rpc
# Execute instructions generated demo service
$ goctl rpc new demo
Done.
After executing the instruction, a demo directory will be generated under the current directory that contains a minimized gRPC service. We will check the directory structure of the service.
# Enter the demo service directory
$ cd ~/workspace/rpc/demo
# view file list
$ ls
demo demo.go demo.proto democlient etc go.mod internal
# View directory interface
$ tree
.
├── demo
│ ├── demo.pb.go
│ └── demo_grpc.pb.go
├── demo.go
├── demo.proto
├── democlient
│ └── demo.go
├── etc
│ └── demo.yaml
├── go.mod
└── internal
├── config
│ └── config.go
├── logic
│ └── pinglogic.go
├── server
│ └── demoserver.go
└── svc
└── servicecontext.go
note
API, RPC, Job Directory structure is similar to what the go-zero project structure can look at Project Structure
Write simple logic code
After completing the above code generation, we can find ~/workspace/api/demo/internal/logic/demologic.go
files, replace codes at line 29
as follows :
return &demo.Response{
Pong:"pong",
}, nil
Then set your profile ~/workspace/rpc/depo/etc/demo.yaml
remove 3
to 7 to
lines of content, then add-on Mode: dev
to end, making configuration file content:
Name: demo.rpc
ListenOn: 0.0.0.0:8080
Mode: dev
:::Note goctl generates minimized gRPC service by registering information about the current service from ETCD Register, so registration is not required for this demonstration, so the registration center configuration in the configuration file has been deleted. :::
Start service
After writing the above code, we can start the service with the following instructions:
# Enter service directory
$ cd ~/workspace/rpc/demo
# Organize dependencies
$ go mod tidy
# Run go program
$ go go run demo.go
When you see the following output Starting rpc server at 0.0.0.0:8080...
indicates that the service has been successfully started, then we come to visit the gRPC service.
- grpcurl 访问
- grpcui 访问
- Postman 中访问
$ grpcurl -plaintext 127.0.0.1:8080 demo.Demo/Ping
``
on behalf of your service has been successfully launched when you see the following output in the terminal.
```json
{
"pong": "pong"
}
NOTE
grpcurl is a command-line tool for accessing gRPC services, for details, please refer to 《grpcurl》
Start the grpcui service at the terminal:
$ grpcui -plaintext 127.0.0.1:8080
then visit the Ping
interface in the browser. The service on behalf of you has started successfully when you see the following output.
{
"pong": "pong"
}
note
grpcui is a gRPC UI debugging tool to access gRPC services. See Grpcui
The service on your behalf has been successfully launched when you see the following output in Postman.
{
"message": "me"
}
When you come here following the steps in the document, congratulations, you have completed the creation and startup of the simplest go-zero api service. For instructions on using the goctl
tool, please refer to "CLI Tools", for a complete description of the go-zero api service, please refer to 《HTTP Server》.