这是我参与「第五届青训营 」笔记创作活动的第3天
录播内容
Gorm
Gorm是一个使用Go语言编写的ORM框架。它文档齐全,对开发者友好,支持主流数据库。在字节内部被广泛使用且拥有非常丰富的开源扩展。
- gorm目前支持MySQL、SQLserver、PostgreSQL、SQLite。通过驱动连接数据库,可自行开发/复用。
- 包含增删改查、事务操作(其中内置提供了transaction用于自动提交,避免漏写commit、rollback)、hook。
- 性能
- SkipDefaultTransaction
- PrepareStmt
- 生态
Kitex
Kitex 是字节跳动内部的Golang微服务RPC框架,具有高性能、强可扩展的特点,在字节内部已广泛使用。
Hertz
Hertz是一个Golang微服务HTTP框架,在设计之初参考了其他开源框架 fasthttp、gin、echo 的优势, 并结合字节跳动内部的需求,使其具有高易用性、高性能、高扩展性等特点,目前在字节跳动内部已广泛使用。
go-zero-looklook实战
注:本文思路从如何团队协作完成最终的项目展开。
前提:已有架构图与模块划分(这里没有展出)。
后面用go-zero-looklook作为参考
系统架构
文件结构
原文引用
app:All business codes include API, RPC and MQ (message queue, delay queue, scheduled task)
common:General components ( error、middleware、interceptor、tool、ctxdata ...)
data:The project contains the data generated by the directory relying on all middleware (mysql, ES, redis, grafana, ...). All contents in this directory should be in Git ignore files and do not need to be submitted.
deploy:
- filebeat: docker deployment filebeat config
- go-stash:go-stash config
- nginx: nginx-gateway config
- prometheus : prometheus config
- script:
- gencode:auto generate api、rpc,and use kafka script ,copy and paste use
- mysql:auto generate model code shell script
- goctl: The template and goctl of the project generate custom code templates. For the usage of template, refer to the go zero document and copy it to the home directory Goctl is enough
doc : This project series documents
modd.conf : Don't be afraid of Modd hot loading configuration file. It's very simple to use. You can learn more about Modd usage here: github.com/cortesi/mod… , the image of this project only uses golang-1.17.7-alpine as the basic image and installs Modd internally. If you want to add goctl, protoc, golint, etc., it's the same to directly create an image without my image
模块交互接口
先不考虑docker、Nginx、k8s等一系列部署问题,在确定了架构与大致技术栈之后,先考虑最基本的,模块如何交互,也即开发如何协作展开。
-
插件:goctl
-
goctl api是goctl中的核心模块之一,其可以通过.api文件一键快速生成一个api服务,如果仅仅是启动一个go-zero的api演示项目, 你甚至都不用编码,就可以完成一个api服务开发及正常运行。在传统的api项目中,我们要创建各级目录,编写结构体, 定义路由,添加logic文件,这一系列操作,如果按照一条协议的业务需求计算,整个编码下来大概需要5~6分钟才能真正进入业务逻辑的编写, 这还不考虑编写过程中可能产生的各种错误,而随着服务的增多,随着协议的增多,这部分准备工作的时间将成正比上升, 而goctl api则可以完全替代你去做这一部分工作,不管你的协议要定多少个,最终来说,只需要花费10秒不到即可完成。
-
todo:分析一下功能模块,拆分不同的类,先定义.api与.proto文件,与队友协商之后生成对应文件夹,合并仓库。