diff --git a/cluster/cluster.go b/cluster/cluster.go index cb5feb1..517e14a 100644 --- a/cluster/cluster.go +++ b/cluster/cluster.go @@ -201,7 +201,7 @@ func GetRpcClient(nodeId int,serviceMethod string,clientList []*rpc.Client) (err serviceName := serviceMethod[:findIndex] //1.找到对应的rpcNodeid - return GetCluster().GetNodeIdByService(serviceName,clientList) + return GetCluster().GetNodeIdByService(serviceName,clientList,true) } func GetRpcServer() *rpc.Server{ diff --git a/cluster/parsecfg.go b/cluster/parsecfg.go index 316adb1..b459682 100644 --- a/cluster/parsecfg.go +++ b/cluster/parsecfg.go @@ -194,7 +194,7 @@ func (cls *Cluster) IsConfigService(serviceName string) bool { return false } -func (cls *Cluster) GetNodeIdByService(serviceName string,rpcClientList []*rpc.Client) (error,int) { +func (cls *Cluster) GetNodeIdByService(serviceName string,rpcClientList []*rpc.Client,bAll bool) (error,int) { cls.locker.RLock() defer cls.locker.RUnlock() nodeIdList,ok := cls.mapServiceNode[serviceName] @@ -202,8 +202,7 @@ func (cls *Cluster) GetNodeIdByService(serviceName string,rpcClientList []*rpc.C if ok == true { for _,nodeId := range nodeIdList { pClient := GetCluster().GetRpcClient(nodeId) - if pClient==nil { - log.Error("Cannot connect node id %d",nodeId) + if pClient==nil || (bAll == false && pClient.IsConnected()==false) { continue } rpcClientList[count] = pClient diff --git a/go.mod b/go.mod index 3822be2..855c6a4 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/duanhf2012/origin go 1.15 + +require gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 diff --git a/service/servicemgr.go b/service/servicemgr.go index 862fb35..c80bbf1 100644 --- a/service/servicemgr.go +++ b/service/servicemgr.go @@ -11,7 +11,10 @@ func Init(chanCloseSig chan bool) { closeSig=chanCloseSig for _,s := range mapServiceName { - s.OnInit() + err := s.OnInit() + if err != nil { + panic(err) + } } } diff --git a/sysmodule/mongomodule/mongomodule_test.go b/sysmodule/mongomodule/mongomodule_test.go index 3173897..387ea38 100644 --- a/sysmodule/mongomodule/mongomodule_test.go +++ b/sysmodule/mongomodule/mongomodule_test.go @@ -8,6 +8,8 @@ import ( "time" ) + + type Student struct { ID bson.ObjectId `bson:"_id"` Name string `bson: "name"` @@ -18,7 +20,7 @@ type Student struct { func Test_Example(t *testing.T) { module:=MongoModule{} - module.Init("127.0.0.1",100, 5*time.Second,5*time.Second) + module.Init("mongodb://admin:123456@127.0.0.1:27017",100, 5*time.Second,5*time.Second) // take session s := module.Take() @@ -62,7 +64,7 @@ func Test_Example(t *testing.T) { fmt.Print(err) } - //序号自增 + //7.序号自增 s.EnsureCounter("test2","t_student","5f252f09999c622d36198951") for i := 0; i < 3; i++ { id, err := s.NextSeq("test2", "t_student", "5f252f09999c622d36198951") @@ -72,4 +74,9 @@ func Test_Example(t *testing.T) { } fmt.Println(id) } + + //8.setoninsert使用 + info,uErr := c.Upsert(bson.M{"_id":bson.ObjectIdHex("5f252f09999c622d36198951")},bson.M{ + "$setOnInsert":bson.M{"Name":"setoninsert","Age":55}}) + fmt.Println(info,uErr) }