dengbiao преди 2 седмици
родител
ревизия
45fa3e2a34
променени са 7 файла, в които са добавени 111 реда и са изтрити 13 реда
  1. +18
    -8
      app/cfg/cfg_app.go
  2. +12
    -0
      app/cfg/init_cfg.go
  3. +12
    -0
      app/cfg/init_es.go
  4. +38
    -1
      app/db/db.go
  5. +25
    -2
      etc/cfg.yml
  6. +1
    -1
      go.mod
  7. +5
    -1
      main.go

+ 18
- 8
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"`
}

+ 12
- 0
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"`
}

+ 12
- 0
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)
}

+ 38
- 1
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
}

/********************************************* 公用方法 *********************************************/

// 数据批量插入


+ 25
- 2
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'
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

+ 1
- 1
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


+ 5
- 1
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")
}


Зареждане…
Отказ
Запис