genesis

module
v1.0.0-rc.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 10, 2026 License: MIT

README

Genesis

一个面向 Go 微服务的轻量组件库,而不是框架。

当前已发布预发行基线为 v1.0.0-rc.1, tag 指向 ec5ad2c31fb4adce2bd42529e3d7fbfe92b23aa7。后续测试和文档提交不会 自动产生新版本;生产修复需要独立的 rc.2 决策与完整发布门禁。

Genesis 提供一组可以直接组合的基础设施与治理组件,目标不是接管应用,而是把日志、配置、连接管理、缓存、分布式锁、消息、认证、限流、熔断、注册发现等通用能力沉淀成统一积木。

最低支持 Go 1.26.0。CI 同时使用最低版本和当前固定补丁版本 1.26.5 验证编译、测试与发布证据;消费者不应依赖本机更高版本才能通过。

项目的核心约束只有三条:

  • 显式依赖注入,不使用运行时 DI 容器。
  • 组件边界清楚,能力按层组织但包结构保持扁平。
  • 谁创建,谁 Close();连接器拥有资源,业务组件只借用资源。

架构分层

层次 核心组件 职责
Level 3: Governance auth, ratelimit, breaker, registry 认证与流量治理
Level 2: Business cache, idgen, dlock, idem, mq 业务通用能力
Level 1: Infrastructure connector, db 连接管理与数据库访问
Level 0: Base clog, config, metrics, trace, xerrors 基础能力与统一约束

项目状态

当前分支已经系统收敛并重写了所有核心组件的实现边界与文档,重点包括:

  • auth 已切换为双 JWT 令牌模型。
  • idgen 已收紧 snowflake 位布局、allocator 所有权和 sequencer 语义。
  • ratelimit 已收紧分布式 key 设计、Redis 时间语义与错误策略。
  • dlock 已收紧锁生命周期、TTL 校验与 Close() 语义。
  • registry 已收紧 gRPC-only endpoint、resolver 空状态、watch 恢复语义与 Close() 返回值。
  • breaker 已补 gRPC 错误分类、统一拒绝错误模型与配置校验。
  • idem 已收紧返回值稳定性、缓存策略和锁续期语义。

快速开始

package main

import (
    "context"
    "os/signal"
    "syscall"

    "github.com/ceyewan/genesis/cache"
    "github.com/ceyewan/genesis/clog"
    "github.com/ceyewan/genesis/config"
    "github.com/ceyewan/genesis/connector"
)

func main() {
    ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
    defer cancel()

    loader, err := config.New(nil)
    if err != nil { panic(err) }
    defer loader.Close()
    if err := loader.Load(ctx); err != nil { panic(err) }

    var cfg AppConfig
    if err := loader.Unmarshal(&cfg); err != nil { panic(err) }
    logger, err := clog.New(&cfg.Log)
    if err != nil { panic(err) }
    defer logger.Close()

    redisConn, err := connector.NewRedis(&cfg.Redis, connector.WithLogger(logger))
    if err != nil { panic(err) }
    defer redisConn.Close()
    if err := redisConn.Connect(ctx); err != nil { panic(err) }

    cacheClient, err := cache.NewDistributed(&cfg.Cache, cache.WithRedisConnector(redisConn), cache.WithLogger(logger))
    if err != nil { panic(err) }
    defer cacheClient.Close()

    _, _ = cacheClient.Get(ctx, "demo:key")
}

更完整的总体设计见 docs/genesis-design.md。各组件的定位、边界、接入方式和设计取舍见 docs/README.md。

常用命令

# 代码质量
go test ./...
go test -race -count=1 ./...
make lint
make modernize
make modernize-check
make api-inventory-check
make api-compat-check

# 文档
go doc -all ./<component>

# 示例
make examples
make example-<component>

测试约束

  • 仓库内测试优先使用 internal/testkit 提供的容器化 helper,例如 testkit.NewRedisContainerClient(t)、testkit.NewMySQLDB(t)。外部项目维护自己的 fixture。
  • 集成测试通过 testcontainers 自动拉起依赖,不要在测试前手动执行 make up。
  • 测试断言使用 require,不要新增 assert。

文档入口

License

MIT

Directories

Path Synopsis
Package auth 提供基于 JWT 的双令牌认证能力。
Package auth 提供基于 JWT 的双令牌认证能力。
Package breaker 提供了面向 gRPC 客户端场景的轻量熔断组件。
Package breaker 提供了面向 gRPC 客户端场景的轻量熔断组件。
Package cache 提供 Genesis L2 业务层的缓存组件族,支持分布式缓存、本地缓存和多级缓存。
Package cache 提供 Genesis L2 业务层的缓存组件族,支持分布式缓存、本地缓存和多级缓存。
serializer
Package serializer defines the value encoding boundary used by cache.
Package serializer defines the value encoding boundary used by cache.
Package clog 为 Genesis 提供统一的结构化日志接口。
Package clog 为 Genesis 提供统一的结构化日志接口。
Package config 为 Genesis 提供统一的多源配置加载与文件驱动的变更通知能力。
Package config 为 Genesis 提供统一的多源配置加载与文件驱动的变更通知能力。
Package connector 为 Genesis 提供统一的连接管理能力。
Package connector 为 Genesis 提供统一的连接管理能力。
Package db 提供基于 GORM 的数据库组件,是 Genesis L1 基础设施层的一部分。
Package db 提供基于 GORM 的数据库组件,是 Genesis L1 基础设施层的一部分。
Package dlock 提供 Genesis 的分布式锁组件。
Package dlock 提供 Genesis 的分布式锁组件。
examples
auth command
breaker command
breaker/internal/proto/demo/v1
Package pb contains generated code used only by the breaker example.
Package pb contains generated code used only by the breaker example.
cache command
clog command
config command
connector command
examples/connector/main.go
examples/connector/main.go
db command
dlock command
grpc-registry command
grpc-registry/internal/proto
Package proto contains generated code used only by the grpc-registry example.
Package proto contains generated code used only by the grpc-registry example.
idem command
idem/internal/proto
Package pb contains generated code used only by the idempotency example.
Package pb contains generated code used only by the idempotency example.
idgen command
mq command
examples/mq/main.go
examples/mq/main.go
observability/internal/proto
Package proto contains generated code used only by the observability example.
Package proto contains generated code used only by the observability example.
ratelimit command
registry command
xerrors command
Package idem 提供结果复用型幂等组件,用于抑制同一请求或消息的重复成功提交。
Package idem 提供结果复用型幂等组件,用于抑制同一请求或消息的重复成功提交。
Package idgen 提供 Genesis L2 业务层的 ID 生成能力。
Package idgen 提供 Genesis L2 业务层的 ID 生成能力。
internal
cmd/apiinventory command
Command apiinventory generates the reviewed Genesis v1 exported API surface.
Command apiinventory generates the reviewed Genesis v1 exported API surface.
cmd/mdlinkcheck command
Command mdlinkcheck validates repository-local links in Markdown files.
Command mdlinkcheck validates repository-local links in Markdown files.
cmd/publishprerelease command
Command publishprerelease stages and publishes the immutable RC2 GitHub prerelease after the exact release evidence has passed its protected gates.
Command publishprerelease stages and publishes the immutable RC2 GitHub prerelease after the exact release evidence has passed its protected gates.
cmd/stage3evidence command
Command stage3evidence validates the immutable Resonance Stage 3 handoff that gates Genesis v1.0.0-rc.2 publication.
Command stage3evidence validates the immutable Resonance Stage 3 handoff that gates Genesis v1.0.0-rc.2 publication.
testkit
Package testkit 提供 Genesis 仓库内部组件测试所需的通用 helper。
Package testkit 提供 Genesis 仓库内部组件测试所需的通用 helper。
Package metrics 提供 Genesis 的 OpenTelemetry 指标封装与 Prometheus 暴露能力。
Package metrics 提供 Genesis 的 OpenTelemetry 指标封装与 Prometheus 暴露能力。
Package mq 提供消息队列组件,支持 NATS JetStream 和 Redis Stream 两种后端。
Package mq 提供消息队列组件,支持 NATS JetStream 和 Redis Stream 两种后端。
Package ratelimit 提供 Genesis 的限流组件。
Package ratelimit 提供 Genesis 的限流组件。
Package registry 提供了基于 Etcd 的服务注册发现组件,以及面向 gRPC 的 resolver 集成。
Package registry 提供了基于 Etcd 的服务注册发现组件,以及面向 gRPC 的 resolver 集成。
Package trace 提供 Genesis 的 OpenTelemetry 链路追踪初始化与传播辅助能力。
Package trace 提供 Genesis 的 OpenTelemetry 链路追踪初始化与传播辅助能力。
Package xerrors 提供 Genesis 的轻量错误封装工具。
Package xerrors 提供 Genesis 的轻量错误封装工具。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL