zero

command module
v0.0.0-...-eea11df Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 8, 2024 License: Apache-2.0 Imports: 0 Imported by: 0

README

go-zero 使用

go-zero框架概述 goctl工具使用

1. goctl 安装

  1. 升级goctl 和 protoc,protoc-gen-go,protoc-gen-go-grpc
   GO111MODULE=on 
   go install github.com/zeromicro/go-zero/tools/goctl@latest
   goctl env check --install --verbose --force
  1. 安装protoc-gen-go (建议使用goctl安装即可)
#   查看所有版本
   go list -m -versions google.golang.org/grpc/cmd/protoc-gen-go-grpc
   go list -m -versions google.golang.org/grpc/cmd/protoc
   
   go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
#   安装
   go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
   go install google.golang.org/grpc/cmd/protoc@latest
   
#   查看已安装版本
   protoc-gen-go-grpc --version

2. goctl 创建一个go-zero demo仓库

# 生成 snake case 文件和目录示例
goctl api new blog --style go_zero

goctl rpc new blog --style go_zero

3. goctl 高级用法

1. 替换代码模板
   goctl template init
   编辑 ~/.goctl/${goctl版本号}/api/handler.tpl

2. 生成api代码
   goctl api go -api blog.api -dir ../ --style go_zero
   
3. 格式化api代码
   goctl api format --dir blog.api
   
4. 生成api.ts代码
   goctl api ts -api blog.api -dir ../ts

4. goctl goctl-swagger生成文档

https://github.com/zeromicro/goctl-swagger

1. 编译goctl-swagger插件
   GOPROXY=https://goproxy.cn/,direct go install github.com/zeromicro/goctl-swagger@latest

2. 使用goctl-swagger插件
   goctl api plugin -plugin goctl-swagger="swagger -filename user.json" -api user.api -dir .

项目启动

1.本地启动项目

启动etcd(可选,使用直连rpc模式时不需要)

etcd

启动rpc服务

go run service/rpc/blog/blog.go -f service/rpc/blog/etc/blog-rpc.yaml

启动api服务

go run service/api/blog/blog.go -f service/api/blog/etc/blog-api.yaml
go run service/api/admin/admin.go -f service/api/admin/etc/admin-api.yaml

2.使用nacos配置启动

go run service/rpc/blog/blog.go
go run service/api/blog/blog.go
go run service/api/admin/admin.go

3.使用docker启动

构造镜像

goctl docker --go service/rpc/blog/blog.go --exe blog

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
internal
service
api/admin/internal/handler
Code generated by goctl.
Code generated by goctl.
api/admin/internal/types
Code generated by goctl.
Code generated by goctl.
api/blog/internal/handler
Code generated by goctl.
Code generated by goctl.
api/blog/internal/types
Code generated by goctl.
Code generated by goctl.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL