liliang 7 месяцев назад
Родитель
Сommit
721a81221b
2 измененных файлов с 25 добавлено и 16 удалено
  1. 20 14
      components/mongo/component.go
  2. 5 2
      game/config/profile-gc.json

+ 20 - 14
components/mongo/component.go

@@ -2,6 +2,7 @@ package mhayaMongo
 
 import (
 	"context"
+	"crypto/tls"
 	"fmt"
 	"time"
 
@@ -58,11 +59,13 @@ func (s *Component) Init() {
 			item := dbGroup.GetConfig(i)
 
 			var (
-				enable  = item.GetBool("enable", true)
-				id      = item.GetString("db_id")
-				dbName  = item.GetString("db_name")
-				uri     = item.GetString("uri")
-				timeout = time.Duration(item.GetInt64("timeout", 3)) * time.Second
+				enable    = item.GetBool("enable", true)
+				id        = item.GetString("db_id")
+				dbName    = item.GetString("db_name")
+				uri       = item.GetString("uri")
+				timeout   = time.Duration(item.GetInt64("timeout", 3)) * time.Second
+				tlsEnable = item.GetInt("tls")
+				maxClient = item.GetInt("maxClient")
 			)
 
 			for _, key := range mongoIdList.Keys() {
@@ -75,7 +78,7 @@ func (s *Component) Init() {
 					panic(fmt.Sprintf("[dbName = %s] is disabled!", dbName))
 				}
 
-				db, err := CreateDatabase(uri, dbName, timeout)
+				db, err := CreateDatabase(uri, dbName, tlsEnable, uint64(maxClient), timeout)
 				if err != nil {
 					panic(fmt.Sprintf("[dbName = %s] create mongodb fail. error = %s", dbName, err))
 				}
@@ -87,21 +90,24 @@ func (s *Component) Init() {
 	}
 }
 
-func CreateDatabase(uri, dbName string, timeout ...time.Duration) (*mongo.Database, error) {
+func CreateDatabase(uri, dbName string, tlsEnable int, maxClient uint64, timeout ...time.Duration) (*mongo.Database, error) {
 	tt := 5 * time.Second
 
 	if len(timeout) > 0 && timeout[0].Seconds() > 3 {
 		tt = timeout[0]
 	}
 
-	// todo tls开启
-	//tlsConfig := &tls.Config{
-	//	MinVersion:               tls.VersionTLS12,
-	//	PreferServerCipherSuites: true,
-	//}
-	//o := options.Client().ApplyURI(uri).SetTLSConfig(tlsConfig)
+	var o *options.ClientOptions
+	if tlsEnable == 1 {
+		tlsConfig := &tls.Config{
+			MinVersion:               tls.VersionTLS12,
+			PreferServerCipherSuites: true,
+		}
+		o = options.Client().ApplyURI(uri).SetTLSConfig(tlsConfig).SetMaxPoolSize(maxClient)
+	} else {
+		o = options.Client().ApplyURI(uri).SetMaxPoolSize(maxClient)
+	}
 
-	o := options.Client().ApplyURI(uri).SetMaxPoolSize(500)
 	if err := o.Validate(); err != nil {
 		return nil, err
 	}

+ 5 - 2
game/config/profile-gc.json

@@ -226,14 +226,17 @@
         "enable": true,
         "db_id": "game_db_1",
         "db_name": "db_mhaya",
-        "uri": "mongodb://s5.z100.vip:27019"
+        "uri": "mongodb://s5.z100.vip:27019",
+        "tls": 1,
+        "maxClient": 500
       }
     ]
   },
   "redis": {
     "address": "s5.z100.vip:36379",
     "password": "",
-    "db": 0
+    "db": 0,
+    "tls": 1
   },
   "data_config": {
     "parser": "json",