goctl quickstart

概述

goctl quickstart 用于快速生成 api、rpc 服务并启动,可以帮助初入 go-zero 的开发者快速上手,开发者在 demo 启动并访问成功后,可以阅读源码简单的了解服务目录结构,go-zero 工程结构,以此来对 go-zero 有个大致的了解。

goctl quickstart 指令

  1. $ goctl quickstart --help
  2. quickly start a project
  3. Usage:
  4. goctl quickstart [flags]
  5. Flags:
  6. -h, --help help for quickstart
  7. -t, --service-type string specify the service type, supported values: [mono, micro] (default "mono")
goctl quickstart - 图1 参数字段goctl quickstart - 图2 参数类型goctl quickstart - 图3 是否必填goctl quickstart - 图4 默认值goctl quickstart - 图5 参数说明
service-typestringNOmono生成 demo 服务类型,mono:单体服务,micro:微服务

使用示例

  • 生成单体服务
  • 生成微服务

如果选择 mono 类型,goctl 会生成一个最小化的 HTTP 服务,然后启动 HTTP 服务。

  1. 快速生成服务
  1. # 进入用户 Home 目录
  2. $ cd ~
  3. # 创建目录
  4. $ mkdir quickstart && cd quickstart
  5. # 生成单体服务
  6. $ goctl quickstart --service-type mono
  7. go: creating new go.mod: module greet
  8. >> Generating quickstart api project...
  9. Done.
  10. >> go mod tidy
  11. go: finding module for package github.com/zeromicro/go-zero/core/logx
  12. go: finding module for package github.com/zeromicro/go-zero/rest
  13. go: finding module for package github.com/zeromicro/go-zero/rest/httpx
  14. go: finding module for package github.com/zeromicro/go-zero/core/conf
  15. go: found github.com/zeromicro/go-zero/core/conf in github.com/zeromicro/go-zero v1.4.3
  16. go: found github.com/zeromicro/go-zero/rest in github.com/zeromicro/go-zero v1.4.3
  17. go: found github.com/zeromicro/go-zero/rest/httpx in github.com/zeromicro/go-zero v1.4.3
  18. go: found github.com/zeromicro/go-zero/core/logx in github.com/zeromicro/go-zero v1.4.3
  19. >> Ready to start an API server...
  20. >> Run 'curl http://127.0.0.1:8888/ping' after service startup...
  21. Starting server at 127.0.0.1:8888...
  1. curl 测试

新打开一个终端,执行 curl 测试

  1. $ curl -i http://127.0.0.1:8888/ping
  2. HTTP/1.1 200 OK
  3. Content-Type: application/json; charset=utf-8
  4. Traceparent: 00-2102678b8c7c5906b792c618b054c9a1-60194e95cceff37f-00
  5. Date: Fri, 06 Jan 2023 08:52:30 GMT
  6. Content-Length: 14
  7. {"msg":"pong"}%

如果选择 micro 类型,goctl 会生成一个 gRPC 服务和一个 HTTP 服务,然后启动 gRPC 服务来和 HTTP 服务进行通讯。

  1. 快速生成服务
  1. # 进入用户 Home 目录
  2. $ cd ~
  3. # 创建目录
  4. $ mkdir quickstart && cd quickstart
  5. # 生成单体服务
  6. $ goctl quickstart --service-type micro
  7. Detected that the "/Users/keson/quickstart/greet" already exists, do you clean up? [y: YES, n: NO]: y
  8. Clean workspace...
  9. go: creating new go.mod: module greet
  10. >> Generating quickstart zRPC project...
  11. [goctl-env]: preparing to check env
  12. [goctl-env]: looking up "protoc"
  13. [goctl-env]: "protoc" is installed
  14. [goctl-env]: looking up "protoc-gen-go"
  15. [goctl-env]: "protoc-gen-go" is installed
  16. [goctl-env]: looking up "protoc-gen-go-grpc"
  17. [goctl-env]: "protoc-gen-go-grpc" is installed
  18. [goctl-env]: congratulations! your goctl environment is ready!
  19. [command]: protoc greet.proto --go_out . --go-grpc_out .
  20. Done.
  21. >> Generating quickstart api project...
  22. Done.
  23. >> go mod tidy
  24. go: finding module for package github.com/zeromicro/go-zero/core/conf
  25. go: finding module for package github.com/zeromicro/go-zero/core/logx
  26. go: finding module for package github.com/zeromicro/go-zero/core/service
  27. go: finding module for package google.golang.org/grpc/reflection
  28. go: finding module for package github.com/zeromicro/go-zero/zrpc
  29. go: finding module for package github.com/zeromicro/go-zero/rest
  30. go: finding module for package github.com/zeromicro/go-zero/rest/httpx
  31. go: finding module for package google.golang.org/grpc
  32. go: finding module for package google.golang.org/grpc/codes
  33. go: finding module for package google.golang.org/grpc/status
  34. go: finding module for package google.golang.org/protobuf/reflect/protoreflect
  35. go: finding module for package google.golang.org/protobuf/runtime/protoimpl
  36. go: found github.com/zeromicro/go-zero/core/conf in github.com/zeromicro/go-zero v1.4.3
  37. go: found github.com/zeromicro/go-zero/rest in github.com/zeromicro/go-zero v1.4.3
  38. go: found github.com/zeromicro/go-zero/rest/httpx in github.com/zeromicro/go-zero v1.4.3
  39. go: found github.com/zeromicro/go-zero/core/logx in github.com/zeromicro/go-zero v1.4.3
  40. go: found github.com/zeromicro/go-zero/zrpc in github.com/zeromicro/go-zero v1.4.3
  41. go: found github.com/zeromicro/go-zero/core/service in github.com/zeromicro/go-zero v1.4.3
  42. go: found google.golang.org/grpc in google.golang.org/grpc v1.51.0
  43. go: found google.golang.org/grpc/reflection in google.golang.org/grpc v1.51.0
  44. go: found google.golang.org/grpc/codes in google.golang.org/grpc v1.51.0
  45. go: found google.golang.org/grpc/status in google.golang.org/grpc v1.51.0
  46. go: found google.golang.org/protobuf/reflect/protoreflect in google.golang.org/protobuf v1.28.1
  47. go: found google.golang.org/protobuf/runtime/protoimpl in google.golang.org/protobuf v1.28.1
  48. >> Ready to start a zRPC server...
  49. >> Ready to start an API server...
  50. >> Run 'curl http://127.0.0.1:8888/ping' after service startup...
  51. Starting rpc server at 127.0.0.1:8080...
  52. Starting server at 127.0.0.1:8888...
  1. curl 测试

新打开一个终端,执行 curl 测试

  1. $ curl -i http://127.0.0.1:8888/ping
  2. HTTP/1.1 200 OK
  3. Content-Type: application/json; charset=utf-8
  4. Traceparent: 00-b7c7bd5b28a49ab97960ed83d2888f1a-4da22e03825041e7-00
  5. Date: Fri, 06 Jan 2023 08:55:33 GMT
  6. Content-Length: 14
  7. {"msg":"pong"}