Support mongodb cluster

pull/351/head
skiffer-git 2 years ago
parent 78ce5c3f69
commit eb2a9d540e

@ -90,7 +90,7 @@ func (ws *WServer) readMsg(conn *UserConn) {
log.NewInfo("", "this is a pingMessage") log.NewInfo("", "this is a pingMessage")
} }
if err != nil { if err != nil {
log.Error("", "WS ReadMsg error ", " userIP", conn.RemoteAddr().String(), "userUid", "platform", "error", err.Error()) log.NewWarn("", "WS ReadMsg error ", " userIP", conn.RemoteAddr().String(), "userUid", "platform", "error", err.Error())
userCount-- userCount--
ws.delUserConn(conn) ws.delUserConn(conn)
return return

@ -103,17 +103,17 @@ type config struct {
SlowThreshold int `yaml:"slowThreshold"` SlowThreshold int `yaml:"slowThreshold"`
} }
Mongo struct { Mongo struct {
DBUri string `yaml:"dbUri"` DBUri string `yaml:"dbUri"`
DBAddress string `yaml:"dbAddress"` DBAddress []string `yaml:"dbAddress"`
DBDirect bool `yaml:"dbDirect"` DBDirect bool `yaml:"dbDirect"`
DBTimeout int `yaml:"dbTimeout"` DBTimeout int `yaml:"dbTimeout"`
DBDatabase string `yaml:"dbDatabase"` DBDatabase string `yaml:"dbDatabase"`
DBSource string `yaml:"dbSource"` DBSource string `yaml:"dbSource"`
DBUserName string `yaml:"dbUserName"` DBUserName string `yaml:"dbUserName"`
DBPassword string `yaml:"dbPassword"` DBPassword string `yaml:"dbPassword"`
DBMaxPoolSize int `yaml:"dbMaxPoolSize"` DBMaxPoolSize int `yaml:"dbMaxPoolSize"`
DBRetainChatRecords int `yaml:"dbRetainChatRecords"` DBRetainChatRecords int `yaml:"dbRetainChatRecords"`
ChatRecordsClearTime string `yaml:"chatRecordsClearTime"` ChatRecordsClearTime string `yaml:"chatRecordsClearTime"`
} }
Redis struct { Redis struct {
DBAddress []string `yaml:"dbAddress"` DBAddress []string `yaml:"dbAddress"`

@ -337,4 +337,4 @@ const StatisticsTimeInterval = 60
const MaxNotificationNum = 100 const MaxNotificationNum = 100
const CurrentVersion = "v2.3.3" const CurrentVersion = "v2.3.3-rc0"

@ -58,15 +58,29 @@ func init() {
// example: mongodb://$user:$password@mongo1.mongo:27017,mongo2.mongo:27017,mongo3.mongo:27017/$DBDatabase/?replicaSet=rs0&readPreference=secondary&authSource=admin&maxPoolSize=$DBMaxPoolSize // example: mongodb://$user:$password@mongo1.mongo:27017,mongo2.mongo:27017,mongo3.mongo:27017/$DBDatabase/?replicaSet=rs0&readPreference=secondary&authSource=admin&maxPoolSize=$DBMaxPoolSize
uri = config.Config.Mongo.DBUri uri = config.Config.Mongo.DBUri
} else { } else {
//mongodb://mongodb1.example.com:27317,mongodb2.example.com:27017/?replicaSet=mySet&authSource=authDB
mongodbHosts := ""
for i, v := range config.Config.Mongo.DBAddress {
if i == len(config.Config.Mongo.DBAddress)-1 {
mongodbHosts += v
} else {
mongodbHosts += v + ","
}
}
if config.Config.Mongo.DBPassword != "" && config.Config.Mongo.DBUserName != "" { if config.Config.Mongo.DBPassword != "" && config.Config.Mongo.DBUserName != "" {
uri = fmt.Sprintf("mongodb://%s:%s@%s/%s?maxPoolSize=%d&authSource=admin", config.Config.Mongo.DBUserName, config.Config.Mongo.DBPassword, config.Config.Mongo.DBAddress, // clientOpts := options.Client().ApplyURI("mongodb://localhost:27017,localhost:27018/?replicaSet=replset")
//mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[defaultauthdb][?options]]
uri = fmt.Sprintf("mongodb://%s:%s@%s/%s?maxPoolSize=%d&authSource=admin&replicaSet=replset",
config.Config.Mongo.DBUserName, config.Config.Mongo.DBPassword, mongodbHosts,
config.Config.Mongo.DBDatabase, config.Config.Mongo.DBMaxPoolSize) config.Config.Mongo.DBDatabase, config.Config.Mongo.DBMaxPoolSize)
} else { } else {
uri = fmt.Sprintf("mongodb://%s/%s/?maxPoolSize=%d&authSource=admin", uri = fmt.Sprintf("mongodb://%s/%s/?maxPoolSize=%d&authSource=admin&replicaSet=replset",
config.Config.Mongo.DBAddress, config.Config.Mongo.DBDatabase, mongodbHosts, config.Config.Mongo.DBDatabase,
config.Config.Mongo.DBMaxPoolSize) config.Config.Mongo.DBMaxPoolSize)
} }
} }
mongoClient, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri)) mongoClient, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
if err != nil { if err != nil {
fmt.Println(" mongo.Connect failed, try ", utils.GetSelfFuncName(), err.Error(), uri) fmt.Println(" mongo.Connect failed, try ", utils.GetSelfFuncName(), err.Error(), uri)

Loading…
Cancel
Save