上传db链接优化

This commit is contained in:
liwei1dao 2023-12-15 11:46:50 +08:00
parent 3554b24695
commit 0670dee5b2

View File

@ -6,6 +6,7 @@ import (
"fmt"
"go_dreamfactory/comm"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/lego/sys/mgo"
"go_dreamfactory/lego/utils/codec/json"
"go_dreamfactory/pb"
"io/ioutil"
@ -85,6 +86,18 @@ func (this *DB) readercrossconf(path string) (err error) {
)
return
}
err = this.RegiestServerDBConn(&pb.ServiceDBInfo{
Serverid: this.options.ServiceId,
ServerName: this.options.ServiceId,
Owner: this.options.CrossChannel,
CrossId: this.options.CrossChannel,
RedisIsCluster: this.options.RedisIsCluster,
RedisAddr: this.options.RedisAddr,
RedisPassword: this.options.RedisPassword,
RedisDb: int32(this.options.RedisDB),
MongodbUrl: this.options.MongodbUrl,
MongodbDatabase: this.options.MongodbDatabase,
})
} else {
err = this.ConnectServiceList()
}
@ -173,7 +186,7 @@ func (this *DB) ServerDBConn(stage string) (conn *DBConn, err error) {
ok := false
conn, ok = this.servers[stage]
if !ok {
err = fmt.Errorf("DBConn:%s on init", stage)
conn, err = this.ConnectServerDBConn(stage)
}
return
}
@ -185,3 +198,36 @@ func (this *DB) GetServerTags() []string {
}
return keys
}
//链接到目标服务数据对象
func (this *DB) ConnectServerDBConn(stage string) (conn *DBConn, err error) {
temp := &pb.ServiceDBInfo{}
if err = this.local.Mgo.FindOne("serverdata", bson.M{"cross": this.options.CrossChannel, "serverid": stage}).Decode(temp); err != nil {
return
}
if this.servers[temp.Serverid], err = newDBConn(this.options.Log, DBConfig{
RedisIsCluster: temp.RedisIsCluster,
RedisAddr: temp.RedisAddr,
RedisPassword: temp.RedisPassword,
RedisDB: int(temp.RedisDb),
MongodbUrl: temp.MongodbUrl,
MongodbDatabase: temp.MongodbDatabase,
}); err != nil {
log.Error("comment db err!",
log.Field{Key: "stag", Value: temp.Serverid},
log.Field{Key: "db", Value: temp},
log.Field{Key: "err", Value: err.Error()},
)
return
}
conn = this.servers[temp.Serverid]
return
}
//注册本服数据到跨服
func (this *DB) RegiestServerDBConn(info *pb.ServiceDBInfo) (err error) {
if this.cross.Mgo.FindOne("serverdata", bson.M{"cross": this.options.CrossChannel, "serverid": info.Serverid}).Err() == mgo.MongodbNil {
_, err = this.cross.Mgo.InsertOne("serverdata", info)
}
return
}