optimization: change the configuration file from being read globally … (#1935)

* optimization: change the configuration file from being read globally to being read independently.

* optimization: change the configuration file from being read globally to being read independently.

* optimization: change the configuration file from being read globally to being read independently.

* optimization: config file changed to dependency injection.

* fix: replace global config with dependency injection

* fix: replace global config with dependency injection

* fix: import the enough param

* fix: import the enough param

* fix: import the enough param

* fix: fix the component check of path

* fix: fix the kafka of tls is nil problem

* fix: fix the TLS.CACrt is nil error

* fix: fix the valiable shadows problem

* fix: fix the comflect

* optimization: message remove options.

* fix: fix the param pass error

* fix: find error

* fix: find error

* fix: find eror

* fix: find error

* fix: find error

* fix: del the undifined func

* fix: find error

* fix: fix the error

* fix: pass config

* fix: find error

* fix: find error

* fix: find error

* fix: find error

* fix: find error

* fix: fix the config

* fix: fix the error

* fix: fix the config pass error

* fix: fix the eror

* fix: fix the error

* fix: fix the error

* fix: fix the error

* fix: find error

* fix: fix the error

* fix: fix the config

* fix: add return err

* fix: fix the err2

* fix: err

* fix: fix the func

* fix: del the chinese comment

* fix: fix the func

* fix: fix the gateway_test logic

* fix: s3

* test

* test

* fix: not found

---------

Co-authored-by: luhaoling <2198702716@qq.com>
Co-authored-by: withchao <993506633@qq.com>
This commit is contained in:
OpenIM-Gordon
2024-03-05 17:53:22 +08:00
committed by GitHub
parent efb8310531
commit 383758782e
114 changed files with 1743 additions and 1802 deletions
+59 -39
View File
@@ -47,27 +47,33 @@ var (
cfgPath = flag.String("c", defaultCfgPath, "Path to the configuration file")
)
func initCfg() error {
func initCfg() (*config.GlobalConfig, error) {
data, err := os.ReadFile(*cfgPath)
if err != nil {
return err
return nil, errs.Wrap(err, "ReadFile unmarshal failed")
}
return yaml.Unmarshal(data, &config.Config)
conf := config.NewGlobalConfig()
err = yaml.Unmarshal(data, &conf)
if err != nil {
return nil, errs.Wrap(err, "InitConfig unmarshal failed")
}
return conf, nil
}
type checkFunc struct {
name string
function func() error
function func(*config.GlobalConfig) error
flag bool
config *config.GlobalConfig
}
func main() {
flag.Parse()
if err := initCfg(); err != nil {
conf, err := initCfg()
if err != nil {
fmt.Printf("Read config failed: %v\n", err)
return
}
@@ -75,11 +81,11 @@ func main() {
checks := []checkFunc{
//{name: "Mysql", function: checkMysql},
{name: "Mongo", function: checkMongo},
{name: "Redis", function: checkRedis},
{name: "Minio", function: checkMinio},
{name: "Zookeeper", function: checkZookeeper},
{name: "Kafka", function: checkKafka},
{name: "Mongo", function: checkMongo, config: conf},
{name: "Redis", function: checkRedis, config: conf},
{name: "Minio", function: checkMinio, config: conf},
{name: "Zookeeper", function: checkZookeeper, config: conf},
{name: "Kafka", function: checkKafka, config: conf},
}
for i := 0; i < maxRetry; i++ {
@@ -92,7 +98,7 @@ func main() {
allSuccess := true
for index, check := range checks {
if !check.flag {
err = check.function()
err = check.function(check.config)
if err != nil {
component.ErrorPrint(fmt.Sprintf("Starting %s failed:%v.", check.name, err))
allSuccess = false
@@ -112,30 +118,30 @@ func main() {
}
// checkMongo checks the MongoDB connection without retries
func checkMongo() error {
_, err := unrelation.NewMongo()
func checkMongo(config *config.GlobalConfig) error {
_, err := unrelation.NewMongo(config)
return err
}
// checkRedis checks the Redis connection
func checkRedis() error {
_, err := cache.NewRedis()
func checkRedis(config *config.GlobalConfig) error {
_, err := cache.NewRedis(config)
return err
}
// checkMinio checks the MinIO connection
func checkMinio() error {
func checkMinio(config *config.GlobalConfig) error {
// Check if MinIO is enabled
if config.Config.Object.Enable != "minio" {
if config.Object.Enable != "minio" {
return errs.Wrap(errors.New("minio.Enable is empty"))
}
minio := &component.Minio{
ApiURL: config.Config.Object.ApiURL,
Endpoint: config.Config.Object.Minio.Endpoint,
AccessKeyID: config.Config.Object.Minio.AccessKeyID,
SecretAccessKey: config.Config.Object.Minio.SecretAccessKey,
SignEndpoint: config.Config.Object.Minio.SignEndpoint,
ApiURL: config.Object.ApiURL,
Endpoint: config.Object.Minio.Endpoint,
AccessKeyID: config.Object.Minio.AccessKeyID,
SecretAccessKey: config.Object.Minio.SecretAccessKey,
SignEndpoint: config.Object.Minio.SignEndpoint,
UseSSL: getEnv("MINIO_USE_SSL", "false"),
}
err := component.CheckMinio(minio)
@@ -143,18 +149,18 @@ func checkMinio() error {
}
// checkZookeeper checks the Zookeeper connection
func checkZookeeper() error {
_, err := zookeeper.NewZookeeperDiscoveryRegister()
func checkZookeeper(config *config.GlobalConfig) error {
_, err := zookeeper.NewZookeeperDiscoveryRegister(config)
return err
}
// checkKafka checks the Kafka connection
func checkKafka() error {
func checkKafka(config *config.GlobalConfig) error {
// Prioritize environment variables
kafkaStu := &component.Kafka{
Username: config.Config.Kafka.Username,
Password: config.Config.Kafka.Password,
Addr: config.Config.Kafka.Addr,
Username: config.Kafka.Username,
Password: config.Kafka.Password,
Addr: config.Kafka.Addr,
}
kafkaClient, err := component.CheckKafka(kafkaStu)
@@ -170,9 +176,9 @@ func checkKafka() error {
}
requiredTopics := []string{
config.Config.Kafka.MsgToMongo.Topic,
config.Config.Kafka.MsgToPush.Topic,
config.Config.Kafka.LatestMsgToRedis.Topic,
config.Kafka.MsgToMongo.Topic,
config.Kafka.MsgToPush.Topic,
config.Kafka.LatestMsgToRedis.Topic,
}
for _, requiredTopic := range requiredTopics {
@@ -181,11 +187,25 @@ func checkKafka() error {
}
}
var tlsConfig *kafka.TLSConfig
if config.Kafka.TLS != nil {
tlsConfig = &kafka.TLSConfig{
CACrt: config.Kafka.TLS.CACrt,
ClientCrt: config.Kafka.TLS.ClientCrt,
ClientKey: config.Kafka.TLS.ClientKey,
ClientKeyPwd: config.Kafka.TLS.ClientKeyPwd,
InsecureSkipVerify: config.Kafka.TLS.InsecureSkipVerify,
}
}
_, err = kafka.NewMConsumerGroup(&kafka.MConsumerGroupConfig{
KafkaVersion: sarama.V2_0_0_0,
OffsetsInitial: sarama.OffsetNewest, IsReturnErr: false,
}, []string{config.Config.Kafka.LatestMsgToRedis.Topic},
config.Config.Kafka.Addr, config.Config.Kafka.ConsumerGroupID.MsgToRedis)
OffsetsInitial: sarama.OffsetNewest,
IsReturnErr: false,
UserName: config.Kafka.Username,
Password: config.Kafka.Password,
}, []string{config.Kafka.LatestMsgToRedis.Topic},
config.Kafka.Addr, config.Kafka.ConsumerGroupID.MsgToRedis, tlsConfig)
if err != nil {
return err
}
@@ -193,8 +213,8 @@ func checkKafka() error {
_, err = kafka.NewMConsumerGroup(&kafka.MConsumerGroupConfig{
KafkaVersion: sarama.V2_0_0_0,
OffsetsInitial: sarama.OffsetNewest, IsReturnErr: false,
}, []string{config.Config.Kafka.MsgToPush.Topic},
config.Config.Kafka.Addr, config.Config.Kafka.ConsumerGroupID.MsgToMongo)
}, []string{config.Kafka.MsgToPush.Topic},
config.Kafka.Addr, config.Kafka.ConsumerGroupID.MsgToMongo, tlsConfig)
if err != nil {
return err
}
@@ -202,8 +222,8 @@ func checkKafka() error {
kafka.NewMConsumerGroup(&kafka.MConsumerGroupConfig{
KafkaVersion: sarama.V2_0_0_0,
OffsetsInitial: sarama.OffsetNewest, IsReturnErr: false,
}, []string{config.Config.Kafka.MsgToPush.Topic}, config.Config.Kafka.Addr,
config.Config.Kafka.ConsumerGroupID.MsgToPush)
}, []string{config.Kafka.MsgToPush.Topic}, config.Kafka.Addr,
config.Kafka.ConsumerGroupID.MsgToPush, tlsConfig)
if err != nil {
return err
}
+10 -19
View File
@@ -21,20 +21,11 @@ import (
"time"
"github.com/redis/go-redis/v9"
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
)
// Mock for initCfg for testing purpose
func mockInitCfg() error {
config.Config.Mysql.Username = "root"
config.Config.Mysql.Password = "openIM123"
config.Config.Mysql.Address = []string{"127.0.0.1:13306"}
return nil
}
func TestRedis(t *testing.T) {
config.Config.Redis.Address = []string{
conf, err := initCfg()
conf.Redis.Address = []string{
"172.16.8.142:7000",
//"172.16.8.142:7000", "172.16.8.142:7001", "172.16.8.142:7002", "172.16.8.142:7003", "172.16.8.142:7004", "172.16.8.142:7005",
}
@@ -45,20 +36,20 @@ func TestRedis(t *testing.T) {
redisClient.Close()
}
}()
if len(config.Config.Redis.Address) > 1 {
if len(conf.Redis.Address) > 1 {
redisClient = redis.NewClusterClient(&redis.ClusterOptions{
Addrs: config.Config.Redis.Address,
Username: config.Config.Redis.Username,
Password: config.Config.Redis.Password,
Addrs: conf.Redis.Address,
Username: conf.Redis.Username,
Password: conf.Redis.Password,
})
} else {
redisClient = redis.NewClient(&redis.Options{
Addr: config.Config.Redis.Address[0],
Username: config.Config.Redis.Username,
Password: config.Config.Redis.Password,
Addr: conf.Redis.Address[0],
Username: conf.Redis.Username,
Password: conf.Redis.Password,
})
}
_, err := redisClient.Ping(context.Background()).Result()
_, err = redisClient.Ping(context.Background()).Result()
if err != nil {
t.Fatal(err)
}
+21 -13
View File
@@ -18,6 +18,7 @@ import (
"context"
"errors"
"fmt"
"github.com/OpenIMSDK/tools/errs"
"log"
"os"
"reflect"
@@ -45,36 +46,43 @@ const (
versionValue = 35
)
func InitConfig(path string) error {
func InitConfig(path string) (*config.GlobalConfig, error) {
data, err := os.ReadFile(path)
if err != nil {
return err
return nil, errs.Wrap(err, "ReadFile unmarshal failed")
}
return yaml.Unmarshal(data, &config.Config)
conf := config.NewGlobalConfig()
err = yaml.Unmarshal(data, &conf)
if err != nil {
return nil, errs.Wrap(err, "InitConfig unmarshal failed")
}
return conf, nil
}
func GetMysql() (*gorm.DB, error) {
conf := config.Config.Mysql
func GetMysql(config *config.GlobalConfig) (*gorm.DB, error) {
conf := config.Mysql
mysqlDSN := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", conf.Username, conf.Password, conf.Address[0], conf.Database)
return gorm.Open(gormmysql.Open(mysqlDSN), &gorm.Config{Logger: logger.Discard})
}
func GetMongo() (*mongo.Database, error) {
mgo, err := unrelation.NewMongo()
func GetMongo(config *config.GlobalConfig) (*mongo.Database, error) {
mgo, err := unrelation.NewMongo(config)
if err != nil {
return nil, err
}
return mgo.GetDatabase(), nil
return mgo.GetDatabase(config.Mongo.Database), nil
}
func Main(path string) error {
if err := InitConfig(path); err != nil {
conf, err := InitConfig(path)
if err != nil {
return err
}
if config.Config.Mysql == nil {
if conf.Mysql == nil {
return nil
}
mongoDB, err := GetMongo()
mongoDB, err := GetMongo(conf)
if err != nil {
return err
}
@@ -91,7 +99,7 @@ func Main(path string) error {
default:
return err
}
mysqlDB, err := GetMysql()
mysqlDB, err := GetMysql(conf)
if err != nil {
if mysqlErr, ok := err.(*mysql.MySQLError); ok && mysqlErr.Number == 1049 {
if err := SetMongoDataVersion(mongoDB, version.Value); err != nil {
@@ -113,7 +121,7 @@ func Main(path string) error {
func() error { return NewTask(mysqlDB, mongoDB, mgo.NewGroupMember, c.GroupMember) },
func() error { return NewTask(mysqlDB, mongoDB, mgo.NewGroupRequestMgo, c.GroupRequest) },
func() error { return NewTask(mysqlDB, mongoDB, mgo.NewConversationMongo, c.Conversation) },
func() error { return NewTask(mysqlDB, mongoDB, mgo.NewS3Mongo, c.Object(config.Config.Object.Enable)) },
func() error { return NewTask(mysqlDB, mongoDB, mgo.NewS3Mongo, c.Object(conf.Object.Enable)) },
func() error { return NewTask(mysqlDB, mongoDB, mgo.NewLogMongo, c.Log) },
func() error { return NewTask(mysqlDB, mongoDB, rtcmgo.NewSignal, c.SignalModel) },