1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- package mdb
- import (
- "context"
- "crypto/tls"
- "fmt"
- "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"
- )
- type ActorDB struct {
- cactor.Base
- Name string
- }
- var (
- MDB *mongo.Database
- RDB redis.UniversalClient
- )
- func (p *ActorDB) AliasID() string {
- return "db" + p.Name
- }
- // OnInit Actor初始化前触发该函数
- func (p *ActorDB) OnInit() {
- mongo := p.App().Find(mhayaMongo.Name).(*mhayaMongo.Component)
- if mongo == nil {
- clog.DPanicf("[component = %s] not found.", mhayaMongo.Name)
- }
- // 获取 db_id = "center_db_1" 的配置
- 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")
- }
- redisConfig := cprofile.GetConfig("redis")
- tlsEnable := redisConfig.GetInt("tls")
- if tlsEnable == 1 {
- tlsConfig := &tls.Config{
- MinVersion: tls.VersionTLS12,
- PreferServerCipherSuites: true,
- }
- RDB = redis.NewUniversalClient(&redis.UniversalOptions{
- Addrs: strings.Split(redisConfig.GetString("address"), ","),
- Password: redisConfig.GetString("password"),
- DB: redisConfig.GetInt("db"),
- TLSConfig: tlsConfig,
- })
- clog.Debugf("redis Addrs str: ", redisConfig.GetString("address"), ",")
- split := strings.Split(redisConfig.GetString("address"), ",")
- for i := 0; i < len(split); i++ {
- if i > 0 {
- fmt.Print(", ")
- }
- clog.Debugf("addr : %s", split[i])
- }
- err := RDB.Set(context.Background(), "test", 0, 0).Err()
- clog.Errorf("redis client error %v:", err)
- } else {
- RDB = redis.NewUniversalClient(&redis.UniversalOptions{
- Addrs: strings.Split(redisConfig.GetString("address"), ","),
- Password: redisConfig.GetString("password"),
- DB: redisConfig.GetInt("db"),
- })
- err := RDB.Set(context.Background(), "test", 0, 0).Err()
- clog.Errorf("redis client error %v:", err)
- }
- if p.App().NodeId() == "m-center" {
- SetIndex()
- }
- }
|