|
|
3 minggu lalu | |
|---|---|---|
| cmd | 3 minggu lalu | |
| example | 3 minggu lalu | |
| .gitignore | 3 minggu lalu | |
| LICENSE | 3 minggu lalu | |
| README.md | 3 minggu lalu | |
| go.mod | 3 minggu lalu | |
| registry.go | 3 minggu lalu | |
| runtime.go | 3 minggu lalu | |
| xpc.go | 3 minggu lalu |
XPC 是一个受 Google Service Weaver 启发,专为 Go 语言打造的现代、高性能、零依赖的微服务开发框架。
它的核心理念是:"像开发单体应用一样开发微服务,运行时既可以单体发布运行,也可以像管理集群一样管理运行时部分或者全部。"
我们推崇 约束大于配置 (Convention over Configuration),通过代码层面的最佳实践约束,消除繁琐的配置文件,让开发者专注于业务逻辑。
逻辑单体,物理分布 (Logical Monolith, Physical Distribution)
Init(ctx) 和 Shutdown(ctx) 生命周期钩子,确保资源正确初始化和释放。零外部依赖 (Zero Dependencies)
Raft 驱动的智能运行时 (Raft-Powered Runtime)
极简代码风格 (Minimalist API)
xpc.Run 自动处理依赖注入和优雅退出 (Graceful Shutdown)。xpc gen 自动生成注册代码,减少样板。XPC 框架经过深度优化,分为三层:
提供极其精简的接口:
xpc.Implements[T]: 标记组件实现。xpc.Run(ctx, app, opts...): 启动应用,自动处理信号、生命周期和依赖注入。
type Calculator interface {
Add(ctx context.Context, a, b int) (int, error)
}
位于根目录的 runtime.go 和 registry.go,高度模块化但结构扁平:
cmd/xpc 提供代码生成工具,扫描源码并自动注册组件。
我们遵循极简的扁平化目录结构:
.go 文件平铺在项目根目录(如 xpc.go, runtime.go, registry.go)。cmd/: 存放 CLI 工具入口。example/: 存放示例代码。internal/: 除非绝对必要,否则不使用深层嵌套的 internal 目录,通过大小写控制可见性。package main
import (
"context"
"fmt"
"igit.com/robert/xpc"
)
// 定义接口
type HelloService interface {
SayHello(ctx context.Context, name string) (string, error)
}
// 实现接口
type helloServiceImpl struct {
xpc.Implements[HelloService]
}
// 可选:初始化钩子
func (h *helloServiceImpl) Init(ctx context.Context) error {
fmt.Println("Service Initialized")
return nil
}
func (h *helloServiceImpl) SayHello(ctx context.Context, name string) (string, error) {
return "Hello, " + name, nil
}
func main() {
// 启动 XPC,自动处理依赖注入和优雅退出
err := xpc.Run(context.Background(), func(ctx context.Context, hello HelloService) error {
res, _ := hello.SayHello(ctx, "World")
println(res)
return nil // 返回后 XPC 会自动执行 Shutdown
})
if err != nil {
panic(err)
}
}
# 生成注册代码
go run cmd/xpc/main.go gen
# 运行
go run .
XPC 优先考虑安全性。在启动时可以配置 TLS 证书,供集群模式使用:
xpc.Run(ctx, app, xpc.Options{
TLSCertFile: "/path/to/cert.pem",
TLSKeyFile: "/path/to/key.pem",
})
[x] Phase 1: Core Framework Refactoring
Init/Shutdown)。[ ] Phase 2: Distribution & Security
xpc gen 支持跨包接口。本项目遵循严格的代码规范:简洁、原生、高性能。