From 45fa3e2a34786afcf9c00f8420dc2b1bb1eac33c Mon Sep 17 00:00:00 2001 From: dengbiao Date: Thu, 5 Dec 2024 11:46:13 +0800 Subject: [PATCH] update --- app/cfg/cfg_app.go | 26 ++++++++++++++++++-------- app/cfg/init_cfg.go | 12 ++++++++++++ app/cfg/init_es.go | 12 ++++++++++++ app/db/db.go | 39 ++++++++++++++++++++++++++++++++++++++- etc/cfg.yml | 27 +++++++++++++++++++++++++-- go.mod | 2 +- main.go | 6 +++++- 7 files changed, 111 insertions(+), 13 deletions(-) create mode 100644 app/cfg/init_es.go diff --git a/app/cfg/cfg_app.go b/app/cfg/cfg_app.go index 788045e..8a04331 100644 --- a/app/cfg/cfg_app.go +++ b/app/cfg/cfg_app.go @@ -5,14 +5,18 @@ import ( ) type Config struct { - CurlDebug bool `yaml:"curldebug"` - Debug bool `yaml:"debug"` - Prd bool `yaml:"prd"` - RedisAddr string `yaml:"redis_addr"` - RedisPassword string `yaml:"redis_password"` - DB DBCfg `yaml:"db"` - MQ MQCfg `yaml:"mq"` - Log LogCfg `yaml:"log"` + CurlDebug bool `yaml:"curldebug"` + Debug bool `yaml:"debug"` + Prd bool `yaml:"prd"` + RedisAddr string `yaml:"redis_addr"` + RedisPassword string `yaml:"redis_password"` + DB DBCfg `yaml:"db"` + ImDB DBCfg `yaml:"im_db"` + MQ MQCfg `yaml:"mq"` + Log LogCfg `yaml:"log"` + ImBusinessRpc ImBusinessRpcCfg `yaml:"im_business_rpc"` + ImLogicRpc ImLogicRpcCfg `yaml:"im_logic_rpc"` + ES ESCfg `yaml:"es"` } // 数据库配置结构体 @@ -49,3 +53,9 @@ type MQCfg struct { User string `yaml:"user"` Pwd string `yaml:"pwd"` } + +type ESCfg struct { + Url string `yaml:"url"` + User string `yaml:"user"` + Pwd string `yaml:"pwd"` +} diff --git a/app/cfg/init_cfg.go b/app/cfg/init_cfg.go index 8184c0e..bdddb32 100644 --- a/app/cfg/init_cfg.go +++ b/app/cfg/init_cfg.go @@ -15,9 +15,12 @@ var ( RedisAddr string RedisPassword string DB *DBCfg + IMDB *DBCfg MQ *MQCfg + ES *ESCfg Log *LogCfg ImLogicRpc *ImLogicRpcCfg + ImBusinessRpc *ImBusinessRpcCfg ) // 初始化配置文件,将cfg.yml读入到内存 @@ -44,13 +47,22 @@ func InitCfg() { Debug = conf.Debug CurlDebug = conf.CurlDebug DB = &conf.DB + IMDB = &conf.ImDB Log = &conf.Log RedisAddr = conf.RedisAddr MQ = &conf.MQ RedisPassword = conf.RedisPassword + ES = &conf.ES + ImLogicRpc = &conf.ImLogicRpc + ImBusinessRpc = &conf.ImBusinessRpc } type ImLogicRpcCfg struct { URL string `yaml:"url"` PORT string `yaml:"port"` } + +type ImBusinessRpcCfg struct { + URL string `yaml:"url"` + PORT string `yaml:"port"` +} diff --git a/app/cfg/init_es.go b/app/cfg/init_es.go new file mode 100644 index 0000000..6df6371 --- /dev/null +++ b/app/cfg/init_es.go @@ -0,0 +1,12 @@ +package cfg + +import ( + "code.fnuoos.com/go_rely_warehouse/zyos_go_es.git/es" + "encoding/json" +) + +func InitEs() { + data, _ := json.Marshal(ES) + filePutContents("init_rabbit_es", string(data)) + es.Init(ES.Url, ES.User, ES.Pwd) +} diff --git a/app/db/db.go b/app/db/db.go index 00630e8..223445e 100644 --- a/app/db/db.go +++ b/app/db/db.go @@ -16,7 +16,7 @@ import ( var Db *xorm.Engine -//根据DB配置文件初始化数据库 +// 根据DB配置文件初始化数据库 func InitDB(c *cfg.DBCfg) error { var ( err error @@ -51,6 +51,43 @@ func InitDB(c *cfg.DBCfg) error { return nil } +var DbIm *xorm.Engine + +// 根据DB配置文件初始化数据库 +func InitImDB(c *cfg.DBCfg) error { + var ( + err error + f *os.File + ) + //创建Orm引擎 + if DbIm, err = xorm.NewEngine("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4", c.User, c.Psw, c.Host, c.Name)); err != nil { + return err + } + DbIm.SetConnMaxLifetime(c.MaxLifetime * time.Second) //设置最长连接时间 + DbIm.SetMaxOpenConns(c.MaxOpenConns) //设置最大打开连接数 + DbIm.SetMaxIdleConns(c.MaxIdleConns) //设置连接池的空闲数大小 + if err = DbIm.Ping(); err != nil { //尝试ping数据库 + return err + } + if c.ShowLog { //根据配置文件设置日志 + DbIm.ShowSQL(true) //设置是否打印sql + DbIm.Logger().SetLevel(0) //设置日志等级 + //修改日志文件存放路径文件名是%s.log + path := fmt.Sprintf(c.Path, c.Name) + f, err = os.OpenFile(path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0777) + if err != nil { + os.RemoveAll(c.Path) + if f, err = os.OpenFile(c.Path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0777); err != nil { + return err + } + } + logger := log.NewSimpleLogger(f) + logger.ShowSQL(true) + DbIm.SetLogger(logger) + } + return nil +} + /********************************************* 公用方法 *********************************************/ // 数据批量插入 diff --git a/etc/cfg.yml b/etc/cfg.yml index 83b563a..3e89860 100644 --- a/etc/cfg.yml +++ b/etc/cfg.yml @@ -8,7 +8,6 @@ local: true redis_addr: '127.0.0.1:6379' redis_password: '' - db: host: '119.23.182.117:3306' name: 'egg' @@ -20,6 +19,17 @@ db: max_idle_conns: 100 path: 'tmp/%s.log' +im_db: + host: '119.23.182.117:3306' + name: 'egg-im' + user: 'root' + psw: 'Fnuo123com@' + show_log: true + max_lifetime: 30 + max_open_conns: 100 + max_idle_conns: 100 + path: 'tmp/%s.log' + # 日志 log: app_name: 'applet' @@ -38,4 +48,17 @@ mq: host: '116.62.62.35' port: '5672' user: 'zhios' - pwd: 'ZHIoscnfnuo123' \ No newline at end of file + pwd: 'ZHIoscnfnuo123' + +es: + url: 'http://120.55.48.175:9200' + user: 'elastic' + pwd: 'fnuo123' + +im_business_rpc: + url: im-rpc-business.izhim.com + port: 1005 + +im_logic_rpc: + url: im-rpc-logic.izhim.com + port: 1008 \ No newline at end of file diff --git a/go.mod b/go.mod index 95ee4d5..9ab5b53 100644 --- a/go.mod +++ b/go.mod @@ -28,7 +28,7 @@ require ( github.com/syyongx/php2go v0.9.8 github.com/wechatpay-apiv3/wechatpay-go v0.2.20 go.uber.org/zap v1.16.0 - google.golang.org/grpc v1.31.0 + google.golang.org/grpc v1.32.0 google.golang.org/protobuf v1.33.0 gopkg.in/natefinch/lumberjack.v2 v2.2.1 gopkg.in/yaml.v2 v2.4.0 diff --git a/main.go b/main.go index 34c6ca7..e9b2bda 100644 --- a/main.go +++ b/main.go @@ -2,10 +2,10 @@ package main import ( "applet/app/cfg" + "applet/app/db" "applet/app/utils" "applet/app/utils/logx" "applet/consume" - db "code.fnuoos.com/EggPlanet/egg_models.git/src" "fmt" "os" "os/signal" @@ -18,10 +18,14 @@ func init() { cfg.InitLog() //日志初始化 cfg.InitCache() //缓存初始化 cfg.InitMq() //队列初始化 + cfg.InitEs() //ElasticSearch初始化 if cfg.Debug { //判断是否是debug if err := db.InitDB(cfg.DB); err != nil { //主数据库初始化 panic(err) } + if err := db.InitImDB(cfg.IMDB); err != nil { // IM主数据库初始化 + panic(err) + } } fmt.Println("init success") }