Merge branch 'master' of 10.10.14.250:huanghai/dsMin

master
wubin 5 years ago
commit 37486282bb

@ -10,5 +10,5 @@ sleep 3
cd /usr/local/dsMin/dsBaseRpc
chmod +x dsBaseRpc
# 运行为后台进程
nohup /usr/local/dsMin/dsBaseRpc/dsBaseRpc >> /usr/local/dsMin/dsBaseRpc/dsBaseRpc.log 2>&1 &
nohup /usr/local/dsMin/dsBaseRpc/dsBaseRpc >> /usr/local/dsMin/dsBaseRpc/Logs/dsBaseRpc.log 2>&1 &

@ -48,16 +48,12 @@ var (
)
func init() {
//判断是不是单元测试,如果是的话,那么配置文件的路径需要加上一个.
var configIniFile = "./Config/Config.ini"
//判断文件不是存在
if !CommonUtil.Exists(configIniFile) {
configIniFile = "." + configIniFile
}
if !CommonUtil.Exists(configIniFile) {
configIniFile = "/usr/local/dsMin/dsBaseRpc/Config/Config.ini"
}
iniParser := IniParser{}
if err := iniParser.Load(configIniFile); err != nil {
fmt.Printf("try load config file[%s] error[%s]\n", configIniFile, err.Error())
@ -99,7 +95,6 @@ func init() {
ProjectName = iniParser.GetString("project", "project_name")
//kafka地址
KafkaAddress = iniParser.GetString("kafka", "KafkaAddress")
//数据汇集中心地址
DataExchangeHost = iniParser.GetString("dataExchange", "host")
DataExchangeUrl = DataExchangeHost + iniParser.GetString("dataExchange", "exchangeUrl")

@ -58,7 +58,7 @@ func init() {
Engine.SetTableMapper(core.SnakeMapper{})
//显示+记录SQL日志
f, _ := os.Create("./Logs/sql.log")
f, _ := os.Create(ConfigUtil.DistributeRemotePath+"sql.log")
Engine.SetLogger(log.NewSimpleLogger(f))
Engine.ShowSQL(true) // 则会在控制台打印出生成的SQL语句

@ -2,6 +2,7 @@ package LogUtil
import (
"dsBaseRpc/Utils/CommonUtil"
"dsBaseRpc/Utils/ConfigUtil"
"fmt"
"log"
"os"
@ -14,17 +15,13 @@ import (
*/
func Error(code string, msg string) {
//判断是不是单元测试,如果是的话,那么配置文件的路径需要加上一个.
var logPath = "./Logs"
//判断文件不是存在(用于兼容单元测试不同的目录结构)
if !CommonUtil.Exists(logPath) {
logPath = "." + logPath
}
var logPath = ConfigUtil.DistributeRemotePath+"Logs"
if !CommonUtil.Exists(logPath) {
//创建
os.Mkdir(logPath, os.ModePerm)
os.MkdirAll(logPath, os.ModePerm)
}
//设置手工日志的办法
logFile, err := os.OpenFile(logPath+"/dsBaseRpc.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
logFile, err := os.OpenFile(logPath+"/dsBaseRpcManual.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
defer logFile.Close()
if err != nil {
fmt.Println("open file error !")

@ -13,9 +13,6 @@ func init(){
if !CommonUtil.Exists(configIniFile) {
configIniFile = "." + configIniFile
}
if !CommonUtil.Exists(configIniFile) {
configIniFile = "/usr/local/dsMin/dsBaseRpc/Config/pinyin.txt"
}
pinyin.LoadingPYFileName(configIniFile)
}

@ -5,29 +5,17 @@ import (
"dsBaseRpc/Utils/ConfigUtil"
"dsBaseRpc/Utils/LogUtil"
"github.com/go-redis/redis/v7"
redigoRedis "github.com/gomodule/redigo/redis"
"time"
)
var (
// 定义redis链接池
RedisClient *redis.Client
//这个是为了使用OAuth2类库引入的redis池也是没办法之举~
Pool *redigoRedis.Pool
)
func init() {
// 从配置文件获取redis的ip以及db
var redisHost = ConfigUtil.RedisIp + ":" + ConfigUtil.RedisPort
Pool = &redigoRedis.Pool{
Dial: func() (redigoRedis.Conn, error) {
conn, err := redigoRedis.Dial("tcp", redisHost)
if err != nil {
return nil, err
}
return conn, nil
},
}
//这个是以后项目中广泛使用的redis池
RedisClient = redis.NewClient(&redis.Options{
Addr: redisHost, // Redis地址

@ -9,6 +9,7 @@ import (
"dsBaseRpc/Utils/RedisUtil"
"encoding/json"
"fmt"
"github.com/go-redis/redis/v7"
"github.com/xormplus/builder"
"reflect"
"strconv"
@ -123,15 +124,23 @@ func batchWriteRedis(list []map[string]interface{}, m Selector) {
2020-02-05
*/
func batchReadRedis(ids []string, prefix string) ([]map[string]interface{}, []string) {
//1、创建管道
var list []*redis.StringCmd
pipe := RedisUtil.RedisClient.Pipeline()
for i := 0; i < len(ids); i++ {
list = append(list, pipe.Get(prefix+ids[i]))
}
//2、执行管道
pipe.Exec()
//存在于缓存中的数据
var existList []map[string]interface{}
//不存在的主键有哪些
var notExistsIds []string
//3、输出结果
for i := 0; i < len(ids); i++ {
r := RedisUtil.RedisClient.Exists(prefix + ids[i]).Val()
if r > 0 {
n := CommonUtil.ConvertJsonStringToMap(RedisUtil.RedisClient.Get(prefix + ids[i]).Val())
for i := 0; i < len(list); i++ {
_bean := list[i].Val()
if len(_bean) > 0 {
n := CommonUtil.ConvertJsonStringToMap(_bean)
//找到了加到返回值列表中去
existList = append(existList, n)
} else {
@ -152,7 +161,7 @@ func Count(baseSql string) (int32, error) {
baseSql = strings.ToLower(baseSql)
//截取去掉最后面的 limit ?
baseSql = strings.Split(baseSql, " limit ")[0]
countSql := "SELECT Count(1) as Count from (" + baseSql + ") as t100"
countSql := "select count(1) as count from (" + baseSql + ") as t100"
var count int32
_, err := db.SQL(countSql).Get(&count)
if err != nil {

@ -8,6 +8,7 @@ import (
"fmt"
"strings"
)
//操作数据库的变量
var db = DbUtil.Engine
@ -35,28 +36,33 @@ func ClearRpcRedis() {
var n int
//不能删除的前缀,统一认证在用的前缀
excludePrefix := []string{"oauth2:access", "TJoinApp"}
for {
var keys []string
var err error
keys, cursor, err = redisClient.Scan(cursor, "T*", 10).Result()
if err != nil {
//panic(err)
fmt.Println(err.Error())
}
if cursor == 0 {
break
}
for i := range keys {
var found = false
for j := range excludePrefix {
if strings.HasPrefix(keys[i], excludePrefix[j]) {
found = true
break
}
includePrefix := []string{"T*", "pk_*"}
//每一种需要清理的前缀
for k := range includePrefix {
// while true 一直在处理,直到没有了~
for {
var keys []string
var err error
keys, cursor, err = redisClient.Scan(cursor, includePrefix[k], 10).Result()
if err != nil {
fmt.Println(err.Error())
}
if cursor == 0 {
break
}
if !found {
redisClient.Del(keys[i])
n++
for i := range keys {
var found = false
for j := range excludePrefix {
if strings.HasPrefix(keys[i], excludePrefix[j]) {
found = true
break
}
}
if !found {
redisClient.Del(keys[i])
n++
}
}
}
}

@ -33,7 +33,6 @@ require (
github.com/gogo/protobuf v1.3.1 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.4.2
github.com/gomodule/redigo v1.8.2
github.com/google/uuid v1.1.1 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.14.4 // indirect
github.com/hashicorp/go-hclog v0.12.2 // indirect

@ -32,6 +32,7 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
"net"
"os"
)
// 配置步骤:
@ -42,11 +43,13 @@ func main() {
// 一、显示Logo
configIniFile := "./Config/logo.txt"
var logo = FileUtil.ReadFileContent(configIniFile)
if !CommonUtil.Exists(configIniFile) {
configIniFile = "/usr/local/dsMin/dsBaseWeb/Config/logo.txt"
}
fmt.Print(logo)
//创建日志文件目录
if !CommonUtil.Exists(ConfigUtil.DistributeRemotePath + "Logs") {
os.MkdirAll(ConfigUtil.DistributeRemotePath+"Logs", os.ModePerm)
}
//添加定时清理垃圾的代码
c := cron.New(cron.WithSeconds())
c.AddFunc("0 0 1 * * *", func() {
@ -57,6 +60,15 @@ func main() {
sql = "truncate table t_base_student_import_excel"
db.SQL(sql).Execute()
//清空自动日志
var logPath = ConfigUtil.DistributeRemotePath + "Logs"
file := logPath + "/dsBaseRpc.log"
if CommonUtil.Exists(file) {
f, _ := os.OpenFile(file, os.O_WRONLY|os.O_TRUNC, 0600)
defer f.Close()
f.WriteString("清空成功!")
}
})
c.Start()
defer c.Stop()

Loading…
Cancel
Save