从零构建微服务框架: 有哪些功能?

微服务相关概念 https://microservices.io 我们开发的仓库代码 https://github.com/bobacgo/kit 这篇文章先介绍: 单个web服务 常见的功能模块 微服务 常见的功能模块 框架主要基于热门的 go pkg 进行精简封装。不追求过度设计。后续的的文章会围绕这些功能模块展开,代码设计思路。 功能描述 单体服务常用的模块 Http 服务 (gin) 配置文件管理 (viper) 日志设计 (zap) 请求参数校验、响应结果封装 (参数校验 validator/v10) 异常恢复 安全模块 (jwt) 数据库初始 (mysql、go-redis) orm框架集成(gorm) 本地缓存组件 (go-cache) http请求工具 (go-retry) 性分析接口 Swagger文档接口 微服务framework 配置中心 (ectd、本地) 注册中心 (ectd) 通信方式(gRPC、http、MQ) 网关服务(go service) 监控中心(prometheus、grafana) 链路追踪 (jaeger) 限流、熔断(rate、 ratelimt,hystrix-go) 相同服务多节点任务分配 (基于redis) 框架使用剧透 1 2 # 下载依赖 go get github.com/bobacgo/kit 快速开始 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 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() { 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(), // app.WithMustDB(), // app.WithMustRedis(), app.WithGinServer(router.Register), ) if err := newApp.Run(); err != nil { log.Panic(err.Error()) } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 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 流行的微服务框架 go-zero go-micro kratos kitex goFrame

2025-03-05 · 3 min · 556 words · Lance

用Hugo构建我的Blog

1.环境准备 1 2 3 4 5 6 7 # 安装 hugo brew install hugo # 查看版本 hugo version # 创建仓库 cd Desktop hugo new site blog --format=yaml # 配置文件使用 yaml 格式 2.引用主题 1 2 cd blog git clone https://github.com/adityatelange/hugo-PaperMod.git themes/PaperMod --depth=1 3.启动服务 1 2 cd Desktop/blog hugo server 4.新建文章 1 hugo new posts/my-first-post.md 5.github 创建仓库 创建一个名为 weilanjin.github.io 的仓库 5.1 创建自动化脚本 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 name: Deploy Hugo site to GitHub Pages # 工作流名称 on: push: branches: - main # 监听 main 分支上的推送操作 jobs: deploy: runs-on: ubuntu-latest # 使用最新的 Ubuntu 作为运行环境 steps: - name: Checkout Repository uses: actions/checkout@v3 # 拉取代码,包括子模块 - name: Install Hugo uses: peaceiris/actions-hugo@v2 # 安装 Hugo with: hugo-version: 'latest' # 使用最新版本的 Hugo - name: Build Hugo Site run: hugo # 运行 hugo 生成静态站点 - name: Deploy to GitHub Pages uses: peaceiris/actions-gh-pages@v3 # 使用 gh-pages 部署 with: github_token: ${{ secrets.GITHUB_TOKEN }} # GitHub 提供的安全令牌 publish_dir: ./public # Hugo 生成的静态站点目录 publish_branch: gh-pages # 部署到 gh-pages 分支 5.2 启用 GitHub Pages 的 Actions 写入权限 进入你的 GitHub 仓库 打开 Settings → Actions → General 在 Workflow permissions 里选择: ✅ Read and write permissions(⚠ 默认是 Read-only) ...

2025-03-04 · 4 min · 837 words · Lance