微服务相关概念 https://microservices.io 我们开发的仓库代码 https://github.com/bobacgo/kit
这篇文章先介绍: 单个web服务 常见的功能模块 微服务 常见的功能模块
框架主要基于热门的 go pkg 进行精简封装。不追求过度设计。后续的的文章会围绕这些功能模块展开,代码设计思路.
功能描述
单体服务常用的模块
1. Http 服务 (gin) 2. 配置文件管理 (viper) 3. 日志设计 (zap) 4. 请求参数校验、响应结果封装 (参数校验 validator/v10) 5. 异常恢复 6. 安全模块 (jwt) 7. 数据库初始 (mysql、go-redis) 8. orm框架集成(gorm) 9. 本地缓存组件 (go-cache) 10. http请求工具 (go-retry) 11. 性分析接口 12. Swagger文档接口
微服务framework
1. 配置中心 (ectd、本地) 2. 注册中心 (ectd) 3. 通信方式(gRPC、http、MQ) 4. 网关服务(go service) 4. 监控中心(prometheus、grafana) 4. 链路追踪 (jaeger) 5. 限流、熔断(rate、 ratelimt,hystrix-go) 6. 相同服务多节点任务分配 (基于redis)
框架使用剧透
# 下载依赖
go get github.com/bobacgo/kit
快速开始(创建一个http服务)
package main
import (
"flag"
"log"
"github.com/bobacgo/kit/app"
"github.com/bobacgo/kit/app/conf"
"github.com/bobacgo/kit/examples/config"
"github.com/bobacgo/kit/examples/internal/app/router"
)
var filepath = flag.String("config", "./config.yaml", "config file path")
func init() {
// 启动参数 name、env、logger.level、port
flag.String("name", "admin-service", "service name")
flag.String("env", "dev", "run config context")
flag.String("logger.level", "info", "logger level")
flag.Int("port", 8080, "http port 8080, rpc port 9080")
conf.BindPFlags()
}
func main() {
newApp := app.New(*filepath,
app.WithScanConfig(config.Cfg), // 服务业务配置文件
app.WithLogger(), // 设置日志
app.WithMustLocalCache(), // 加载相关的组件 缓存、数据库、redis
// app.WithMustDB(),
// app.WithMustRedis(),
app.WithGinServer(router.Register), // 启动http服务
)
if err := newApp.Run(); err != nil {
log.Panic(err.Error())
}
}
运行服务
go run main.go --config ./config.yaml
2025-03-06 14:00:22.870 INFO app/option.go:116 local config info
basic:
name: examples-service
version: 1.0.0
env: dev
configs:
- ./deploy/v1.0.0/db.yaml
- ./deploy/v1.0.0/logger.yaml
- ./deploy/v1.0.0/redis.yaml
registry:
addr: 127.0.0.1:2379
timeout: ""
server:
http:
addr: 0.0.0.0:8080
timeout: 1s
rpc:
addr: 0.0.0.0:9080
timeout: 1s
security:
ciphertext:
isCiphertext: false
cipherKey: YpC5w*****uMvd4f
jwt:
secret: YpC5w*****uMvd4f
issuer: gogo-admin
accessTokenExpired: ""
refreshTokenExpired: ""
cacheKeyPrefix: admin:login_token
logger:
level: ""
timeFormat: "2006-01-02 15:04:05.000"
filepath: ./logs
filename: examples-service
filenameSuffix: 2006-01-02-150405
fileExtension: log
fileJsonEncoder: false
fileSizeMax: 10
fileAgeMax: 180
fileCompress: true
db:
default:
source: admin******tcp(127.0.0.1:3306)/mall-ums?charset=utf8mb4&parseTime=True&loc=Local
dryRun: false
slowThreshold: 1
maxLifeTime: 1
maxOpenConn: 100
maxIdleConn: 30
localCache:
maxSize: 500MB
redis:
addrs:
- 127.0.0.1:6379
username: ""
password: ""
db: 0
poolSize: 50
readTimeout: 1ms
writeTimeout: 1ms
service:
errattemptlimit: 5
2025-03-06 14:00:22.870 INFO app/option.go:118 [config] init done.
2025-03-06 14:00:22.870 INFO app/option.go:119 [logger] init done.
2025-03-06 14:00:22.877 INFO app/option.go:132 [local_cache] init done.
2025-03-06 14:00:22.877 INFO app/service.go:95 server started
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env: export GIN_MODE=release
- using code: gin.SetMode(gin.ReleaseMode)
2025-03-06 14:00:22.878 WARN app/http.go:45 [gin] Running in "debug" mode
[GIN-debug] GET /health --> github.com/bobacgo/kit/app.healthApi.func1 (4 handlers)
[GIN-debug] GET /debug/pprof/ --> github.com/bobacgo/kit/app.pprofApi.WrapF.func1 (4 handlers)
[GIN-debug] GET /debug/pprof/cmdline --> github.com/bobacgo/kit/app.pprofApi.WrapF.func2 (4 handlers)
[GIN-debug] GET /debug/pprof/profile --> github.com/bobacgo/kit/app.pprofApi.WrapF.func3 (4 handlers)
[GIN-debug] GET /debug/pprof/symbol --> github.com/bobacgo/kit/app.pprofApi.WrapF.func4 (4 handlers)
[GIN-debug] GET /debug/pprof/trace --> github.com/bobacgo/kit/app.pprofApi.WrapF.func5 (4 handlers)
[GIN-debug] GET /debug/pprof/allocs --> github.com/bobacgo/kit/app.pprofApi.WrapF.func6 (4 handlers)
[GIN-debug] GET /debug/pprof/block --> github.com/bobacgo/kit/app.pprofApi.WrapF.func7 (4 handlers)
[GIN-debug] GET /debug/pprof/goroutine --> github.com/bobacgo/kit/app.pprofApi.WrapF.func8 (4 handlers)
[GIN-debug] GET /debug/pprof/heap --> github.com/bobacgo/kit/app.pprofApi.WrapF.func9 (4 handlers)
[GIN-debug] GET /debug/pprof/mutex --> github.com/bobacgo/kit/app.pprofApi.WrapF.func10 (4 handlers)
[GIN-debug] GET /debug/pprof/threadcreate --> github.com/bobacgo/kit/app.pprofApi.WrapF.func11 (4 handlers)
[GIN-debug] POST /v1/user/create --> github.com/bobacgo/kit/examples/internal/app/admin.Register.func1 (4 handlers)
[GIN-debug] PUT /v1/user/update --> github.com/bobacgo/kit/examples/internal/app/admin.Register.func2 (4 handlers)
[GIN-debug] DELETE /v1/user/delete --> github.com/bobacgo/kit/examples/internal/app/admin.Register.func3 (4 handlers)
[GIN-debug] POST /v1/user/pageList --> github.com/bobacgo/kit/examples/internal/app/admin.Register.func4 (4 handlers)
2025-03-06 14:00:22.878 INFO app/http.go:61 http server running http://192.168.1.4:8080
欢迎留言讨论,点赞👍分享🌹 流行的微服务框架
1. [go-zero](https://go-zero.dev) 2. [go-micro](https://github.com/micro/go-micro) 3. [kratos](https://go-kratos.dev/docs/) 4. [kitex](https://www.cloudwego.io/zh/) 5. [goFrame](https://goframe.org.cn)