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

  1. Learn how to create a minimized gRPC service using goctl
  2. Preliminary understanding of the project structure of go-zero
  3. Preliminary method of gRPC local debugging

Preparing

  1. Complete golang installation
  2. Complete goctl installation

Code Generation

  1. # Create workspaces and enter the directory
  2. $ mkdir -p ~/workspace/rpc && cd ~/workspace/rpc
  3. # Execute instructions generated demo service
  4. $ goctl rpc new demo
  5. 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.

  1. # Enter the demo service directory
  2. $ cd ~/workspace/rpc/demo
  3. # view file list
  4. $ ls
  5. demo demo.go demo.proto democlient etc go.mod internal
  6. # View directory interface
  7. $ tree
  8. .
  9. ├── demo
  10. ├── demo.pb.go
  11. └── demo_grpc.pb.go
  12. ├── demo.go
  13. ├── demo.proto
  14. ├── democlient
  15. └── demo.go
  16. ├── etc
  17. └── demo.yaml
  18. ├── go.mod
  19. └── internal
  20. ├── config
  21. └── config.go
  22. ├── logic
  23. └── pinglogic.go
  24. ├── server
  25. └── demoserver.go
  26. └── svc
  27. └── servicecontext.go
gRPC demo code generation - 图1note

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 :

  1. return &demo.Response{
  2. Pong:"pong",
  3. }, nil

Then set your profile ~/workspace/rpc/depo/etc/demo.yamlremove 3 to 7 to lines of content, then add-on Mode: dev to end, making configuration file content:

  1. Name: demo.rpc
  2. ListenOn: 0.0.0.0:8080
  3. 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:

  1. # Enter service directory
  2. $ cd ~/workspace/rpc/demo
  3. # Organize dependencies
  4. $ go mod tidy
  5. # Run go program
  6. $ 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 中访问
  1. $ grpcurl -plaintext 127.0.0.1:8080 demo.Demo/Ping
  2. ``
  3. on behalf of your service has been successfully launched when you see the following output in the terminal.
  4. ```json
  5. {
  6. "pong": "pong"
  7. }
gRPC demo code generation - 图2NOTE

grpcurl is a command-line tool for accessing gRPC services, for details, please refer to 《grpcurl》

Start the grpcui service at the terminal:

  1. $ 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.

  1. {
  2. "pong": "pong"
  3. }

postman

Access in grpcui

gRPC demo code generation - 图4note

grpcui is a gRPC UI debugging tool to access gRPC services. See Grpcui

postman

Access in Postman

The service on your behalf has been successfully launched when you see the following output in Postman.

  1. {
  2. "message": "me"
  3. }

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》.