Skip to content

1562600869/test273

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

特性开关(Feature Flag)管理服务

一个轻量级的特性开关管理服务,使用 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"
  }
}

API 接口

1. 评估特性开关

POST /evaluate

请求体:

{
  "flagKey": "new_ui",
  "userId": "user123",
  "environment": "prod",
  "attributes": {
    "country": "CN",
    "version": "1.0"
  }
}

响应:

{
  "flagKey": "new_ui",
  "enabled": true
}

2. 批量评估所有特性开关

GET /flags/bulk?userId=user123&environment=prod&country=CN

响应:

{
  "new_ui": true,
  "dark_mode": false,
  "premium_feature": true
}

3. 获取所有特性开关

GET /flags

4. 创建特性开关

POST /flags

请求头:X-Operator: admin

请求体:

{
  "key": "new_feature",
  "name": "新功能",
  "description": "新功能描述",
  "enabled": true,
  "defaultValue": false,
  "rules": []
}

5. 获取单个特性开关

GET /flags/{key}

6. 更新特性开关

PUT /flags/{key}

请求头:X-Operator: admin

7. 删除特性开关

DELETE /flags/{key}

请求头:X-Operator: admin

8. 获取变更历史

GET /history

9. 获取统计数据

GET /stats

响应:

{
  "new_ui": {
    "totalCount": 100,
    "trueCount": 60,
    "falseCount": 40,
    "trueRatio": 0.6
  }
}

热重载

服务会每2秒检查一次配置文件,当文件发生变更时自动重新加载,无需重启服务。

变更历史

每次创建、更新、删除操作都会被记录,保留最近100条记录。

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages