# go-example 一个基于 Go 语言的示例项目,展示了现代云原生(Cloud Native)应用的实践,包括静态代码规范、监控、配置管理、容器化以及基于 Flux 的 GitOps 持续部署。 ## 🚀 项目特性 - **高效微服务**: 使用 Go 1.25+ 构建,精简且高性能。 - **可观测性**: 集成 [Prometheus](https://prometheus.io/),提供 HTTP 请求计数和耗时采集。 - **灵活配置**: 使用 [Viper](https://github.com/spf13/viper) 支持多种配置格式及环境变量。 - **GitOps 实战**: 包含完备的 [Flux CD](https://fluxcd.io/) 配置,实现 Kubernetes 自动化部署。 - **轻量级容器**: 采用多阶段构建(Multi-stage Build),基于 `scratch` 镜像,体积极小。 - **安全加固**: 包含 [Cosign](https://github.com/sigstore/cosign) 公钥,用于镜像签名校验。 ## 🛠 技术栈 - **语言**: Go - **配置**: Viper - **监控**: Prometheus - **部署**: k3s/k8s, Flux CD - **容器**: Docker (Scratch base) ## 📁 目录结构 - `main.go`: 程序入口,负责解析参数与初始化服务。 - [hello/](hello/): 核心业务逻辑,包括接口处理与监控指标定义。 - [cmn/](cmn/): 通用组件,包含配置处理(`conf`)与辅助工具(`utils`)。 - [kustomize/](kustomize/): Kubernetes 资源清单与 Kustomize 配置。 - [clusters/](clusters/): Flux CD 系统组件及应用同步配置。 - [certs/](certs/): 安全证书与公钥。 ## 🏁 快速开始 ### 本地运行 1. 确保安装了 Go 环境。 2. 准备配置文件(如 `.linux-config.yaml`)。 3. 启动应用: ```bash go run main.go -f path/to/config.yaml ``` 4. 访问接口: - 业务接口: `http://localhost:8080/hello` - 指标接口: `http://localhost:8080/metrics` ### 在 Kubernetes 中部署 使用 Kustomize 手动部署: ```bash kubectl apply -k kustomize/ ``` ### GitOps 自动同步 通过 Flux CD 脚本进行初始化: ```bash ./clusters/app/flux-deploy.sh ``` ## 📊 监控指标 项目默认暴露 Prometheus 格式指标: - `http_requests_total`: HTTP 请求总计。 - `http_request_duration_seconds`: HTTP 请求耗时分布。