新增 zhub 客户端完整实现,包括配置加载、TCP通信、消息发布/订阅、 RPC 调用、分布式锁等功能。支持从项目配置文件、环境变量等多种方式初始化客户端,并提供便捷的全局调用接口。- 添加 `.gitignore` 忽略 IDE 和临时文件 - 实现 `api.go` 提供全局便捷方法封装客户端调用 - 实现 `client.go` 核心客户端逻辑,包含网络通信、消息处理等 - 添加 `client_test.go` 单元测试和集成测试示例 - 实现 `config.go` 支持灵活的配置加载机制 - 添加示例配置文件 `example-config.yml` - 初始化 Go 模块依赖 `go.mod` 和 `go.sum` - 实现 `init.go` 提供多种初始化方式 - 添加 MIT 许可证文件 `LICENSE` - 新增使用示例 `example/main.go` 展示基本功能调用 - 实现 `manager.go` 管理默认客户端实例及初始化逻辑
77 lines
1.3 KiB
Go
77 lines
1.3 KiB
Go
package zhub
|
|
|
|
import "strings"
|
|
|
|
// Lock 分布式锁
|
|
type Lock struct {
|
|
Key string // lock Key
|
|
Value string // lock Value
|
|
flagChan chan int //
|
|
}
|
|
|
|
// Rpc RPC 请求结构
|
|
type Rpc struct {
|
|
Ruk string `json:"ruk"`
|
|
Topic string `json:"topic"`
|
|
Value string `json:"value"`
|
|
|
|
Ch chan int `json:"-"` //请求返回标记
|
|
RpcResult RpcResult `json:"-"`
|
|
}
|
|
|
|
// RpcRet RPC 返回值接口
|
|
type RpcRet interface {
|
|
GetRuk() string
|
|
GetRetcode() int
|
|
GetRetinfo() string
|
|
GetResult() any
|
|
}
|
|
|
|
// RpcResult RPC 返回结果
|
|
type RpcResult struct {
|
|
Ruk string `json:"ruk"`
|
|
Retcode int `json:"retcode"`
|
|
Retinfo string `json:"retinfo"`
|
|
Result any `json:"result"`
|
|
}
|
|
|
|
func (r *RpcResult) GetRuk() string {
|
|
return r.Ruk
|
|
}
|
|
|
|
func (r *RpcResult) GetRetcode() int {
|
|
return r.Retcode
|
|
}
|
|
|
|
func (r *RpcResult) GetRetinfo() string {
|
|
return r.Retinfo
|
|
}
|
|
|
|
func (r *RpcResult) GetResult() any {
|
|
return r.Result
|
|
}
|
|
|
|
func (r *RpcResult) Err(err error) RpcResult {
|
|
r.Retcode = 100
|
|
r.Retinfo = err.Error()
|
|
return *r
|
|
}
|
|
|
|
func (r Rpc) backTopic() string {
|
|
return strings.Split(r.Ruk, "::")[0]
|
|
}
|
|
|
|
func (r Rpc) Err(err error) RpcResult {
|
|
return RpcResult{
|
|
Retcode: 100,
|
|
Retinfo: err.Error(),
|
|
}
|
|
}
|
|
|
|
func (r Rpc) Render(result any) RpcResult {
|
|
return RpcResult{
|
|
Retcode: 0,
|
|
Result: result,
|
|
}
|
|
}
|