Commit 339e1d59 by zhengqiuyun86

初始化

parent ad9ce240
......@@ -16,7 +16,7 @@ type Config struct {
//EsServerHttp
EsServerHttp string
//mysql
//mysqlrym
MysqlConf MysqlConf
}
......@@ -41,17 +41,17 @@ var Env = ""
//log conf
var LogLevel = "DEBUG"
var LogColorful = false //日志是否开启彩色打印
var LogColorful = true //日志是否开启彩色打印
var LogResponseLength = 10
var LogShowSql = true
//Redis conf
var RedisAddr = "to-chengdu-office.168cad.top:50001"
var RedisPwd = "testPassevnsoo567"
var RedisAddr = ""
var RedisPwd = ""
var RedisDb = 0
//ES conf
var EsServerHttp = "http://192.168.1.124:9200"
var EsServerHttp = ""
//Mysql conf
type MysqlConf struct {
......
......@@ -4,13 +4,16 @@ import (
"context"
"fmt"
"git.168cad.top/zhengqiuyun/rym-util/conf"
"git.168cad.top/zhengqiuyun/rym-util/exception"
"git.168cad.top/zhengqiuyun/rym-util/log"
"github.com/olivere/elastic/v7"
_log "log"
"os"
)
func TestLog() {
log.Info("Es 测试")
}
var Client *elastic.Client
//初始化
......@@ -20,12 +23,13 @@ func init() {
errorLog := _log.New(os.Stdout, "APP", _log.LstdFlags)
Client, err = elastic.NewClient(elastic.SetErrorLog(errorLog), elastic.SetURL(host))
if err != nil {
exception.ThrowsErrS(fmt.Sprintf("ES连接失败:%s", err.Error()))
}
log.Error(fmt.Sprintf("Elasticsearch connect error:%s", err.Error()))
} else {
info, code, err := Client.Ping(host).Do(context.Background())
if err != nil {
panic(err)
}
log.Info(fmt.Sprintf("Elasticsearch connect %d", code))
log.Info(fmt.Sprintf("Elasticsearch version %s", info.Version.Number))
}
}
......@@ -6,7 +6,7 @@ import (
"git.168cad.top/zhengqiuyun/rym-util/conf"
"git.168cad.top/zhengqiuyun/rym-util/es"
"git.168cad.top/zhengqiuyun/rym-util/log"
db "git.168cad.top/zhengqiuyun/rym-util/mysql"
db "git.168cad.top/zhengqiuyun/rym-util/mysqlrym"
"git.168cad.top/zhengqiuyun/rym-util/redis"
"git.168cad.top/zhengqiuyun/rym-util/util"
"github.com/gin-gonic/gin"
......
......@@ -3,9 +3,39 @@ module git.168cad.top/zhengqiuyun/rym-util
go 1.17
require (
github.com/gin-gonic/gin v1.7.4
github.com/garyburd/redigo v1.6.3
github.com/gin-gonic/gin v1.8.1
github.com/go-playground/validator/v10 v10.11.0
github.com/go-redis/redis v6.15.9+incompatible
github.com/olivere/elastic/v7 v7.0.32
gorm.io/driver/mysql v1.2.2
gorm.io/gorm v1.22.4
)
require (
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/goccy/go-json v0.9.7 // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.3 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/onsi/ginkgo v1.16.5 // indirect
github.com/onsi/gomega v1.20.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect
golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4 // indirect
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
package main
import (
"git.168cad.top/zhengqiuyun/rym-util/es"
"git.168cad.top/zhengqiuyun/rym-util/mysqlrym"
"git.168cad.top/zhengqiuyun/rym-util/redis"
)
func main() {
mysqlrym.TestLog()
es.TestLog()
redis.TestLog()
}
package db
package mysqlrym
import (
"fmt"
"git.168cad.top/zhengqiuyun/rym-util/conf"
"git.168cad.top/zhengqiuyun/rym-util/exception"
"git.168cad.top/zhengqiuyun/rym-util/log"
"git.168cad.top/zhengqiuyun/rym-util/util"
"gorm.io/driver/mysql"
......@@ -17,6 +16,10 @@ import (
_log "log"
)
func TestLog() {
log.Info("Mysql 测试")
}
var customerDb *gorm.DB
var customerDbMap sync.Map
......@@ -28,7 +31,7 @@ func SetTx() {
gidNeedTX.Store(gid, 1)
}
func GetPgDb() *gorm.DB {
func GetDb() *gorm.DB {
return getDb(customerDb, &customerDbMap)
}
......@@ -109,7 +112,8 @@ func init() {
Logger: newLogger,
})
if err != nil {
exception.ThrowsErr(err)
}
log.Error(fmt.Sprintf("%s connect error:%s", "Mysql server", err.Error()))
} else {
log.Info(fmt.Sprintf("%s connect success", "Mysql server"))
}
}
package redis
import (
"fmt"
"git.168cad.top/zhengqiuyun/rym-util/conf"
"git.168cad.top/zhengqiuyun/rym-util/const/YorN"
"git.168cad.top/zhengqiuyun/rym-util/log"
"github.com/garyburd/redigo/redis"
)
var pool *redis.Pool //创建redis连接池
func init() {
pool = &redis.Pool{ //实例化一个连接池
MaxIdle: 10, //最初的连接数量
MaxActive: 0, //连接池最大连接数量,不确定可以用0(0表示自动定义),按需分配
IdleTimeout: 300, //连接关闭时间 300秒 (300秒不使用自动关闭)
Dial: func() (redis.Conn, error) { //要连接的redis数据库
c, err := redis.Dial("tcp", conf.RedisAddr)
if err != nil {
return nil, err
}
if _, err := c.Do("AUTH", conf.RedisPwd); err != nil {
c.Close()
return nil, err
}
if _, err := c.Do("SELECT", conf.RedisDb); err != nil {
c.Close()
return nil, err
}
return c, nil
},
}
}
func Incr(key string) int64 {
c := pool.Get() //从连接池,取一个链接
defer c.Close() //函数运行结束 ,把连接放回连接池
r, err := redis.Int64(c.Do("INCR", key))
if err != nil {
log.Error(err.Error())
}
return r
}
func IncrBy(key string, n int) int64 {
c := pool.Get() //从连接池,取一个链接
defer c.Close() //函数运行结束 ,把连接放回连接池
r, err := redis.Int64(c.Do("INCRBY", key, n))
if err != nil {
log.Error(err.Error())
}
return r
}
func DecrBy(key string, n int) int64 {
c := pool.Get() //从连接池,取一个链接
defer c.Close() //函数运行结束 ,把连接放回连接池
r, err := redis.Int64(c.Do("DECRBY", key, n))
if err != nil {
log.Error(err.Error())
}
return r
}
func Exists(key string) bool {
c := pool.Get() //从连接池,取一个链接
defer c.Close() //函数运行结束 ,把连接放回连接池
r, err := redis.Int64(c.Do("EXISTS", key))
if err != nil {
return false
}
return r > 0
}
//SwitchOpen 在redis中存0或1,1代表OK
func SwitchOpen(key string) bool {
r := GetString(key)
log.Debug(fmt.Sprintf("缓存[%s]数据为:%s", key, r))
return r == YorN.Y
}
func Get(key string) []byte {
c := pool.Get() //从连接池,取一个链接
defer c.Close() //函数运行结束 ,把连接放回连接池
r, err := redis.Bytes(c.Do("GET", key))
if err != nil {
return nil
}
return r
}
func GetString(key string) string {
c := pool.Get() //从连接池,取一个链接
defer c.Close() //函数运行结束 ,把连接放回连接池
r, err := redis.String(c.Do("GET", key))
if err != nil {
return ""
}
return r
}
func Set(k string, v string) {
c := pool.Get() //从连接池,取一个链接
defer c.Close() //函数运行结束 ,把连接放回连接池
r, err := c.Do("SET", k, v)
if err != nil {
log.Error(err.Error())
}
log.Debug(fmt.Sprintf("设置缓存成功:%s", r))
}
func Expire(k string, t int) {
c := pool.Get() //从连接池,取一个链接
defer c.Close()
r, err := c.Do("EXISTS", k, t)
if err != nil {
log.Error(err.Error())
}
log.Debug(fmt.Sprintf("设置缓存过期成功:%d", r))
}
package redis
import (
"fmt"
"git.168cad.top/zhengqiuyun/rym-util/conf"
"git.168cad.top/zhengqiuyun/rym-util/const/YorN"
"git.168cad.top/zhengqiuyun/rym-util/log"
"github.com/go-redis/redis"
"time"
)
func TestLog() {
log.Info("Redis 测试")
}
var redisClient *redis.Client
func init() {
redisClient = redis.NewClient(&redis.Options{
Addr: conf.RedisAddr,
Password: conf.RedisPwd,
DB: conf.RedisDb, // redis一共16个库,指定其中一个库即可
})
_, err := redisClient.Ping().Result()
if err != nil {
log.Error(fmt.Sprintf("Redis connect error :%s", err.Error()))
}
return
}
func Incr(key string) int64 {
r := redisClient.Incr(key)
return r.Val()
}
func IncrBy(key string, n int64) int64 {
r := redisClient.IncrBy(key, n)
return r.Val()
}
func DecrBy(key string, n int64) int64 {
r := redisClient.DecrBy(key, n)
return r.Val()
}
func Exists(key string) bool {
r := redisClient.Exists(key)
return r.Val() > 0
}
//SwitchOpen 在redis中存0或1,1代表OK
func SwitchOpen(key string) bool {
r := GetString(key)
log.Debug(fmt.Sprintf("缓存[%s]数据为:%s", key, r))
return r == YorN.Y
}
func GetString(key string) string {
r := redisClient.Get(key)
return r.Val()
}
func Set(k string, v string) {
redisClient.Set(k, v, 0)
log.Debug(fmt.Sprintf("设置缓存成功:%s %s", k, v))
}
func Expire(k string, t int64) {
redisClient.Expire(k, time.Duration(t))
log.Debug(fmt.Sprintf("设置缓存过期成功:%s %d", k, t))
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment