Giter Site home page Giter Site logo

zeromicro / go-zero Goto Github PK

View Code? Open in Web Editor NEW
27.6K 308.0 3.8K 20.52 MB

A cloud-native Go microservices framework with cli tool for productivity.

Home Page: https://go-zero.dev

License: MIT License

Go 98.49% Makefile 0.06% ANTLR 0.13% Dockerfile 0.05% Smarty 1.04% Shell 0.08% TypeScript 0.10% Lua 0.06%
golang microservice rpc-framework web-framework gozero rpc restful-api code-generation go-zero goctl

go-zero's Introduction

go-zero

go-zero is a web and rpc framework with lots of builtin engineering practices. It’s born to ensure the stability of the busy services with resilience design and has been serving sites with tens of millions of users for years.

Go codecov Go Report Card Release Go Reference Awesome Go License: MIT Discord

🤷‍ What is go-zero?

English | 简体中文

go-zero - A web & rpc framework written in Go. | Product Hunt

go-zero (listed in CNCF Landscape: https://landscape.cncf.io/?selected=go-zero) is a web and rpc framework with lots of builtin engineering practices. It’s born to ensure the stability of the busy services with resilience design and has been serving sites with tens of millions of users for years.

go-zero contains simple API description syntax and code generation tool called goctl. You can generate Go, iOS, Android, Kotlin, Dart, TypeScript, JavaScript from .api files with goctl.

Advantages of go-zero:

  • Improves the stability of the services with tens of millions of daily active users
  • Builtin chained timeout control, concurrency control, rate limit, adaptive circuit breaker, adaptive load shedding, even no configuration needed
  • Builtin middlewares also can be integrated into your frameworks
  • Simple API syntax, one command to generate a couple of different languages
  • Auto validate the request parameters from clients
  • Plenty of builtin microservice management and concurrent toolkits

Architecture

Backgrounds of go-zero

At the beginning of 2018, we decided to re-design our system, from monolithic architecture with Java+MongoDB to microservice architecture. After research and comparison, we chose to: In early 2018, we embarked on a transformative journey to redesign our system, transitioning from a monolithic architecture built with Java and MongoDB to a microservices architecture. After careful research and comparison, we made a deliberate choice to:

  • Go Beyond with Golang

    • Great performance
    • Simple syntax
    • Proven engineering efficiency
    • Extreme deployment experience
    • Less server resource consumption
  • Self-Design Our Microservice Architecture

    • Microservice architecture facilitates the creation of scalable, flexible, and maintainable software systems with independent, reusable components.
    • Easy to locate the problems within microservices.
    • Easy to extend the features by adding or modifying specific microservices without impacting the entire system.

Design considerations on go-zero

By designing the microservice architecture, we expected to ensure stability, as well as productivity. And from just the beginning, we have the following design principles:

  • Keep it simple
  • High availability
  • Stable on high concurrency
  • Easy to extend
  • Resilience design, failure-oriented programming
  • Try best to be friendly to the business logic development, encapsulate the complexity
  • One thing, one way

After almost half a year, we finished the transfer from a monolithic system to microservice system and deployed on August 2018. The new system guaranteed business growth and system stability.

The implementation and features of go-zero

go-zero is a web and rpc framework that integrates lots of engineering practices. The features are mainly listed below:

  • Powerful tool included, less code to write
  • Simple interfaces
  • Fully compatible with net/http
  • Middlewares are supported, easy to extend
  • High performance
  • Failure-oriented programming, resilience design
  • Builtin service discovery, load balancing
  • Builtin concurrency control, adaptive circuit breaker, adaptive load shedding, auto-trigger, auto recover
  • Auto validation of API request parameters
  • Chained timeout control
  • Auto management of data caching
  • Call tracing, metrics, and monitoring
  • High concurrency protected

As below, go-zero protects the system with a couple of layers and mechanisms:

Resilience

The simplified architecture that we use with go-zero

image

Installation

Run the following command under your project:

go get -u github.com/zeromicro/go-zero

Quick Start

  1. Full examples can be checked out from below:

    Rapid development of microservice systems

    Rapid development of microservice systems - multiple RPCs

  2. Install goctl

    goctlcan be read as go control. goctl means not to be controlled by code, instead, we control it. The inside go is not golang. At the very beginning, I was expecting it to help us improve productivity, and make our lives easier.

    # for Go
    go install github.com/zeromicro/go-zero/tools/goctl@latest
    
    # For Mac
    brew install goctl
    
    # docker for amd64 architecture
    docker pull kevinwan/goctl
    # run goctl like
    docker run --rm -it -v `pwd`:/app kevinwan/goctl --help
    
    # docker for arm64(Mac) architecture
    docker pull kevinwan/goctl:latest-arm64
    # run goctl like
    docker run --rm -it -v `pwd`:/app kevinwan/goctl:latest-arm64 --help

    make sure goctl is executable.

  3. Create the API file, like greet.api, you can install the plugin of goctl in vs code, api syntax is supported.

    type (
      Request {
        Name string `path:"name,options=[you,me]"` // parameters are auto validated
      }
    
      Response {
        Message string `json:"message"`
      }
    )
    
    service greet-api {
      @handler GreetHandler
      get /greet/from/:name(Request) returns (Response)
    }

    the .api files also can be generated by goctl, like below:

    goctl api -o greet.api
  4. Generate the go server-side code

    goctl api go -api greet.api -dir greet

    the generated files look like:

    ├── greet
    │   ├── etc
    │   │   └── greet-api.yaml        // configuration file
    │   ├── greet.go                  // main file
    │   └── internal
    │       ├── config
    │       │   └── config.go         // configuration definition
    │       ├── handler
    │       │   ├── greethandler.go   // get/put/post/delete routes are defined here
    │       │   └── routes.go         // routes list
    │       ├── logic
    │       │   └── greetlogic.go     // request logic can be written here
    │       ├── svc
    │       │   └── servicecontext.go // service context, mysql/redis can be passed in here
    │       └── types
    │           └── types.go          // request/response defined here
    └── greet.api                     // api description file
    

    the generated code can be run directly:

    cd greet
    go mod init
    go mod tidy
    go run greet.go -f etc/greet-api.yaml

    by default, it’s listening on port 8888, while it can be changed in the configuration file.

    you can check it by curl:

    curl -i http://localhost:8888/greet/from/you

    the response looks like below:

    HTTP/1.1 200 OK
    Date: Sun, 30 Aug 2020 15:32:35 GMT
    Content-Length: 0
  5. Write the business logic code

    • the dependencies can be passed into the logic within servicecontext.go, like mysql, redis, etc.
    • add the logic code in a logic package according to .api file
  6. Generate code like Java, TypeScript, Dart, JavaScript, etc. just from the api file

    goctl api java -api greet.api -dir greet
    goctl api dart -api greet.api -dir greet
    ...

Benchmark

benchmark

Checkout the test code

Documents

Chat group

Join the chat via https://discord.gg/4JQvC5A4Fe

Cloud Native Landscape

   

go-zero enlisted in the CNCF Cloud Native Landscape.

Give a Star! ⭐

If you like or are using this project to learn or start your solution, please give it a star. Thanks!

Buy me a coffee

Buy Me A Coffee

go-zero's People

Contributors

bittoy avatar chenquan avatar chensylz avatar chowyu12 avatar code-fight avatar dependabot[bot] avatar dfang avatar foliet avatar fondoger avatar fynxiu avatar heyanfu avatar kesonan avatar kevwan avatar kingxt avatar markjoyma avatar miaogaolin avatar mikaelemmmm avatar poabob avatar reatang avatar shenbaise9527 avatar sjatsh avatar suyghur avatar szpnygo avatar taobig avatar testwill avatar wsx864321 avatar wubenqi avatar xiaowei520 avatar zcong1993 avatar zhoushuguang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

go-zero's Issues

redis pool是否应该支持可配置?

目前看了下core/store/redis里面redis连接的代码,redis pool参数都是默认的,满足不了所有公司的需求,比如redis的最大连接数、redis的最大空闲数以及超时时间等,这样会更灵活一些?

执行go run check.go -f etc/check.yaml报如下错误

go run check.go -f etc/check.yaml

Starting rpc server at 127.0.0.1:8080...
{"level":"warn","ts":"2020-10-14T15:29:28.833+0800","caller":"clientv3/retry_interceptor.go:62","msg":"retrying of unary invoker failed","target":"passthrough:///localhost:2379","attempt":0,"error":"rpc error: code = DeadlineExceeded desc = latest balancer error: connection closed"}
{"@timestamp":"2020-10-14T15:29:28.834+08","level":"error","content":"server.go:83 context deadline exceeded"}
panic: context deadline exceeded

goroutine 1 [running]:
github.com/tal-tech/go-zero/zrpc.(*RpcServer).Start(0xc0001249a0)
/root/go/pkg/mod/github.com/tal-tech/[email protected]/zrpc/server.go:84 +0xce
main.main()
/root/golang/bookstore/rpc/check/check.go:37 +0x33f
exit status 2

.api文件是否可以支持include语法

  • 一旦接口数量变多,将不同接口定义到不同的.api文件中进行归类就变得自然而然了。在一个入口的.api文件中include多个.api文件,goctl执行时只需要指定一个入口的.api文件。
  • 当然,这是一个锦上添花的功能,目前的方式依然可以正常使用。

建议:增加debug级别的日志;所有级别的日志统一增加文件名以及行数内容

  • 所有日志统一格式

目前有些级别的日志有文件名和行数内容输出,比如error级别日志,有些级别的日志无文件名和行数内容输出,例如

{"@timestamp":"2020-10-25T18:39:21.376+08","level":"slow","content":"hello slow world"}
{"@timestamp":"2020-10-25T18:39:21.376+08","level":"stat","content":"hello stat world"}
{"@timestamp":"2020-10-25T18:39:21.376+08","level":"info","duration":"61000.0ms","content":"hello"}
{"@timestamp":"2020-10-25T18:39:21.376+08","level":"error","duration":"61000.0ms","content":"logging.go:28 hello"}
{"@timestamp":"2020-10-25T18:39:21.376+08","level":"error","duration":"1000.0ms","content":"logging.go:10 world"}

建议所有日志统一添加文件名以及行数

  • 建议类似zap添加debug级别的日志

时间轮moveTask函数处理有问题

timer.item.removed = true
newItem := &timingEntry{
baseEntry: task,
value: timer.item.value,
}
tw.slots[pos].PushBack(newItem)
tw.setTimerPosition(pos, newItem)
以上代码中,旧的task的removed被标记为了true,但是新、旧task在timers里都是同样的key,当执行到旧task时从timers里移除了该key,当再move或remove新task的时候由于key已经不存在,导致执行失效了。

一下代码可以测试出bug:

const testStep = time.Second

func TestMoveTask(t *testing.T) {
keys := make([]string, 0)
tw, _ := NewTimingWheel(testStep, 10, func(k, v interface{}) {
keys = append(keys, k.(string))
fmt.Printf("%v\n", k)
})

fmt.Printf("第一次SetTimer\n")
tw.SetTimer("first", "v1", testStep * 8)
time.Sleep(testStep * 6)

fmt.Printf("move task\n")
tw.MoveTimer("first", testStep * 7)

time.Sleep(testStep * 3)

fmt.Printf("remove task\n")
tw.removeTask("first")

time.Sleep(testStep * 30)

assert.Equal(t, 0, len(keys))

}

82:71: expected selector or type assertion, found ')' (and 5 more errors)

根据这个文档来的 https://www.yuque.com/tal-tech/go-zero/nkg20f

执行的时候报错

$ goctl model mysql datasource -url="root:root@tcp(127.0.0.1:3306)/blog" -table="posts" -dir ./model

82:71: expected selector or type assertion, found ')' (and 5 more errors)

根据db生成model代码

数据库是这样的

image

CREATE TABLE `posts` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(255) DEFAULT NULL,
  `content` text,
  PRIMARY KEY (`id`),
  UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

core/executors有数据丢失的风险

语鹊的文档地址:https://www.yuque.com/tal-tech/go-zero/zig6zy

文中说的“带上 dts.insertExecutor.Wait(),当然要等待全部的 goroutine task 完成”,我做了个实验,代码如下:
executor := executors.NewBulkExecutor(
myExecutor,
executors.WithBulkInterval(time.Second*3), // 3s会自动刷一次container中task去执行
executors.WithBulkTasks(16), // container最大task数。一般设为2的幂次
)
for i := 0; i < 20; i++ {
executor.Add(i)
}
executor.Wait()
// time.Sleep(10 * time.Second)
// executor.Flush()
如果把executor.Flush()注释掉,只会打印出0到15,进程就退出了,剩下的任务就丢失了。因为periodicalexecutor.go里的 pe.waitGroup.Add(1) 是在子协程里调用的,所以调用了executor.Wait()主协程也等不到子协程到ticker的时间自动去flush就退出了,所以16到20的任务就丢失了。在主协程调用executor.Flush(),其实是把剩下的任务取出到了主协程来执行了,如果忘了的话就会丢任务。或者主协程执行的时间足够长,能让子协程达到ticker的时间自动去flush。

改进'goctl api go'生成代码的模块化程度

image

背景

  • 罗马不是一天建成的,系统功能的实现也不会是一蹴而就的。今天开发A功能,明天开发B功能,后天开发C功能,而开发A功能时,大概率不会想清楚后续的Z功能的具体实现。
  • 假设我今天开发A功能,并且写了一个a.api文件。过几天,我开发B功能,写了一个b.api。我想通过goctl api go --api b.api生成B功能相关的代码,但是就会遇到类似截图中的困境:无法在当前项目目录下生成B功能相关的代码,如果选择生成,之前开发的功能的部分代码就会被覆盖掉(types.go、routes.go等)。

需求

  • goctl工具能够支持迭代式的代码生成且保证不会覆盖已有代码

方案(个人浅见)

  • 支持覆盖追加两种代码生成模式。比如在二次执行goctl api go --api xxx.api时,由操作人选择是覆盖或者追加到已有的代码文件中(types.go、routes.go等)。
  • 将代码进一步划分到不同的源代码文件中,做到更彻底点的模块化。举例来说,a.api生成的types相关的代码就放置在types/a.go中,b.api生成的types相关的代码就放置在types/b.go中。这样一来,b模块代码的生成不会直接覆盖掉以后的代码。其他模块也类似。如果能做到包级的相互隔离就更赞了。

项目名称带‘/’生成代码异常

  1. mkdir go-zero
  2. go mod init study/go-zero
  3. goctl api -o greet.api
  4. goctl api go -api greet.api -dir greet
imports (
    "go-zero/greet/internal/svc" // 生成的
    "study/go-zero/greet/internal/svc" // 期望的
)

zrpc redis鉴权不生效

调试发现auth.Authenticate 这个方法没有走,是哪里设置错了吗?

Auth: true
StrictControl: false
Redis:
  Key: appserver.auth
  Host: 127.0.0.1:6379
  Type: node

gctl new同样目录下两次执行不同结果,生成错误文件目录

执行命令cmd,及log
`
G:\go\src\kispr\greet 的目录

2020/10/25 16:13

.
2020/10/25 16:13 ..
0 个文件 0 字节
2 个目录 307,307,012,096 可用字节

G:\go\src\kispr\greet>dir
驱动器 G 中的卷是 新加卷
卷的序列号是 AA02-EC66

G:\go\src\kispr\greet 的目录

2020/10/25 16:13

.
2020/10/25 16:13 ..
2020/10/25 16:12 etc
2020/10/25 16:13 67 go.mod
2020/10/25 16:13 35,166 go.sum
2020/10/25 16:12 272 greet.api
2020/10/25 16:12 586 greet.go
2020/10/25 16:12 internal
4 个文件 36,091 字节
4 个目录 307,307,012,096 可用字节

G:\go\src\kispr\greet>G:\go\bin\goctl api new greet
�[32mDone.�[0m

G:\go\src\kispr\greet>G:\go\bin\goctl api go -api greet.api -dir .
error: near line: 15, wrong line "get /greet/from/:name(Request) returns ( );",
"route syntax: [get/post/delete] /path(request) returns[(response)]"

G:\go\src\kispr\greet>G:\go\bin\goctl -api greet.api -dir .
Incorrect Usage. flag provided but not defined: -api

NAME:
goctl - a cli tool to generate code

USAGE:
goctl [global options] command [command options] [arguments...]

VERSION:
20201021 windows/amd64

COMMANDS:
api generate api related files
docker generate Dockerfile and Makefile
rpc generate rpc code
model generate model code
config generate config json
template template operation
help, h Shows a list of commands or help for one command

GLOBAL OPTIONS:
--help, -h show help
--version, -v print the version
error: flag provided but not defined: -api

G:\go\src\kispr\greet>G:\go\bin\goctl api go -api greet.api
error: missing -dir

G:\go\src\kispr\greet>G:\go\bin\goctl api go -api greet.api -dir .
error: near line: 15, wrong line "get /greet/from/:name(Request) returns ( );",
"route syntax: [get/post/delete] /path(request) returns[(response)]"

G:\go\src\kispr\greet>G:\go\bin\goctl -api greet.api -dir .
Incorrect Usage. flag provided but not defined: -api

NAME:
goctl - a cli tool to generate code

USAGE:
goctl [global options] command [command options] [arguments...]

VERSION:
20201021 windows/amd64

COMMANDS:
api generate api related files
docker generate Dockerfile and Makefile
rpc generate rpc code
model generate model code
config generate config json
template template operation
help, h Shows a list of commands or help for one command

GLOBAL OPTIONS:
--help, -h show help
--version, -v print the version
error: flag provided but not defined: -api

G:\go\src\kispr\greet>G:\go\bin\goctl api go -dir .
error: missing -api

G:\go\src\kispr\greet>G:\go\bin\goctl api new greet
G:\go\src\kispr\greet\greet/etc/greet-api.yaml exists, ignored generation
G:\go\src\kispr\greet\greet/internal/config/config.go exists, ignored generation

G:\go\src\kispr\greet\greet/greet.go exists, ignored generation
G:\go\src\kispr\greet\greet/internal/svc/servicecontext.go exists, ignored gener
ation
G:\go\src\kispr\greet\greet/internal/types/types.go exists, ignored generation
G:\go\src\kispr\greet\greet/internal/handler/greethandler.go exists, ignored gen
eration
G:\go\src\kispr\greet\greet/internal/handler/routes.go exists, ignored generatio
n
G:\go\src\kispr\greet\greet/internal/logic/greetlogic.go exists, ignored generat
ion
�[32mDone.�[0m

G:\go\src\kispr\greet>`

quick start 例子启动时,注册 etcd 时发生错误

按照例子 ”快速构建高并发微服务“
https://github.com/tal-tech/go-zero/blob/master/doc/shorturl.md

运行

go run transform.go -f etc/transform.yaml

发生错误 (看起来是服务注册 etcd 时有点问题)

# go.etcd.io/etcd/clientv3/balancer/picker
../../../../go/pkg/mod/go.etcd.io/[email protected]/clientv3/balancer/picker/err.go:25:9: cannot use &errPicker literal (type *errPicker) as type Picker in return argument:
	*errPicker does not implement Picker (wrong type for Pick method)
		have Pick(context.Context, balancer.PickInfo) (balancer.SubConn, func(balancer.DoneInfo), error)
		want Pick(balancer.PickInfo) (balancer.PickResult, error)
../../../../go/pkg/mod/go.etcd.io/[email protected]/clientv3/balancer/picker/roundrobin_balanced.go:33:9: cannot use &rrBalanced literal (type *rrBalanced) as type Picker in return argument:
	*rrBalanced does not implement Picker (wrong type for Pick method)
		have Pick(context.Context, balancer.PickInfo) (balancer.SubConn, func(balancer.DoneInfo), error)
		want Pick(balancer.PickInfo) (balancer.PickResult, error)
# github.com/tal-tech/go-zero/zrpc/internal/balancer/p2c
../../../../go/pkg/mod/github.com/tal-tech/[email protected]/zrpc/internal/balancer/p2c/p2c.go:41:32: not enough arguments in call to base.NewBalancerBuilder
	have (string, *p2cPickerBuilder)
	want (string, base.PickerBuilder, base.Config)
../../../../go/pkg/mod/github.com/tal-tech/[email protected]/zrpc/internal/balancer/p2c/p2c.go:58:9: cannot use &p2cPicker literal (type *p2cPicker) as type balancer.Picker in return argument:
	*p2cPicker does not implement balancer.Picker (wrong type for Pick method)
		have Pick(context.Context, balancer.PickInfo) (balancer.SubConn, func(balancer.DoneInfo), error)
		want Pick(balancer.PickInfo) (balancer.PickResult, error)

etcd version

etcd Version: 3.4.13
Git SHA: Not provided (use ./build instead of go build)
Go Version: go1.15
Go OS/Arch: darwin/amd64

go 微服务的数据库选型,多数据库事务问题

我在参考贵司该项目时有几个问题困扰?
①:请问贵司出于什么考虑将之前的Mongo换成mysql了呢?(我司重度mongo用户😂)
②:Mongo不可以做微服务的存储仓吗?
③:该项目的分布式事务时啥方案啊?

劳烦解惑啊

goctl docker generate的用法

请教一下大佬,bookstore中,我需要生成哪些dockerfile,怎么生成?然后怎么用?
如果需要在k8s中部署,有没有可以参考的配置文件?
谢谢

goctl api new greet

2020/09/18 10:44:02 gen.go:57 'pwd' �����ڲ����ⲿ���Ҳ���ǿ����еij���
���������ļ���

protoc-gen-go 版本问题

goctl rpc proto -src transform.proto -dir .

当编译pb文件时候,会出现两个报错

  1. Missing 'go_package' option in "transform.proto"

  2. --go_out: protoc-gen-go: plugins are not supported; use 'protoc --go-grpc_out=...' to generate gRPC

panic: set concurent

Running tool: /usr/local/go/bin/go test -timeout 30s github.com/tal-tech/go-zero/core/collection -run ^TestConcurrent$

fatal error: concurrent map read and map write
fatal error: concurrent map write

func TestConcurrent(t *testing.T) {
	set := NewSet()

	go func() {
		i := 0
		for {
			set.AddInt(i)
			i++
			if i == 10000 {
				return
			}
		}
	}()

	go func() {
		i := 10000
		for {
			set.Remove(i)
			if i < 1 {
				return
			}
			i--
		}
	}()

	go func() {
		i := 10000
		for {
			set.Contains(i)
			i--
			if i < 1 {
				return
			}
		}
	}()
	time.Sleep(time.Second * 5)
}

go get github.com/tal-tech/go-zero/tools/goctl error

github.com/tal-tech/go-zero/tools/goctl

gopath/src/github.com/tal-tech/go-zero/tools/goctl/goctl.go:32:19: cannot use cli.StringFlag literal (type cli.StringFlag) as type cli.Flag in slice literal:
cli.StringFlag does not implement cli.Flag (Apply method has pointer receiver)
gopath/src/github.com/tal-tech/go-zero/tools/goctl/goctl.go:43:21: cannot use cli.StringFlag literal (type cli.StringFlag) as type cli.Flag in slice literal:
cli.StringFlag does not implement cli.Flag (Apply method has pointer receiver)
gopath/src/github.com/tal-tech/go-zero/tools/goctl/goctl.go:47:19: cannot use cli.BoolFlag literal (type cli.BoolFlag) as type cli.Flag in slice literal:
cli.BoolFlag does not implement cli.Flag (Apply method has pointer receiver)
gopath/src/github.com/tal-tech/go-zero/tools/goctl/goctl.go:51:19: cannot use cli.BoolFlag literal (type cli.BoolFlag) as type cli.Flag in slice literal:
cli.BoolFlag does not implement cli.Flag (Apply method has pointer receiver)
gopath/src/github.com/tal-tech/go-zero/tools/goctl/goctl.go:63:21: cannot use cli.StringFlag literal (type cli.StringFlag) as type cli.Flag in slice literal:
cli.StringFlag does not implement cli.Flag (Apply method has pointer receiver)
gopath/src/github.com/tal-tech/go-zero/tools/goctl/goctl.go:74:21: cannot use cli.StringFlag literal (type cli.StringFlag) as type cli.Flag in slice literal:
cli.StringFlag does not implement cli.Flag (Apply method has pointer receiver)
gopath/src/github.com/tal-tech/go-zero/tools/goctl/goctl.go:85:21: cannot use cli.StringFlag literal (type cli.StringFlag) as type cli.Flag in slice literal:
cli.StringFlag does not implement cli.Flag (Apply method has pointer receiver)
gopath/src/github.com/tal-tech/go-zero/tools/goctl/goctl.go:89:21: cannot use cli.StringFlag literal (type cli.StringFlag) as type cli.Flag in slice literal:
cli.StringFlag does not implement cli.Flag (Apply method has pointer receiver)
gopath/src/github.com/tal-tech/go-zero/tools/goctl/goctl.go:100:21: cannot use cli.StringFlag literal (type cli.StringFlag) as type cli.Flag in slice literal:
cli.StringFlag does not implement cli.Flag (Apply method has pointer receiver)
gopath/src/github.com/tal-tech/go-zero/tools/goctl/goctl.go:104:21: cannot use cli.StringFlag literal (type cli.StringFlag) as type cli.Flag in slice literal:
cli.StringFlag does not implement cli.Flag (Apply method has pointer receiver)
gopath/src/github.com/tal-tech/go-zero/tools/goctl/goctl.go:104:21: too many errors

win10下遇到的两个bug

1:goctl工具的bug,拉去的最新源码编译的

image
看不到版本号信息
2:在win10上运行goctl api go -api .\api\shorturl\shorturl.api -dir api
image

grpc新工程报错

goctl创建的项目 run的时候报如下错误,这个是依赖版本有问题嘛?

# go.etcd.io/etcd/clientv3/balancer/picker
F:\xxx\pkg\mod\go.etcd.io\e[email protected]\clientv3\balancer\picker\err.go:25:9: cannot use &errPicker literal (type *errPicker) as type Picker in return argument:
	*errPicker does not implement Picker (wrong type for Pick method)
		have Pick(context.Context, balancer.PickInfo) (balancer.SubConn, func(balancer.DoneInfo), error)
		want Pick(balancer.PickInfo) (balancer.PickResult, error)
F:\xxx\pkg\mod\go.etcd.io\e[email protected]\clientv3\balancer\picker\roundrobin_balanced.go:33:9: cannot use &rrBalanced literal (type *rrBalanced) as type Picker in return argument:
	*rrBalanced does not implement Picker (wrong type for Pick method)
		have Pick(context.Context, balancer.PickInfo) (balancer.SubConn, func(balancer.DoneInfo), error)
		want Pick(balancer.PickInfo) (balancer.PickResult, error)
# github.com/tal-tech/go-zero/zrpc/internal/balancer/p2c
F:\xxx\pkg\mod\github.com\tal-tech\g[email protected]\zrpc\internal\balancer\p2c\p2c.go:41:32: not enough arguments in call to base.NewBalancerBuilder
	have (string, *p2cPickerBuilder)
	want (string, base.PickerBuilder, base.Config)
F:\xxx\pkg\mod\github.com\tal-tech\g[email protected]\zrpc\internal\balancer\p2c\p2c.go:58:9: cannot use &p2cPicker literal (type *p2cPicker) as type balancer.Picker in return argument:
	*p2cPicker does not implement balancer.Picker (wrong type for Pick method)
		have Pick(context.Context, balancer.PickInfo) (balancer.SubConn, func(balancer.DoneInfo), error)
		want Pick(balancer.PickInfo) (balancer.PickResult, error)

wsl下cgroups环境问题导致goctl无法运行

➜ go-zero goctl api -o greet.api
2020/08/27 10:59:46 cpu_linux.go:27 open cpuacct.usage_percpu: no such file or directory
2020/08/27 10:59:46 {"@timestamp":"2020-08-27T10:59:46.111+08","level":"fatal","content":"cpu_linux.go:27 open cpuacct.usage_percpu: no such file or directory"}

这块cpu相关的stat是否可以通过配置屏蔽?

params of request not support multi tags

type Request struct {
	Name string `json:"name" form:"name" path:"name"`
}

when pass params on url

curl -i http://localhost:8888/users/id/1?name=1

and then get error: "field name is not set"

rpc 示例中 internal/svc/servicecontext.go 中 NewErr 返回 errPicker 参数与 Picker 不一致

错误版本

  • gozero v1.0.22
  • goctl version 20201021 darwin/amd64

描述

internal/svc/servicecontext.go 中 NewErr 返回 errPicker 参数与 Picker 不一致

errPicker 为3个, Picker 为 2个

func (ep *errPicker) Pick(context.Context, balancer.PickInfo) (balancer.SubConn, func(balancer.DoneInfo), error) {
	return nil, nil, ep.err
}


type Picker interface {
	Pick(info PickInfo) (PickResult, error)
}

具体代码

package picker

import (
	"context"

	"google.golang.org/grpc/balancer"
)

// NewErr returns a picker that always returns err on "Pick".
func NewErr(err error) Picker {
	return &errPicker{p: Error, err: err}
}

type errPicker struct {
	p   Policy
	err error
}

func (ep *errPicker) String() string {
	return ep.p.String()
}

func (ep *errPicker) Pick(context.Context, balancer.PickInfo) (balancer.SubConn, func(balancer.DoneInfo), error) {
	return nil, nil, ep.err
}

操作:

mkdir hello && cd hello
goctl rpc template -o=hello.proto
goctl rpc proto -src hello.proto
go run hello.go

具体错误如下:

# go.etcd.io/etcd/clientv3/balancer/picker
../../../../go/pkg/mod/go.etcd.io/[email protected]/clientv3/balancer/picker/err.go:25:9: cannot use &errPicker literal (type *errPicker) as type Picker in return argument:
        *errPicker does not implement Picker (wrong type for Pick method)
                have Pick(context.Context, balancer.PickInfo) (balancer.SubConn, func(balancer.DoneInfo), error)
                want Pick(balancer.PickInfo) (balancer.PickResult, error)
../../../../go/pkg/mod/go.etcd.io/[email protected]/clientv3/balancer/picker/roundrobin_balanced.go:33:9: cannot use &rrBalanced literal (type *rrBalanced) as type Picker in return argument:
        *rrBalanced does not implement Picker (wrong type for Pick method)
                have Pick(context.Context, balancer.PickInfo) (balancer.SubConn, func(balancer.DoneInfo), error)
                want Pick(balancer.PickInfo) (balancer.PickResult, error)
# github.com/tal-tech/go-zero/zrpc/internal/balancer/p2c
../../../../go/pkg/mod/github.com/tal-tech/[email protected]/zrpc/internal/balancer/p2c/p2c.go:41:32: not enough arguments in call to base.NewBalancerBuilder
        have (string, *p2cPickerBuilder)
        want (string, base.PickerBuilder, base.Config)
../../../../go/pkg/mod/github.com/tal-tech/[email protected]/zrpc/internal/balancer/p2c/p2c.go:58:9: cannot use &p2cPicker literal (type *p2cPicker) as type balancer.Picker in return argument:
        *p2cPicker does not implement balancer.Picker (wrong type for Pick method)
                have Pick(context.Context, balancer.PickInfo) (balancer.SubConn, func(balancer.DoneInfo), error)
                want Pick(balancer.PickInfo) (balancer.PickResult, error)

依赖问题:undefined: "github.com/golang/protobuf/proto".ProtoPackageIsVersion4

系统环境:

平台:MacOS Catalina 10.15.5
GO ENV:
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/xx/Library/Caches/go-build"
GOENV="/Users/xx/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/xx/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/xx/go"
GOPRIVATE=""
GOPROXY="https://goproxy.cn/,direct"
GOROOT="/usr/local/Cellar/go/1.15.1/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.15.1/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/xx/docker-files/dnmp/www/shorturl/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/q1/k1fjfgbs1ynbrzj33jqqg7680000gp/T/go-build909429352=/tmp/go-build -gno-record-gcc-switches -fno-common"

Go 依赖做了调整:

replace (
	github.com/golang/protobuf => github.com/golang/protobuf v1.3.2
	google.golang.org/grpc => google.golang.org/grpc v1.26.0
)

其他依赖保持默认的状态

运行教程 “快速构建高并发微服务”
执行 go run transform.go -f etc/transform.yaml
出现以下错误:

# google.golang.org/genproto/googleapis/rpc/status
/Users/xx/go/pkg/mod/google.golang.org/[email protected]/googleapis/rpc/status/status.pb.go:42:11: undefined: "github.com/golang/protobuf/proto".ProtoPackageIsVersion4
# google.golang.org/genproto/googleapis/api/annotations
/Users/xx/go/pkg/mod/google.golang.org/[email protected]/googleapis/api/annotations/annotations.pb.go:41:11: undefined: "github.com/golang/protobuf/proto".ProtoPackageIsVersion4
/Users/xx/go/pkg/mod/google.golang.org/[email protected]/googleapis/api/annotations/client.pb.go:41:11: undefined: "github.com/golang/protobuf/proto".ProtoPackageIsVersion4
/Users/xx/go/pkg/mod/google.golang.org/[email protected]/googleapis/api/annotations/field_behavior.pb.go:42:11: undefined: "github.com/golang/protobuf/proto".ProtoPackageIsVersion4
/Users/xx/go/pkg/mod/google.golang.org/[email protected]/googleapis/api/annotations/http.pb.go:41:11: undefined: "github.com/golang/protobuf/proto".ProtoPackageIsVersion4
/Users/xx/go/pkg/mod/google.golang.org/[email protected]/googleapis/api/annotations/resource.pb.go:42:11: undefined: "github.com/golang/protobuf/proto".ProtoPackageIsVersion4

疑问🤔️:网上查了以下,说是 github.com/golang/protobuf v1.3.2 依赖的版本太低了,但是就是因为之前是 1.4.1 导致有问题才做了降级,现在这个问题该怎么处理呢,不可能再做升级处理吧

建议:增加自动生成swagger文档功能

建议:利用反射获取结构体注解的文档注释,自动生成swagger文档
可参考:https://github.com/henrylee2cn/apiware 项目实现。
Apiware将Go语言net/http及fasthttp请求的指定参数绑定到结构体,并验证参数值的合法性。 建议您可以使用结构体作为web框架的Handler,并用该中间件快速绑定请求参数,节省了大量参数类型转换与有效性验证的工作。同时还可以通过该结构体标签,创建swagger的json配置文件,轻松创建api文档服务。

Windows下使用goctl会输出异常信息

系统信息:
image
go版本:
image
异常信息:
image

这种情况我在家里的macOS试了,执行goctl api new test是没有问题的,但公司配的是Windows,在公司电脑上会出现这种异常信息,所以我的想法就是系统平台的原因,另外Windows下执行这边命令后,生成的目录也是不全的,如图:
image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.