mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-12 04:55:59 +08:00
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:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user