🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
~~~ package main import ( "encoding/json" "fmt" "tanex.com/library/dao/common" "tanex.com/library/dao/config" "tanex.com/library/dao/gorm" "tanex.com/library/dao/log" "tanex.com/library/dao/redis" "tanex.com/library/dao/sqlx" "tanex.com/library/dao/zookeeper" ) const ( MySqlList_1 = "/MySqlList_1" RedisPath = "/RedisPath" MySqlList_2 = "/MySqlList_2" ZaptestLog = "Zap_test_Log" ) var ( testCfg = &config.Config{} testReady = &GetReady{} testService = &Service{} ) type GetReady struct { zookeeperClient *zookeeper.ZookeeperMgr } func InitZookeeper(cfg *config.Config) *GetReady { return &GetReady{ zookeeperClient: zookeeper.InitZookeeper(cfg.ZkCfg), } } type Service struct { redisClient *redis.RedisSingleMgr logClient *log.LogMgr mysqlTest1Client *sqlx.SqlxMgr mysqlTest2Client *gorm.GormMgr } func InitService(cfg *config.Config) *Service { return &Service{ redisClient: redis.InitRedisSingle(cfg.RedisCfg), logClient: log.InitLog(cfg.LogCfg), mysqlTest1Client: sqlx.InitSqlx(cfg.MysqlCfg), mysqlTest2Client: gorm.InitGorm(ChangeDbName(cfg)), } } func ChangeDbName(cfg *config.Config) (cfg2 *config.MysqlCfg) { cfg2 = &config.MysqlCfg{ User: cfg.MysqlCfg.User, Password: cfg.MysqlCfg.Password, Addrs: cfg.MysqlCfg.Addrs, DBName: "test_2", } return cfg2 } func InitConfig(cfg []string) { //初始化Zk testCfg.ZkCfg = &config.ZkCfg{cfg} testReady = InitZookeeper(testCfg) //获取Zk上Redis配置 redisConfigByte, _, err := testReady.zookeeperClient.Get(RedisPath) if err != nil { common.SimplePanic("获取ZK上"+RedisPath+"的配置失败", "Error", err.Error(), "Path", RedisPath) } var redisConfigList []config.RedisCfg if err = json.Unmarshal(redisConfigByte, &redisConfigList); err != nil { common.SimplePanic("解析ZK上RedisCluster的数据失败", "Error", err.Error()) } //获取Zk上MySQL配置 mysqlConfigByte, _, err := testReady.zookeeperClient.Get(MySqlList_2) if err != nil { common.SimplePanic("获取ZK上"+MySqlList_2+"的配置失败", "Error", err.Error(), "Path", MySqlList_2) } var mysqlConfig []config.MysqlCfg if err = json.Unmarshal(mysqlConfigByte, &mysqlConfig); err != nil { common.SimplePanic("解析ZK上MysqlCfg的数据失败", "Error", err.Error()) } testCfg = &config.Config{ RedisCfg: &config.RedisCfg{Addr: fmt.Sprintf("%s:%d", redisConfigList[0].IP, redisConfigList[0].Port), Password: redisConfigList[0].Password}, LogCfg: &config.LogCfg{ProgramName: "Test", Debug: true}, MysqlCfg: &config.MysqlCfg{ Addrs: fmt.Sprintf("%s:%d", mysqlConfig[0].IP, mysqlConfig[0].Port), User: mysqlConfig[0].User, Password: mysqlConfig[0].Password, DBName: mysqlConfig[0].DBName, }, } testService = InitService(testCfg) fmt.Println("-------InitConfig-------") //初始化日记 testService.logClient.ZapCustom(ZaptestLog) //日志库在写的时候是异步的, 如果主进程过快结束可能没有东西打印 } ~~~