一个功能完善的 Nacos 配置管理和权限控制 Go SDK,提供简洁易用的 API 来操作 Nacos 配置中心。 A comprehensive Nacos configuration management and permission control Go SDK that provides simple and easy-to-use APIs for operating Nacos Configuration Center.
- 获取配置 Get Configuration
- 发布配置 Publish Configuration
- 删除配置 Delete Configuration
- 监听配置变化 Listen Configuration Changes
- 查询配置历史 Query Configuration History
- 查询历史详情 Get History Details
- 查询上一版本 Get Previous Version
- 用户管理 User Management
- 创建用户 Create User
- 查询用户列表 List Users
- 修改用户密码 Update User Password
- 删除用户 Delete User
- 角色管理 Role Management
- 创建角色 Create Role
- 查询角色列表 List Roles
- 删除角色 Delete Role
- 权限管理 Permissions Management
- 创建权限 Create Permission
- 查询权限列表 List Permissions
- 删除权限 Delete Permission
- 查询命名空间 List Namespaces
- 创建命名空间 Create Namespace
- 修改命名空间 Update Namespace
- 删除命名空间 Delete Namespace
nacos-client 支持最新的两个 Go 版本,需要 Go 1.20 或更高版本,并支持 Go modules。 nacos-client supports the 2 latest Go versions and requires Go 1.20+ with modules support.
go mod init github.com/my/repo
go get github.com/cx0110/nacos-config-gopackage main
import (
"fmt"
"log"
nacos "github.com/cx0110/nacos-config-go"
)
func main() {
// 创建客户端 Create client
client := nacos.New(&nacos.Config{
Addr: "http://localhost:8848",
Username: "nacos",
Password: "nacos",
})
// 检查服务健康状态 Check service health
if err := client.Health(); err != nil {
log.Fatal("Health check failed:", err)
}
// 登录获取访问令牌 Login to get access token
if err := client.Login(); err != nil {
log.Fatal("Login failed:", err)
}
// 发布配置 Publish configuration
err := client.PublishConfig(&nacos.PublishConfigRequest{
ConfigBase: nacos.ConfigBase{
DataId: "app.properties",
Group: "DEFAULT_GROUP",
Tenant: "public",
},
Content: "app.name=demo\napp.version=1.0.0",
ContentType: "properties",
})
if err != nil {
log.Fatal("Publish config failed:", err)
}
// 获取配置 Get configuration
content, err := client.GetConfig(&nacos.ConfigBase{
DataId: "app.properties",
Group: "DEFAULT_GROUP",
Tenant: "public",
})
if err != nil {
log.Fatal("Get config failed:", err)
}
fmt.Println("Configuration content:", content)
}// 创建用户 Create user
err := client.CreateUser(&nacos.User{
Username: "testuser",
Password: "password123",
})
// 查询用户列表 List users
users, err := client.GetUsers(&nacos.Page{
PageNo: 1,
PageSize: 50,
})
// 删除用户 Delete user
err := client.DeleteUser(&nacos.DeleteUserRequest{
Username: "testuser",
})// 创建权限 Create permission
err := client.CreatePermission(&nacos.CreatePermissionRequest{
Role: "ADMIN",
NamespaceId: "test-namespace",
Action: "rw", // r=read, w=write, rw=read/write
})
// 查询权限列表 List permissions
permissions, err := client.GetPermissions(&nacos.Page{
PageNo: 1,
PageSize: 50,
})// 注意:此功能正在开发中
// Note: This feature is under development
err := client.ListenConfig(&nacos.ListeningConfigs{
ConfigBase: nacos.ConfigBase{
DataId: "app.properties",
Group: "DEFAULT_GROUP",
Tenant: "public",
},
ContentMD5: "5d41402abc4b2a76b9719d911017c592",
})| 方法 Method | 描述 Description |
|---|---|
GetConfig() |
获取配置 Get configuration |
PublishConfig() |
发布配置 Publish configuration |
DeleteConfig() |
删除配置 Delete configuration |
ListenConfig() |
监听配置变化 Listen to configuration changes |
GetConfigHistory() |
获取配置历史 Get configuration history |
GetConfigHistoryDetail() |
获取历史详情 Get history details |
GetConfigHistoryPrevious() |
获取上一版本 Get previous version |
| 方法 Method | 描述 Description |
|---|---|
CreateUser() |
创建用户 Create user |
GetUsers() |
查询用户列表 List users |
PutUser() |
修改用户 Update user |
DeleteUser() |
删除用户 Delete user |
| 方法 Method | 描述 Description |
|---|---|
CreateRoles() |
创建角色 Create role |
GetRoles() |
查询角色列表 List roles |
DeleteRoles() |
删除角色 Delete role |
| 方法 Method | 描述 Description |
|---|---|
CreatePermission() |
创建权限 Create permission |
GetPermissions() |
查询权限列表 List permissions |
DeletePermission() |
删除权限 Delete permission |
# 运行所有测试 Run all tests
go test ./...
# 运行测试并显示覆盖率 Run tests with coverage
go test -v -coverprofile=coverage.out ./...
# 生成覆盖率报告 Generate coverage report
go tool cover -html=coverage.out -o coverage.html我们欢迎所有形式的贡献!我们欢迎任何形式的贡献,包括但不限于: We welcome all forms of contribution! This includes but is not limited to:
- 🐛 报告 Bug (Bug Reports)
- 💡 新功能建议 (Feature Requests)
- 📝 文档改进 (Documentation Improvements)
- 🔧 代码贡献 (Code Contributions)
- 🧪 测试用例 (Test Cases)
请查看 CONTRIBUTING.md 了解详细的贡献指南。 Please check CONTRIBUTING.md for detailed contribution guidelines.
本项目采用 Apache 2.0 许可证。详情请查看 LICENSE 文件。 This project is licensed under the Apache 2.0 License. See the LICENSE file for details.
- 📧 Email: githun.cx0110@gmail.com
- 🐛 Issues: GitHub Issues
- 💬 Discussions: GitHub Discussions