Skip to content

Commit 0fab75b

Browse files
author
samshi
committed
feat: optimize cluster
1. remove useless code. 2. rename name to id
1 parent 757ae5f commit 0fab75b

File tree

17 files changed

+69
-75
lines changed

17 files changed

+69
-75
lines changed

cluster/impl/configfile/cluster.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,24 @@ func New(filePath string) (base.Cluster, error) {
3737
return cluster, nil
3838
}
3939

40-
// RegInstance 注册本地实例,name传空则默认使用ip作为name,否则请保证该名称与配置文件中的name保持一致
41-
func (cluster *Cluster) RegInstance(ctx context.Context, name string) (base.Instance, error) {
42-
if name == "" {
40+
// RegInstance 注册本地实例,id传空则默认使用ip作为id,只有配置文件中的id才能注册成功,否则反错
41+
func (cluster *Cluster) RegInstance(ctx context.Context, id string) (base.Instance, error) {
42+
if id == "" {
4343
var err error
44-
name, err = base.GetLocalIP()
44+
id, err = base.GetLocalIP()
4545
if err != nil {
4646
return nil, err
4747
}
4848
}
4949
for _, ins := range cluster.baseInsList {
5050
// 查找本机是否在配置的ins列表,如果在,则注册成功
51-
if ins.GetName() == name {
51+
if ins.GetID() == id {
5252
cluster.localInstance = ins
5353
return ins, nil
5454
}
5555
}
5656
// 本机不在配置中,返回失败
57-
return nil, fmt.Errorf("invalid instance. name:%v", name)
57+
return nil, fmt.Errorf("invalid instance. id:%v", id)
5858
}
5959

6060
// UnregInstance 注销本地实例
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
instance_list:
2-
- name: 192.168.0.1
3-
- name: 192.168.0.2
4-
- name: 192.168.0.3
2+
- id: 192.168.0.1
3+
- id: 192.168.0.2
4+
- id: 192.168.0.3
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
instance_list:
2-
- name: 192.168.0.1
3-
- name: 192.168.0.2
4-
- name: 192.168.0.3
2+
- id: 192.168.0.1
3+
- id: 192.168.0.2
4+
- id: 192.168.0.3

cluster/impl/configfile/example/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ replace github.com/tencent-connect/botgo-plugins/cluster/base => ../../../base
77
replace github.com/tencent-connect/botgo-plugins/cluster/impl/configfile => ../
88

99
require (
10-
github.com/tencent-connect/botgo-plugins/cluster/base v0.0.0-20211124034518-37adad080eb7
10+
github.com/tencent-connect/botgo-plugins/cluster/base v0.0.0-20211124073815-757ae5fa4913
1111
github.com/tencent-connect/botgo-plugins/cluster/impl/configfile v0.0.0-00010101000000-000000000000
1212
)

cluster/impl/configfile/example/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func main() {
2727
}
2828
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
2929
defer cancel()
30-
// 注册当前实例,name传空,默认使用ip作为名称
30+
// 注册当前实例,id传空,默认使用ip作为名称
3131
ins, err := cluster.RegInstance(ctx, "")
3232
if err != nil {
3333
fmt.Printf("reg failed. err:%v\n", err)
@@ -80,7 +80,7 @@ func getAllInstance(cluster base.Cluster) {
8080
}
8181
fmt.Printf("get all ins num:%v\n", len(all))
8282
for _, ins := range all {
83-
fmt.Printf("ins=====>:%v\n", ins.GetName())
83+
fmt.Printf("ins=====>:%v\n", ins.GetID())
8484
}
8585
}
8686

cluster/impl/configfile/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ go 1.15
44

55
require (
66
github.com/agiledragon/gomonkey/v2 v2.3.1
7-
github.com/tencent-connect/botgo-plugins/cluster/base v0.0.0-20211124034518-37adad080eb7
7+
github.com/tencent-connect/botgo-plugins/cluster/base v0.0.0-20211124073815-757ae5fa4913
88
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
99
)

cluster/impl/configfile/go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ github.com/tencent-connect/botgo-plugins/cluster/base v0.0.0-20211123172531-daee
1212
github.com/tencent-connect/botgo-plugins/cluster/base v0.0.0-20211123172531-daee3d202d89/go.mod h1:Mqc/VXp1cMJehHB6TC0EWf2sXsDTZwliNOkytByuxjI=
1313
github.com/tencent-connect/botgo-plugins/cluster/base v0.0.0-20211124034518-37adad080eb7 h1:Fz+8sKChKw66LkT8LsDLro3G1BGWPfEDEtX9HkGs78Q=
1414
github.com/tencent-connect/botgo-plugins/cluster/base v0.0.0-20211124034518-37adad080eb7/go.mod h1:Mqc/VXp1cMJehHB6TC0EWf2sXsDTZwliNOkytByuxjI=
15+
github.com/tencent-connect/botgo-plugins/cluster/base v0.0.0-20211124045614-539d45c4107b h1:ptGDEqMFJ8FnfodszqrdPARqMCE3LfCmeWRCXdxVbME=
16+
github.com/tencent-connect/botgo-plugins/cluster/base v0.0.0-20211124045614-539d45c4107b/go.mod h1:Mqc/VXp1cMJehHB6TC0EWf2sXsDTZwliNOkytByuxjI=
17+
github.com/tencent-connect/botgo-plugins/cluster/base v0.0.0-20211124073815-757ae5fa4913 h1:IsBPvxKKdZBqLpnm0EbgxwD3dQnnmeWSZwWpOo4McTs=
18+
github.com/tencent-connect/botgo-plugins/cluster/base v0.0.0-20211124073815-757ae5fa4913/go.mod h1:Mqc/VXp1cMJehHB6TC0EWf2sXsDTZwliNOkytByuxjI=
1519
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
1620
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
1721
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
11
// Package configfile 基于配置文件实现的集群实例
22
package configfile
33

4-
import "github.com/tencent-connect/botgo-plugins/cluster/base"
5-
64
// Instance 实例配置
75
type Instance struct {
8-
// Name 实例名称,需要保证唯一
9-
Name string `yaml:"name"`
6+
// ID 实例ID,需要保证唯一
7+
ID string `yaml:"id"`
108
}
119

12-
// GetName 获取实例名称
13-
func (ins *Instance) GetName() string {
14-
return ins.Name
10+
// GetID 获取实例名称
11+
func (ins *Instance) GetID() string {
12+
return ins.ID
1513
}
1614

1715
// IsValid 是否是有效实例
1816
func (ins *Instance) IsValid() bool {
19-
return ins.Name != ""
20-
}
21-
22-
// IsSame 是否是相同实例
23-
func (ins *Instance) IsSame(i base.Instance) bool {
24-
return ins.Name == i.GetName()
17+
return ins.ID != ""
2518
}

cluster/impl/etcd/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# 概要说明
22
* 本模块实现基于ETCD的分布式集群管理器;
3-
* 每个实例以自身ip作为实例唯一标识
3+
* 每个实例默认以自身ip作为实例唯一标识,在容器场景,由于ip可能有重复,请在注册时指定id(例如使用容器id)
44
* 本模块主要用于搭配 schedule 模块,实现机器人集群实例的分区调度。
55

66
# 使用方法
77
参见example。
88

99
# 注意事项
10-
如果是容器场景,可能存在容器ip相同情况,此时请在RegInstance是指定实例名称(例如使用容器id),而不要使用默认的ip作为名称
10+
如果是容器场景,可能存在容器ip相同情况,此时请在RegInstance是指定实例id(例如使用容器id),而不要使用默认的ip作为id

cluster/impl/etcd/cluster.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,14 @@ func NewArgs(clusterName string, endpoints []string) *Args {
7373
}
7474
}
7575

76-
// RegInstance 注册实例,如果name为空,则自动使用ip作为名称,完整实例名称为 clusterName_name
77-
func (cluster *Cluster) RegInstance(ctx context.Context, name string) (base.Instance, error) {
76+
// RegInstance 注册实例,如果id为空,则自动使用ip作为id,完整实例名称为 clusterName_id
77+
func (cluster *Cluster) RegInstance(ctx context.Context, id string) (base.Instance, error) {
7878
if cluster.localInstance != nil {
7979
// 已注册,直接返回
8080
return cluster.localInstance, nil
8181
}
8282
// 创建实例
83-
ins, err := newInstance(cluster.args.ClusterName, name)
83+
ins, err := newInstance(cluster.args.ClusterName, id)
8484
if err != nil {
8585
return nil, err
8686
}
@@ -131,7 +131,7 @@ func (cluster *Cluster) GetAllInstances(ctx context.Context) ([]base.Instance, e
131131
}
132132
var instances []base.Instance
133133
for _, item := range rsp.Kvs {
134-
ins, err := newInstanceWithName(string(item.Key))
134+
ins, err := newInstanceWithID(string(item.Key))
135135
if err != nil {
136136
continue
137137
}
@@ -208,7 +208,7 @@ func (cluster *Cluster) startHeartBeat(ins *Instance) error {
208208
if r := recover(); r != nil {
209209
buf := make([]byte, 4096)
210210
buf = buf[:runtime.Stack(buf, false)]
211-
fmt.Printf("[HeartBeatPanic]ins:%v, err:%v, stack:\n%s\n", ins.GetName(), r, buf)
211+
fmt.Printf("[HeartBeatPanic]ins:%v, err:%v, stack:\n%s\n", ins.GetID(), r, buf)
212212
// 如果故障,则退出进程(通常不可能进入到这里)
213213
os.Exit(-1)
214214
}
@@ -272,7 +272,7 @@ func delNode(ctx context.Context, cli *clientv3.Client, ins *Instance) error {
272272
if !ins.IsValid() {
273273
return errors.New("invalid instance")
274274
}
275-
_, err := cli.Delete(ctx, ins.GetName())
275+
_, err := cli.Delete(ctx, ins.GetID())
276276
_, _ = cli.Revoke(ctx, ins.leaseID)
277277
ins.clear()
278278
return err
@@ -289,7 +289,7 @@ func putNode(ctx context.Context, cli *clientv3.Client, ins *Instance, ttl int64
289289
return err
290290
}
291291
// 写入etcd节点,节点内容暂无实际意义,暂且写入1
292-
_, err = cli.Put(ctx, ins.GetName(), "1", clientv3.WithLease(rsp.ID))
292+
_, err = cli.Put(ctx, ins.GetID(), "1", clientv3.WithLease(rsp.ID))
293293
if err != nil {
294294
return err
295295
}

0 commit comments

Comments
 (0)