各级目录、包功能介绍,有些点需要注意一下
➜ go-template git:(master) ✗ tree -L 3
.
├── README.md // 项目README.md
├── bin
│ └── app // 编译后的二进制文件
├── cicd // cicd脚本
│ ├── build.sh // 构建脚本(目前发不到容器内的不需要),需要构建机器上执行,目前是root用户 git config --global url."git@gitea.ckfah.com:".insteadOf "https://gitea.ckfah.com/"
│ └── deploy.sh // 发布脚本(目前发不到容器内的不需要),需要手动修改一下 CURRPORT 变量记得修改
├── cmd
│ └── main.go // 主函数
├── business
│ ├── controller // controller
│ │ ├── base_controller.go // 这个文件放置全局变量,这个项目不推荐以包隔离
│ ├── dao // dao
│ ├── dto // 数据传输对象,包含返回给前段的数据,也包含有调用远程的数据
│ ├── exception // 全局异常变量
│ ├── job // job,目前不推荐go项目写定时任务,里面放有 kafka的消费
│ ├── model // 数据库模型对象
│ ├── service // service
│ │ ├── base_service.go // 这个文件放置全局变量,这个项目不推荐以包隔离
│ └── third // 调用三方接口
│ ├── middleware // 自定义中间件
│ └── util // 自定义工具包,优先使用框架自带工具包:import "gitea.ckfah.com/cjjy/gocommon/pkg/common"
├── config // 配置文件目录
│ ├── apollo // 使用apollo时需要配置,注意 apollo和nacos不可以一起使用
│ ├── env.ini // 当前运行的配置文件
│ ├── env.dev.ini // 各环境配置
│ ├── env.release.ini
│ ├── env.slave.ini
│ ├── env.test.ini
│ ├── env.test2.ini
│ └── env.test3.ini
├── router // http 路由
│ ├── op_router.go // 自定义路由
│ ├── router.go // 所有路由放到这里配置
│ └── sys_router.go // 项目路由,包含web_status
├── go.mod // go mod 配置
├── go.sum // go sum 文件
├── Makefile //make 脚本,需要手动修改一下 PORT 变量
├── Dockerfile //Dokcerfile文件,是的可以依赖容器去启动
├── coverage.txt //go test 测试完后展示的覆盖测试文件
└── app.pid // 当前运行中程序的pid
Go Version >=1.11 (最好 1.13), 安装Git 、Go 即可
➜ ~ go version
go version go1.13.5 darwin/amd64
➜ ~ git version
git version 2.24.3 (Apple Git-128)
gocommon项目权限,找组内人申请权限开通!
libqconf.dylib 包 ,也可以使用容器去启动项目
git clone git@gitea.ckfah.com/cjjy/go-template.git 到本地mkdir /data/log/go-templatemake 命令即可运行➜ go-template git:(master) ✗ make help
all 启动项目程序
init 初始化项目环境依赖
build 构建go程序
run 启动Go程序
fmt 格式化Go项目代码
govet 静态代码检测工具
golint 检测代码规范工具
clean 清理无用文件
get go get 包, eg: make get import=github.com/apache/rocketmq-client-go/v2@v2.0.0
get-gocommon-last-tag 获取gocommon框架最新的依赖tag
get-gocommon-master 获取gocommon框架的master分支
test 单元测试, eg: make test test_func=Test_cityConfigThird_CityInfo test_pkg=./business/third
benchmark 性能测试, eg: make benchmark test_func=Test_cityConfigThird_CityInfo test_pkg=./business/third
testall 测试整个项目
docker 使用docker容器进行启动, 依靠绑定目录实现快速构建
prometheus 本地的prometheus监控,由于mac os 操作系统本身的原因导致无法看到进程信息,参考文档:https://prometheus.io/docs/prometheus/latest/getting_started/
1、在项目根目录执行 make 即可
➜ go-template git:(master) ✗ make
go build -v -ldflags "-s -w" -race -o bin/app cmd/main.go
GODEBUG=gctrace=1 bin/app
[GO-COMMON] 2021/04/09 17:31:32 boot.go:113: [INFO] [Config] start init...
2、检测项目是否启动成功,执行 curl http://localhost:8080/go-template/web_status -v
➜ ~ curl http://localhost:8080/go-template/web_status -v
* Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> GET /go-template/web_status HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.64.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: application/json; charset=utf-8
< Date: Fri, 09 Apr 2021 09:24:34 GMT
< Content-Length: 12
<
"status_ok"
* Connection #0 to host localhost left intact
* Closing connection 0
测试 go-template/business/util 包下的 TestDemo 方法,执行 make test test_func=TestDemo test_pkg=./business/util 即可!
➜ go-template git:(master) ✗ make test test_func=TestDemo test_pkg=./business/util
rm -f -r bin coverage.txt app.pid
go test -v -race -short -cover -coverprofile=coverage.txt -covermode=atomic -run=TestDemo ./business/util
=== RUN TestDemo
--- PASS: TestDemo (0.00s)
util_test.go:17: arr: []int{9, 8, 7, 6, 5, 4, 3, 2, 1}
util_test.go:19: sorted arr: []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
PASS
coverage: 0.0% of statements
ok go-template/business/util 1.037s coverage: 0.0% of statements
benchmark go-template/business/util 包下的 BenchmarkDemo 方法,执行 make benchmark test_func=BenchmarkDemo test_pkg=./business/util 即可!
➜ go-template git:(master) ✗ make benchmark test_func=BenchmarkDemo test_pkg=./business/util
rm -f -r bin coverage.txt app.pid
go test -v -run=none -bench=BenchmarkDemo -benchmem ./business/util
goos: darwin
goarch: amd64
pkg: go-template/business/util
BenchmarkDemo-12 14818328 78.5 ns/op 32 B/op 1 allocs/op
PASS
ok go-template/business/util 1.261s
使用Goland工具进行调试项目的时候切记要设置一下输出目录 !
首先你本地需要装docker,容器启动后,绑定的是你本机的 8080端口,和你本地启动基本是一样的,你本机可以访问服务路由
➜ go-template git:(master) ✗ make docker
cp -r /Users/fanhaodong/.ssh ./
docker build -t go-template:latest .
Sending build context to Docker daemon 201.2kB
Step 1/19 : FROM centos:7
## ........
Step 19/19 : CMD [ "/bin/bash"]
---> Running in 95c8cac0cc45
Removing intermediate container 95c8cac0cc45
---> 698785dd6e95
Successfully built 698785dd6e95
Successfully tagged go-template:latest
e3a231c7c0f8 go-project bridge local
docker run -it --rm \
--name go-template \
-p 8080:8080 \
--network go-project \
-v /Users/fanhaodong/go:/root/go \
-v /Users/fanhaodong/project/go-template:/data/go-template \
-v /data/log/go-template:/data/log/go-template \
-v /tmp/go-build:/root/.cache/go-build \
go-template:latest make run
go build -v -ldflags "-s -w" -race -o bin/app cmd/main.go
GODEBUG=gctrace=1 bin/app
[GO-COMMON] 2021/04/09 17:51:12 boot.go:113: [INFO] [Config] start init...
## .........
1、访问 curl http://localhost:8080/metrics
2、使用容器启动server端进行监控,记得修改项目路径下 bin/prometheus/prometheus.yml 配置文件,为你本机的IP,服务名称写你的服务名称,比如我本地网卡是 en0,ip是 172.15.64.10,服务名是 go-template !
targets: ['172.15.64.10:8080','go-template:8080']
3、启动的时候会帮助把配置文件下载下来
➜ go-template git:(master) ✗ make prometheus
e3a231c7c0f8 go-project bridge local
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1004 100 1004 0 0 9471 0 --:--:-- --:--:-- --:--:-- 9471
docker run --rm \
-p 9090:9090 \
-v /Users/fanhaodong/project/go-template/bin/prometheus/prometheus.yml:/opt/bitnami/prometheus/conf/prometheus.yml \
-v /Users/fanhaodong/project/go-template/bin/prometheus/data:/opt/bitnami/prometheus/data \
--network go-project \
bitnami/prometheus:latest
level=info ts=2021-04-09T09:53:59.316Z caller=main.go:322 msg="No time or size retention was set so using the default time retention" duration=15d
level=info ts=2021-04-09T09:53:59.317Z caller=main.go:360 msg="Starting Prometheus" version="(version=2.23.0, branch=HEAD, revision=26d89b4b0776fe4cd5a3656dfa520f119a375273)"
level=info ts=2021-04-09T09:53:59.317Z caller=main.go:365 build_context="(go=go1.15.5, user=root@37609b3a0a21, date=20201126-10:56:17)"
4、可以访问本机的 http://localhost:9090/graph 查看监控信息!