|
@@ -1,12 +1,16 @@
|
|
|
package mdb
|
|
|
|
|
|
import (
|
|
|
+ "context"
|
|
|
+ "crypto/tls"
|
|
|
"github.com/go-redis/redis/v8"
|
|
|
mhayaMongo "github.com/mhaya/components/mongo"
|
|
|
clog "github.com/mhaya/logger"
|
|
|
cactor "github.com/mhaya/net/actor"
|
|
|
cprofile "github.com/mhaya/profile"
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
|
+ "strings"
|
|
|
+ "time"
|
|
|
)
|
|
|
|
|
|
type ActorDB struct {
|
|
@@ -34,19 +38,61 @@ func (p *ActorDB) OnInit() {
|
|
|
dbID := p.App().Settings().GetConfig("db_id_list").GetString("game_db_id")
|
|
|
MDB = mongo.GetDb(dbID)
|
|
|
if MDB == nil {
|
|
|
- clog.Panic("game_db_id not found")
|
|
|
+ clog.Panicf("game_db_id not found")
|
|
|
}
|
|
|
redisConfig := cprofile.GetConfig("redis")
|
|
|
- /* tlsConfig := &tls.Config{
|
|
|
- MinVersion: tls.VersionTLS12,
|
|
|
- PreferServerCipherSuites: true,
|
|
|
- }*/
|
|
|
- RDB = redis.NewUniversalClient(&redis.UniversalOptions{
|
|
|
- Addrs: []string{redisConfig.GetString("address")},
|
|
|
- Password: redisConfig.GetString("password"),
|
|
|
- DB: redisConfig.GetInt("db"),
|
|
|
- //TLSConfig: tlsConfig,
|
|
|
- })
|
|
|
+ addr := strings.Split(redisConfig.GetString("address"), ",")
|
|
|
+ isTls := redisConfig.GetInt("tls", 0)
|
|
|
+ if isTls == 1 {
|
|
|
+ tlsConfig := &tls.Config{
|
|
|
+ MinVersion: tls.VersionTLS12,
|
|
|
+ PreferServerCipherSuites: true,
|
|
|
+ }
|
|
|
+ /* RDB = redis.NewUniversalClient(&redis.UniversalOptions{
|
|
|
+ Addrs: []string{redisConfig.GetString("address")},
|
|
|
+ Password: redisConfig.GetString("password"),
|
|
|
+ DB: redisConfig.GetInt("db"),
|
|
|
+ TLSConfig: tlsConfig,
|
|
|
+ })*/
|
|
|
+
|
|
|
+ clusterOptions := &redis.ClusterOptions{
|
|
|
+ Addrs: addr,
|
|
|
+ Password: "", // 如果启用了身份验证,请提供密码
|
|
|
+ PoolSize: 1000, // 每个节点的最大连接数
|
|
|
+ DialTimeout: 10 * time.Second,
|
|
|
+ ReadTimeout: 30 * time.Second,
|
|
|
+ WriteTimeout: 30 * time.Second,
|
|
|
+ TLSConfig: tlsConfig,
|
|
|
+ }
|
|
|
+
|
|
|
+ // 创建集群客户端
|
|
|
+ rdb := redis.NewClusterClient(clusterOptions)
|
|
|
+ // 测试连接
|
|
|
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
+ defer cancel()
|
|
|
+
|
|
|
+ pong, err := rdb.Ping(ctx).Result()
|
|
|
+ if err != nil {
|
|
|
+ clog.Panicf("无法连接到Redis集群: %v ,param:%v", err, addr)
|
|
|
+ }
|
|
|
+ clog.Warnf("连接到Redis集群: %v,param:%v", pong, addr)
|
|
|
+ RDB = rdb
|
|
|
+ } else {
|
|
|
+ RDB = redis.NewUniversalClient(&redis.UniversalOptions{
|
|
|
+ Addrs: addr,
|
|
|
+ Password: redisConfig.GetString("password"),
|
|
|
+ DB: redisConfig.GetInt("db"),
|
|
|
+ })
|
|
|
+ // 测试连接
|
|
|
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
+ defer cancel()
|
|
|
+
|
|
|
+ pong, err := RDB.Ping(ctx).Result()
|
|
|
+ if err != nil {
|
|
|
+ clog.Panicf("无法连接到Redis: %v ,param:%v", err, addr)
|
|
|
+ }
|
|
|
+ clog.Warnf("连接到Redis: %v,param:%v", pong, addr)
|
|
|
+ }
|
|
|
|
|
|
if p.App().NodeId() == "m-center" {
|
|
|
SetIndex()
|