一个轻量级的特性开关管理服务,使用 Go 标准库开发,无需任何外部依赖。
- ✅ 特性开关管理:创建、修改、删除特性开关
- ✅ 多维度规则:支持基于用户ID、灰度百分比、环境、自定义属性的规则
- ✅ 评估接口:POST /evaluate 接口评估单个特性开关
- ✅ 批量评估:GET /flags/bulk 一次返回用户所有特性开关结果
- ✅ 变更历史:记录每次配置变更(谁改的、改了什么、什么时候)
- ✅ 热重载:配置文件变更时自动重载,无需重启服务
- ✅ 统计功能:每个特性开关的评估次数和 true/false 比例
go run main.go --config flags.json或指定端口:
go run main.go --config flags.json --port 8080配置文件为 JSON 格式,包含特性开关列表和变更历史。
{
"key": "flag_key",
"name": "开关名称",
"description": "开关描述",
"enabled": true,
"defaultValue": false,
"rules": [...]
}1. 基于用户ID (userId)
指定特定用户可以访问:
{
"type": "userId",
"userIds": ["user1", "user2", "admin"]
}2. 基于灰度百分比 (percentage)
使用FNV-1a哈希算法对userId + flagKey计算哈希值,按哈希结果取模进行灰度发布:
{
"type": "percentage",
"percentage": 30
}3. 基于环境 (environment)
{
"type": "environment",
"environments": ["dev", "staging"]
}4. 基于自定义属性 (attribute)
{
"type": "attribute",
"attributes": {
"country": "CN",
"version": "2.0"
}
}5. 组合规则 (combined)
组合多个条件,通过operator字段指定匹配逻辑:
AND:所有条件都满足才匹配OR(默认):任一条件满足即匹配
{
"type": "combined",
"operator": "AND",
"userIds": ["user1", "user2"],
"environments": ["prod"],
"attributes": {
"plan": "premium"
}
}POST /evaluate
请求体:
{
"flagKey": "new_ui",
"userId": "user123",
"environment": "prod",
"attributes": {
"country": "CN",
"version": "1.0"
}
}响应:
{
"flagKey": "new_ui",
"enabled": true
}GET /flags/bulk?userId=user123&environment=prod&country=CN
响应:
{
"new_ui": true,
"dark_mode": false,
"premium_feature": true
}GET /flags
POST /flags
请求头:X-Operator: admin
请求体:
{
"key": "new_feature",
"name": "新功能",
"description": "新功能描述",
"enabled": true,
"defaultValue": false,
"rules": []
}GET /flags/{key}
PUT /flags/{key}
请求头:X-Operator: admin
DELETE /flags/{key}
请求头:X-Operator: admin
GET /history
GET /stats
响应:
{
"new_ui": {
"totalCount": 100,
"trueCount": 60,
"falseCount": 40,
"trueRatio": 0.6
}
}服务会每2秒检查一次配置文件,当文件发生变更时自动重新加载,无需重启服务。
每次创建、更新、删除操作都会被记录,保留最近100条记录。