这是一个基于golang + gin的基础Web框架。项目是完全按照Gopher 公认的项目标准结构定制,很多组件的实现大量参考B站 开源的Kratos设计理念。为加深自己对go的理解,写了一些Web常用的Examples。
包含如下特性:
- 
基于gin的轻量级web框架,拥有更加出色的性能。
- gin-validator表单验证。底层实现:https://gopkg.in/go-playground/validator.v8
 - middleware拦截功能:可以自由的实现一些拦截handler
 - swagger API文档
 - pprof:性能分析工具
 - 优雅退出机制
 
 - 
基于toml的配置文件,真的是谁用谁知道。
 - 
参考Kratos实现的log组件。支持配置日志位置、按文件大小滚动日志、缓冲大小等。
 - 
参考Kratos实现的mysql组件。读写分离、支持事务、慢查询日志记录等。
 - 
基于redigo封装的redis组件。这也是golang官方推荐的redis包。
 - 
基于net/http封装的http连接池组件。
 - 
经典的错误码设计理念,与golang的error处理机制完美结合。
 - 
and so on...
 
- 新加基于 hystrix-go 的熔断器。
 - 优化 http Client 组件,并集成 hystrix,配置 demo 如下
 
[httpClient]
    [httpClient.abc]
        addr = "http://api.abc.com"
        [httpClient.abc.clientConf]
            maxTotal = 10
            maxPerHost  = 10
            keepAlive = "5s"
            dialTimeout = "1s"
            timeout = "1s"
            [httpClient.abc.clientConf.breaker]
                namespace = "abc"
                timeout = "3s"
                maxConcurrentRequests = 5
                requestVolumeThreshold= 1
                sleepWindow = "5s"
                errorPercentThreshold = 50- 新加 gRPC Client 组件,并集成 hystrix,配置 demo 如下
 
[grpcClient]
    [grpcClient.sayHello]
        addr = "10.1.172.180:9101"
        [grpcClient.sayHello.clientConf]
            dialTimeout = "1s"
            timeout = "1s"
            poolSize = 4
            [grpcClient.sayHello.clientConf.breaker]
                namespace = "sayHello"
                timeout = "1s"
                maxConcurrentRequests = 1000
                requestVolumeThreshold= 10
                sleepWindow = "5s"
                errorPercentThreshold = 60- 完善相关组件单元测试
 
$ go get github.com/daoshenzzg/go-web-demo
# go get代理,org包要科学上网
$ export GOPROXY=https://goproxy.io
# 开启GO111MODULE模式
$ export GO111MODULE=on
# 使用GO MODULE模式管理包
$ go mod init
# 编译
$ cd ../app/demo-api/cmd; go build
# 运行
$ ./cmd -conf ../configs/application.toml 
或者指定端口
$ ./cmd -conf ../configs/application.toml --http.port=8080
1、go get
$ go get -u github.com/swaggo/swag/cmd/swag
若 $GOPATH/bin 没有加入$PATH中,你需要执行将其可执行文件移动到$GOBIN下
$ mv $GOPATH/bin/swag /usr/local/go/bin
2、gopm get
gopm get -g -v github.com/swaggo/swag/cmd/swag
cd $GOPATH/src/github.com/swaggo/swag/cmd/swag
go install
同理将其可执行文件移动到$GOBIN下
$ swag -v
swag version v1.5.0
// @Summary 添加学生
// @Produce json
// @Param studName query string true "学生姓名"
// @Param studAge query int true "年龄"
// @Param studSex query string true "性别"
// @Success 200 {object} render.JSON
// @Router /api/v1/student/add [post]
我们进入到demo-api的根目录,执行初始化命令
$ cd ../app/demo-api
$ swag init
  2019/07/12 18:33:53 Generate swagger docs....
  2019/07/12 18:33:53 Generate general API Info
  2019/07/12 18:33:59 Generating render.JSON
  2019/07/12 18:33:59 create docs.go at  docs/docs.go
  2019/07/12 18:33:59 create swagger.json at  docs/swagger.json
  2019/07/12 18:33:59 create swagger.yaml at  docs/swagger.yaml
大功告成,访问:http://localhost:8080/swagger/index.html
请参考:https://github.com/DeanThompson/ginpprof
大功告成,访问:http://localhost:8080/debug/pprof/

请参考:https://blog.wolfogre.com/posts/go-ppof-practice/
用起来不要太爽,谁用谁知道...
基于gin的Router高效、强大、简单
基于gin的middleware,可以实现日志打印、权限验证等等..
很实用的表单validation功能,文档:https://gopkg.in/go-playground/validator.v8
定义Codes,并直接errors.Cause(e).(Codes)进行强转判断,完美兼容golang的error显性处理机制
- DB、Redis、Http、gRPC组件
 - 熔断器
 - Router限速器功能
 










