Commit 3d0f553e by sunlongv520

init

parents
/config/config.toml
/.idea
/.DS_Store
/logs/*
/config/*.toml
{"interface_type":"1","access_url":"api.ichun.com/v3/login","err_msg":"响应超时","err_code":"40004","uid":"16678","user_name":"张三","user_ip":"153.2.15.1","create_time":1581582711,"create_time_str":"2020-02-14 16:40"}
{"interface_type":"1","access_url":"api.ichun.com/v3/login","err_msg":"响应超时","err_code":"40004","uid":"16678","user_name":"张三","user_ip":"153.2.15.1","create_time":1581582711,"create_time_str":"2020-02-14 16:40"}
{"interface_type":"1","access_url":"api.ichun.com/v3/login","err_msg":"响应超时","err_code":"40004","uid":"16678","user_name":"张三","user_ip":"153.2.15.1","create_time":1581582711,"create_time_str":"2020-02-14 16:40"}
{"interface_type":"1","access_url":"api.ichun.com/v3/login","err_msg":"响应超时","err_code":"40004","uid":"16678","user_name":"张三","user_ip":"153.2.15.1","create_time":1581582711,"create_time_str":"2020-02-14 16:40"}
{"interface_type":"1","access_url":"api.ichun.com/v3/login","err_msg":"响应超时","err_code":"40004","uid":"16678","user_name":"张三","user_ip":"153.2.15.1","remark":"a=1\u0026b=2\u0026c=3","create_time":1581582711,"create_time_str":"2020-02-14 16:40"}
{"interface_type":"1","access_url":"api.ichun.com/v3/login","err_msg":"响应超时","err_code":"40004","uid":"16678","user_name":"张三","user_ip":"153.2.15.1","remark":"a=1\u0026b=2\u0026c=3","create_time":1581582711,"create_time_str":"2020-02-14 16:40"}
go run .\service\behavior\main.go -configDir=./config/ -logDir=./log/ -LogReportDir=./LogReport/
{
"interface_type":"1",
"access_url":"api.ichun.com/v3/login",
"err_msg":"响应超时",
"err_code":"40004",
"uid":"16678",
"user_name":"张三",
"user_ip":"153.2.15.1",
"remark":"a=1&b=2&c=3",
"create_time":1581582711,
"create_time_str":"2020-02-14 16:40"
}
\ No newline at end of file
[Crm_domain]
send_mail="http://crm.liexin.net/api/sendCaiGouEmailByOrderAdmin"
[Ding_msg]
webhook="https://oapi.dingtalk.com/robot/send?access_token=ec03b2cba0d62ef0bd7b9ebb16aaf69b5f626658dfb6b194045381324b0786cf"
jingDiao="https://oapi.dingtalk.com/robot/send?access_token=eaeae1253e9c125a053bc8a7bcfec5096a23625a98044eaa346591863449ba17"
\ No newline at end of file
[Liexin_databases]
dns="liexin:liexin#zsyM@tcp(192.168.2.232:3306)/liexin?parseTime=true"
[rabbitmq_ichunt]
queue_name="send_buyer_mail"
routing_key="send_buyer_mail"
exchange="ichunt_order_msg"
type="direct"
dns="amqp://guest:guest@192.168.2.232:5672/"
package db
import (
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
)
var (
DB *sqlx.DB
)
func Init(dns string) error {
var err error
DB, err = sqlx.Open("mysql", dns)
if err != nil {
return err
}
err = DB.Ping()
if err != nil {
return err
}
DB.SetMaxOpenConns(100)
DB.SetMaxIdleConns(16)
return nil
}
package db
import "fmt"
type Order struct {
Order_id int `db:"order_id"`
}
type OrderItems struct {
GoodsName string `db:"goods_name"`
}
type OrderList struct {
Order
OrderItems
BrandName string `db:"brand_name"`
}
func Findabc(order_id int) (data *OrderList,err error){
data = &OrderList{}
// sql := `select a.order_id,b.goods_name,b.brand_name from lie_order as a left join
//lie_order_items as b on a.order_id = b.order_id
//where a.create_time >= 1550115738`
sql := `select a.order_id,b.goods_name,b.brand_name from lie_order as a left join
lie_order_items as b on a.order_id = b.order_id
where a.order_id = ?`
err = DB.Get(data,sql,order_id)
if err != nil{
fmt.Println(err)
}
fmt.Println("...............")
fmt.Printf("%+v",data)
fmt.Println("...............")
//for i ,v := range data{
// fmt.Println(i)
// fmt.Println(v)
//}
//DB.Close()
return
}
package OrderActionLog
import (
"database/sql"
"fmt"
"github.com/ichunt2019/logger"
"go-api-behavior/dal/db"
"go-api-behavior/util"
"log"
"time"
)
type ActionLog struct {
LogId int `db:"log_id"`
OrderId int `db:"order_id"`
OperatorId int `db:"operator_id"`
OperatorType int `db:"operator_type"`
Event string `db:"event"`
Ip string `db:"ip"`
CreateTime int `db:"create_time"`
}
func initDb(dns string) (err error) {
err = db.Init(dns)
if err != nil {
return
}
return
}
func AddLog() (err error) {
initDb(util.Configs.Liexin_databases.Dns) //初始化db
// QueryRow()
// Query()
Insert(3340, 1000, 2, "go test")
return
}
// 单行查询
func QueryRow() {
var actionLog ActionLog
err1 := db.DB.Get(&actionLog, "select * from lie_order_action_log where order_id = 3340")
if err1 == sql.ErrNoRows {
log.Printf("not found data of the id:%d", 1)
}
if err1 != nil {
panic(err1)
}
fmt.Printf("actionLog: %v\n", actionLog)
}
// 多行查询
func Query() {
var actionLog2 []*ActionLog
err2 := db.DB.Select(&actionLog2, "select * from lie_order_action_log where order_id = 3340")
if err2 == sql.ErrNoRows {
log.Printf("not found data of the id:%d", 1)
}
if err2 != nil {
panic(err2)
}
for _, v := range actionLog2 {
fmt.Println(v)
}
}
func Insert(order_id int, operator_id int, operator_type int, event string) (err error) {
create_time := time.Now().Unix()
_, err1 := db.DB.Exec("insert into lie_order_action_log (order_id, operator_id, operator_type, event, create_time) values (?, ?, ?, ?, ?)", order_id, operator_id, operator_type, event, create_time)
if err1 != nil {
logger.Fatal("数据库操作失败")
return err1
}
//id, err := result.LastInsertId()
//if err != nil {
// panic(err)
//}
//
//affected, err := result.RowsAffected()
//if err != nil {
// panic(err)
//}
//
//fmt.Printf("last insert id:%d affect rows:%d\n", id, affected)
return err
}
\ No newline at end of file
module go-api-behavior
go 1.12
require (
github.com/BurntSushi/toml v0.3.1
github.com/go-sql-driver/mysql v1.5.0
github.com/ichunt2019/go-msgserver v1.0.5 // indirect
github.com/ichunt2019/logger v1.0.5
github.com/jmoiron/sqlx v1.2.0
)
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/ichunt2019/go-msgserver v1.0.5 h1:yanQ95Ld0etJzVhyZql0jhXXE18qGFX48qFgTCim5hg=
github.com/ichunt2019/go-msgserver v1.0.5/go.mod h1:fWAvbry0W9nhmkqgT2agwRqYCWlguUJXgy2rgoWOUmA=
github.com/ichunt2019/logger v1.0.5 h1:85C6kJCH9xlbLt1VmwHp/8iScm+bIlenK6nanWwwq/o=
github.com/ichunt2019/logger v1.0.5/go.mod h1:5IWMrrqJIWwOIGav9ACWOI+KOuYeteUvOei4zubclwg=
github.com/jmoiron/sqlx v1.2.0 h1:41Ip0zITnmWNR/vHV+S4m+VoUivnWY5E4OJfLZjCJMA=
github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/streadway/amqp v0.0.0-20190827072141-edfb9018d271 h1:WhxRHzgeVGETMlmVfqhRn8RIeeNoPr2Czh33I4Zdccw=
github.com/streadway/amqp v0.0.0-20190827072141-edfb9018d271/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
2020-02-13 17:33:18.762 INFO (main.go:main.(*RecvPro).Consumer:25) {
"interface_type":"1",
"access_url":"api.ichun.com/v3/login",
"err_msg":"响应超时",
"err_code":"40004",
"uid":"16678",
"user_name":"张三",
"user_ip":"153.2.15.1",
"create_time":1581582711
}
2020-02-13 17:33:27.427 INFO (main.go:main.(*RecvPro).Consumer:25) {
"interface_type":"1",
"access_url":"api.ichun.com/v3/login",
"err_msg":"响应超时",
"err_code":"40004",
"uid":"16678",
"user_name":"张三",
"user_ip":"153.2.15.1",
"create_time":1581582711
}
2020-02-13 17:34:16.595 INFO (main.go:main.(*RecvPro).Consumer:25) {
"interface_type":"1",
"access_url":"api.ichun.com/v3/login",
"err_msg":"响应超时",
"err_code":"40004",
"uid":"16678",
"user_name":"张三",
"user_ip":"153.2.15.1",
"create_time":1581582711
}
2020-02-13 17:35:10.521 INFO (main.go:main.(*RecvPro).Consumer:25) {
"interface_type":"1",
"access_url":"api.ichun.com/v3/login",
"err_msg":"响应超时",
"err_code":"40004",
"uid":"16678",
"user_name":"张三",
"user_ip":"153.2.15.1",
"create_time":1581582711
}
2020-02-13 17:36:10.066 INFO (main.go:main.(*RecvPro).Consumer:25) {
"interface_type":"1",
"access_url":"api.ichun.com/v3/login",
"err_msg":"响应超时",
"err_code":"40004",
"uid":"16678",
"user_name":"张三",
"user_ip":"153.2.15.1",
"create_time":1581582711
}
2020-02-13 17:36:52.347 INFO (main.go:main.(*RecvPro).Consumer:25) {
"interface_type":"1",
"access_url":"api.ichun.com/v3/login",
"err_msg":"响应超时",
"err_code":"40004",
"uid":"16678",
"user_name":"张三",
"user_ip":"153.2.15.1",
"create_time":1581582711
}
2020-02-13 17:36:57.067 INFO (main.go:main.(*RecvPro).Consumer:25) {
"interface_type":"1",
"access_url":"api.ichun.com/v3/login",
"err_msg":"响应超时",
"err_code":"40004",
"uid":"16678",
"user_name":"张三",
"user_ip":"153.2.15.1",
"create_time":1581582711
}
2020-02-13 17:37:43.885 INFO (main.go:main.(*RecvPro).Consumer:25) {
"interface_type":"1",
"access_url":"api.ichun.com/v3/login",
"err_msg":"响应超时",
"err_code":"40004",
"uid":"16678",
"user_name":"张三",
"user_ip":"153.2.15.1",
"create_time":1581582711
}
2020-02-13 20:32:01.406 INFO (main.go:main.(*RecvPro).Consumer:25) {
"interface_type":"1",
"access_url":"api.ichun.com/v3/login",
"err_msg":"响应超时",
"err_code":"40004",
"uid":"16678",
"user_name":"张三",
"user_ip":"153.2.15.1",
"create_time":1581582711
}
2020-02-13 20:32:35.107 INFO (main.go:main.(*RecvPro).Consumer:25) {
"interface_type":"1",
"access_url":"api.ichun.com/v3/login",
"err_msg":"响应超时",
"err_code":"40004",
"uid":"16678",
"user_name":"张三",
"user_ip":"153.2.15.1",
"create_time":1581582711
}
2020-02-13 20:32:39.176 INFO (main.go:main.(*RecvPro).Consumer:25) {
"interface_type":"1",
"access_url":"api.ichun.com/v3/login",
"err_msg":"响应超时",
"err_code":"40004",
"uid":"16678",
"user_name":"张三",
"user_ip":"153.2.15.1",
"create_time":1581582711
}
2020-02-13 20:32:39.824 INFO (main.go:main.(*RecvPro).Consumer:25) {
"interface_type":"1",
"access_url":"api.ichun.com/v3/login",
"err_msg":"响应超时",
"err_code":"40004",
"uid":"16678",
"user_name":"张三",
"user_ip":"153.2.15.1",
"create_time":1581582711
}
2020-02-13 20:32:40.447 INFO (main.go:main.(*RecvPro).Consumer:25) {
"interface_type":"1",
"access_url":"api.ichun.com/v3/login",
"err_msg":"响应超时",
"err_code":"40004",
"uid":"16678",
"user_name":"张三",
"user_ip":"153.2.15.1",
"create_time":1581582711
}
2020-02-13 20:32:40.936 INFO (main.go:main.(*RecvPro).Consumer:25) {
"interface_type":"1",
"access_url":"api.ichun.com/v3/login",
"err_msg":"响应超时",
"err_code":"40004",
"uid":"16678",
"user_name":"张三",
"user_ip":"153.2.15.1",
"create_time":1581582711
}
2020-02-13 20:32:41.408 INFO (main.go:main.(*RecvPro).Consumer:25) {
"interface_type":"1",
"access_url":"api.ichun.com/v3/login",
"err_msg":"响应超时",
"err_code":"40004",
"uid":"16678",
"user_name":"张三",
"user_ip":"153.2.15.1",
"create_time":1581582711
}
2020-02-13 20:32:45.43 INFO (main.go:main.(*RecvPro).Consumer:25) {
"interface_type":"1",
"access_url":"api.ichun.com/v3/login",
"err_msg":"响应超时",
"err_code":"40004",
"uid":"16678",
"user_name":"张三",
"user_ip":"153.2.15.1",
"create_time":1581582711
}
2020-02-13 20:32:46.037 INFO (main.go:main.(*RecvPro).Consumer:25) {
"interface_type":"1",
"access_url":"api.ichun.com/v3/login",
"err_msg":"响应超时",
"err_code":"40004",
"uid":"16678",
"user_name":"张三",
"user_ip":"153.2.15.1",
"create_time":1581582711
}
2020-02-13 20:32:46.869 INFO (main.go:main.(*RecvPro).Consumer:25) {
"interface_type":"1",
"access_url":"api.ichun.com/v3/login",
"err_msg":"响应超时",
"err_code":"40004",
"uid":"16678",
"user_name":"张三",
"user_ip":"153.2.15.1",
"create_time":1581582711
}
2020-02-13 20:32:51.22 INFO (main.go:main.(*RecvPro).Consumer:25) {
"interface_type":"1",
"access_url":"api.ichun.com/v3/login",
"err_msg":"响应超时",
"err_code":"40004",
"uid":"16678",
"user_name":"张三",
"user_ip":"153.2.15.1",
"create_time":1581582711
}
2020-02-13 20:32:51.8 INFO (main.go:main.(*RecvPro).Consumer:25) {
"interface_type":"1",
"access_url":"api.ichun.com/v3/login",
"err_msg":"响应超时",
"err_code":"40004",
"uid":"16678",
"user_name":"张三",
"user_ip":"153.2.15.1",
"create_time":1581582711
}
2020-02-13 20:32:55.066 INFO (main.go:main.(*RecvPro).Consumer:25) {
"interface_type":"1",
"access_url":"api.ichun.com/v3/login",
"err_msg":"响应超时",
"err_code":"40004",
"uid":"16678",
"user_name":"张三",
"user_ip":"153.2.15.1",
"create_time":1581582711
}
2020-02-13 20:32:55.604 INFO (main.go:main.(*RecvPro).Consumer:25) {
"interface_type":"1",
"access_url":"api.ichun.com/v3/login",
"err_msg":"响应超时",
"err_code":"40004",
"uid":"16678",
"user_name":"张三",
"user_ip":"153.2.15.1",
"create_time":1581582711
}
2020-02-13 20:32:56.131 INFO (main.go:main.(*RecvPro).Consumer:25) {
"interface_type":"1",
"access_url":"api.ichun.com/v3/login",
"err_msg":"响应超时",
"err_code":"40004",
"uid":"16678",
"user_name":"张三",
"user_ip":"153.2.15.1",
"create_time":1581582711
}
2020-02-13 16:37:58.629 INFO (main.go:main.(*RecvPro).Consumer:24) {
"interface_type":"登录",
"access_url":"api.ichun.com/v3/login",
"err_msg":"响应超时",
"err_code":"40004",
"uid":"16678",
"user_name":"张三",
"user_ip":"153.2.15.1",
"create_time":1581582711
}
2020-02-14 14:04:34.084 ERROR (main.go:main.(*RecvPro).FailAction:33) 任务处理失败了,我要进入db日志库了
2020-02-14 14:04:34.084 ERROR (main.go:main.(*RecvPro).FailAction:34) 任务处理失败了,发送钉钉消息通知主人
2020-02-14 14:04:34.084 ERROR (main.go:main.(*RecvPro).FailAction:35)
2020-02-14 16:43:31.925 ERROR (main.go:main.(*RecvPro).FailAction:33) 任务处理失败了,我要进入db日志库了
2020-02-14 16:43:31.925 ERROR (main.go:main.(*RecvPro).FailAction:34) 任务处理失败了,发送钉钉消息通知主人
2020-02-14 16:43:31.925 ERROR (main.go:main.(*RecvPro).FailAction:35) {
"interface_type":"1",
"access_url":"api.ichun.com/v3/login",
"err_msg":"响应超时",
"err_code":"40004",
"uid":"16678",
"user_name":"张三",
"user_ip":"153.2.15.1",
"create_time":1581582711
"create_time_str":"2020-02-14 16:40"
}
2020-02-14 16:43:33.326 ERROR (main.go:main.(*RecvPro).FailAction:33) 任务处理失败了,我要进入db日志库了
2020-02-14 16:43:33.326 ERROR (main.go:main.(*RecvPro).FailAction:34) 任务处理失败了,发送钉钉消息通知主人
2020-02-14 16:43:33.326 ERROR (main.go:main.(*RecvPro).FailAction:35) {
"interface_type":"1",
"access_url":"api.ichun.com/v3/login",
"err_msg":"响应超时",
"err_code":"40004",
"uid":"16678",
"user_name":"张三",
"user_ip":"153.2.15.1",
"create_time":1581582711
"create_time_str":"2020-02-14 16:40"
}
2020-02-14 16:43:33.971 ERROR (main.go:main.(*RecvPro).FailAction:33) 任务处理失败了,我要进入db日志库了
2020-02-14 16:43:33.971 ERROR (main.go:main.(*RecvPro).FailAction:34) 任务处理失败了,发送钉钉消息通知主人
2020-02-14 16:43:33.971 ERROR (main.go:main.(*RecvPro).FailAction:35) {
"interface_type":"1",
"access_url":"api.ichun.com/v3/login",
"err_msg":"响应超时",
"err_code":"40004",
"uid":"16678",
"user_name":"张三",
"user_ip":"153.2.15.1",
"create_time":1581582711
"create_time_str":"2020-02-14 16:40"
}
2020-02-14 16:43:34.603 ERROR (main.go:main.(*RecvPro).FailAction:33) 任务处理失败了,我要进入db日志库了
2020-02-14 16:43:34.603 ERROR (main.go:main.(*RecvPro).FailAction:34) 任务处理失败了,发送钉钉消息通知主人
2020-02-14 16:43:34.603 ERROR (main.go:main.(*RecvPro).FailAction:35) {
"interface_type":"1",
"access_url":"api.ichun.com/v3/login",
"err_msg":"响应超时",
"err_code":"40004",
"uid":"16678",
"user_name":"张三",
"user_ip":"153.2.15.1",
"create_time":1581582711
"create_time_str":"2020-02-14 16:40"
}
2020-02-14 16:43:35.147 ERROR (main.go:main.(*RecvPro).FailAction:33) 任务处理失败了,我要进入db日志库了
2020-02-14 16:43:35.147 ERROR (main.go:main.(*RecvPro).FailAction:34) 任务处理失败了,发送钉钉消息通知主人
2020-02-14 16:43:35.147 ERROR (main.go:main.(*RecvPro).FailAction:35) {
"interface_type":"1",
"access_url":"api.ichun.com/v3/login",
"err_msg":"响应超时",
"err_code":"40004",
"uid":"16678",
"user_name":"张三",
"user_ip":"153.2.15.1",
"create_time":1581582711
"create_time_str":"2020-02-14 16:40"
}
2020-02-20 15:41:27.12 ERROR (main.go:main.(*RecvPro).Consumer:22) %!s(<nil>)
package apiMsgService
import (
"encoding/json"
"fmt"
"go-api-behavior/util"
"time"
)
func ToJson(msg string) (err error){
err = json.Unmarshal([]byte(msg),&util.MsgParams)
return err
}
//写日志到管道
func SaveElkLogChan(MsgParams *util.DataParams){
util.MsgChan <- MsgParams
}
func SaveElkLog(LogReportDir string){
var(
logBatch *util.LogBatch
)
fileLog,_ := util.NewFileLogger(LogReportDir)
fileLog.Init()
for{
select{
case util.MsgParams = <- util.MsgChan:
if logBatch == nil{
logBatch = &util.LogBatch{}
}
// 把新日志追加到批次中
logBatch.Logs = append(logBatch.Logs, util.MsgParams)
// 如果批次满了, 就立即写入
if len(logBatch.Logs) > 500{
//写入日志
fmt.Println("长度到了开始写入日志")
defer fileLog.Close()
fileLog.WriteLog(logBatch)
// 清空logBatch
logBatch = nil
}
case <- time.NewTimer(10*time.Second).C:
if logBatch == nil{
logBatch = &util.LogBatch{}
}
fmt.Println("超时到期了")
fmt.Println(len(logBatch.Logs))
//写入日志
fmt.Println("超时了写入日志")
defer fileLog.Close()
fileLog.WriteLog(logBatch)
// 清空logBatch
logBatch = nil
}
time.Sleep(time.Microsecond*10)
}
}
\ No newline at end of file
package main
import (
"flag"
"fmt"
"github.com/ichunt2019/go-msgserver/utils/rabbitmq"
"github.com/ichunt2019/logger"
"go-api-behavior/service/behavior/apiMsgService"
"go-api-behavior/util"
"sync"
"time"
)
type RecvPro struct {
}
//// 实现消费者 消费消息失败 自动进入延时尝试 尝试3次之后入库db
func (t *RecvPro) Consumer(dataByte []byte) error {
//logger.Info(string(dataByte))
err := apiMsgService.ToJson(string(dataByte))
if err != nil{
logger.Error("%s",err)
return err
}
apiMsgService.SaveElkLogChan(util.MsgParams)
////return errors.New("顶顶顶顶")
return nil
}
//消息已经消费3次 失败了 请进行处理
func (t *RecvPro) FailAction(dataByte []byte) error {
logger.Error("任务处理失败了,我要进入db日志库了")
logger.Error("任务处理失败了,发送钉钉消息通知主人")
logger.Error(string(dataByte))
return nil
}
//func initDb(dns string) (err error) {
// err = db.Init(dns)
// if err != nil {
// return
// }
//
// return
//}
var ConfigDir string
var LogDir string
var LogReportDir string
// 解析命令行参数
func initArgs() {
// worker -config ./worker.json
// worker -h
flag.StringVar(&ConfigDir, "configDir", "", "配置文件")
flag.StringVar(&LogDir, "logDir", "", "日志目录")
flag.StringVar(&LogReportDir, "LogReportDir", "", "用户行为日志目录")
flag.Parse()
}
func main() {
initArgs()
//初始化配置文件
util.Init(ConfigDir)
util.MsgChan = make(chan *util.DataParams,10)
//
logConfig := make(map[string]string)
logConfig["log_path"] = LogDir+"api/behavior"
logConfig["log_chan_size"] = "1000"
logger.InitLogger("file",logConfig)
logger.Init()
//初始化db
//initDb(util.Configs.Liexin_databases.Dns)
t := &RecvPro{}
queueExchange := &rabbitmq.QueueExchange{
util.Configs.Rabbitmq_ichunt.QueueName,
util.Configs.Rabbitmq_ichunt.RoutingKey,
util.Configs.Rabbitmq_ichunt.Exchange,
util.Configs.Rabbitmq_ichunt.Type,
util.Configs.Rabbitmq_ichunt.Dns,
}
go func(){
apiMsgService.SaveElkLog(LogReportDir)
}()
for{
var wg sync.WaitGroup
fmt.Println("开始执行任务....")
for i := 0;i<3;i++{
wg.Add(1)
go func(wg *sync.WaitGroup){
mq := rabbitmq.New(queueExchange)
mq.RegisterReceiver(t)
err :=mq.Start()
if err != nil{
fmt.Println(err)
}
wg.Done()
}(&wg)
}
wg.Wait()
fmt.Println("执行任务完成....")
time.Sleep(time.Microsecond*10)
}
}
\ No newline at end of file
package util
import (
"fmt"
"github.com/BurntSushi/toml"
)
//订制配置文件解析载体
type Config struct{
Liexin_databases *LiexinMysqlConfig
Rabbitmq_ichunt *RabbitmqIchunt
Crm_domain *SendMail
Ding_msg *Ding
Api_domain *ApiDomain
}
type LiexinMysqlConfig struct{
Dns string `toml:"dns"`
}
type RabbitmqIchunt struct {
QueueName string `toml:"queue_name"`
RoutingKey string `toml:"routing_key"`
Exchange string `toml:"exchange"`
Type string `toml:"type"`
Dns string `toml:"dns"`
}
type SendMail struct{
SendMailUrl string `toml:"send_mail"`
}
type Ding struct {
Webhook string `toml:"webhook"`
JingDiao string `toml:"jingDiao"`
}
type ApiDomain struct {
ApiUrl string `toml:"api_url"`
}
var Configs *Config =new (Config)
func Init(ConfigDir string){
//fmt.Println(ConfigDir+"config/config.toml")
var err error
_, err = toml.DecodeFile(ConfigDir+"config.toml",Configs)
_, err = toml.DecodeFile(ConfigDir+"db.toml",Configs)
if err!=nil{
fmt.Println(err)
}
//fmt.Printf("%+v",Configs.Liexin_databases)
//fmt.Printf("%+v",Configs.Rabbitmq_ichunt)
//fmt.Printf("%+v",Configs.Crm_domain)
//fmt.Printf("%+v",Configs.Ding_msg)
//
//fmt.Printf("%+v",Configs.Crm_domain)
//fmt.Printf("%+v",Configs.Rabbitmq_ichunt)
}
package ding
import (
_"fmt"
"go-api-behavior/util"
"net/http"
"strings"
"encoding/json"
"io/ioutil"
"github.com/ichunt2019/logger"
)
type Msg struct {
Msgtype string `json:"msgtype"`
Text Contents `json:"text"`
At Ats `json:"at"`
}
type Contents struct {
Content string `json:"content"`
}
type Ats struct {
AtMobiles []string `json:"atMobiles"`
IsAtAll bool `json:"isAtAll"`
}
// json 返回值
type JosnResp struct {
Errcode int `json:"errcode"`
Errmsg string `json:"errmsg"`
}
func Send(textMsg string, mobiles []string, isAtAll bool) (jsonStr string) {
var msg Msg
msg = Msg{Msgtype:"text"}
msg.Text.Content = "监控告警: " + textMsg // 固定标签 + 文本
msg.At.AtMobiles = mobiles
msg.At.IsAtAll = isAtAll;
content, _ := json.Marshal(msg)
// content := `{
// "msgtype": "text",
// "text": {
// "content": "`+ msg + `"
// }
// }`
ding_url := util.Configs.Ding_msg.Webhook // 钉钉请求接口
client := &http.Client{}
req, _ := http.NewRequest("POST", ding_url, strings.NewReader(string(content)))
req.Header.Set("Content-Type", "application/json; charset=utf-8")
resp, err := client.Do(req);
defer resp.Body.Close()
if err != nil {
logger.Info(err.Error())
}
body, _ := ioutil.ReadAll(resp.Body) // 获取接口返回数据
res := JosnResp{}
if err1 := json.Unmarshal([]byte(body), &res); err1 != nil { // 将接口数据写入res
logger.Info(err1.Error())
}
result, _ := json.Marshal(res)
return string(result)
}
\ No newline at end of file
package util
import (
"encoding/json"
"fmt"
"os"
"strconv"
"time"
"github.com/ichunt2019/logger"
)
//2018/3/26 0:01.383 DEBUG logDebug.go:29 this is a debug log
//2006-01-02 15:04:05.999
type FileLogger struct {
level int
logPath string
logName string
file *os.File
warnFile *os.File
LogDataChan chan *LogData
logSplitType int
logSplitSize int64
lastSplitHour int
}
type LogData struct {
Message string
}
// 队列参数
type DataParams struct {
InterfaceType string `json:"interface_type"`
AccessUrl string `json:"access_url"`
ErrMsg string `json:"err_msg"`
ErrCode string `json:"err_code"`
Uid string `json:"uid"`
UserName string `json:"user_name"`
UserIp string `json:"user_ip"`
Remakr string `json:"remark"`
CreateTime int64 `json:"create_time"`
CreateTimeStr string `json:"create_time_str"`
}
var MsgChan chan *DataParams
var autoCommitChan chan *DataParams
var MsgParams *DataParams
// 日志批次
type LogBatch struct {
Logs []*DataParams // 多条日志
}
func NewFileLogger(LogReportDir string) (log FileLogger, err error) {
logPath := LogReportDir
logName := time.Now().Format("2006-01-02")
logSplitSize, err := strconv.ParseInt("104857600", 10, 64)
if err != nil {
logSplitSize = 104857600
}
logChanSize := "50000"
chanSize, err := strconv.Atoi(logChanSize)
if err != nil {
chanSize = 50000
}
log = FileLogger{
logPath: logPath,
logName: logName,
LogDataChan: make(chan *LogData, chanSize),
logSplitSize: logSplitSize,
}
return
}
//调用os.MkdirAll递归创建文件夹
func createFile(filePath string) error {
if !isExist(filePath) {
err := os.MkdirAll(filePath,os.ModePerm)
return err
}
return nil
}
// 判断所给路径文件/文件夹是否存在(返回true是存在)
func isExist(path string) bool {
_, err := os.Stat(path) //os.Stat获取文件信息
if err != nil {
if os.IsExist(err) {
return true
}
return false
}
return true
}
func (f *FileLogger) Init() {
filename := fmt.Sprintf("%s/%s.log", f.logPath, f.logName)
createFile(f.logPath)
file, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0755)
if err != nil {
panic(fmt.Sprintf("open faile %s failed, err:%v", filename, err))
}
f.file = file
}
func (f *FileLogger) splitFileHour(warnFile bool) {
now := time.Now()
hour := now.Hour()
if hour == f.lastSplitHour {
return
}
f.lastSplitHour = hour
var backupFilename string
var filename string
backupFilename = fmt.Sprintf("%s/%s.log_%04d%02d%02d%02d",
f.logPath, f.logName, now.Year(), now.Month(), now.Day(), f.lastSplitHour)
filename = fmt.Sprintf("%s/%s.log", f.logPath, f.logName)
file := f.file
file.Close()
os.Rename(filename, backupFilename)
file, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0755)
if err != nil {
return
}
f.file = file
}
func (f *FileLogger) splitFileSize() {
file := f.file
statInfo, err := file.Stat()
if err != nil {
return
}
fileSize := statInfo.Size()
if fileSize <= f.logSplitSize {
return
}
var backupFilename string
var filename string
now := time.Now()
backupFilename = fmt.Sprintf("%s/%s.log_%04d%02d%02d%02d%02d%02d",
f.logPath, f.logName, now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second())
filename = fmt.Sprintf("%s/%s.log", f.logPath, f.logName)
file.Close()
os.Rename(filename, backupFilename)
file, err = os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0755)
if err != nil {
return
}
f.file = file
}
func (f *FileLogger) checkSplitFile() {
f.splitFileSize()
}
func (f *FileLogger) WriteLog(logBatch *LogBatch) {
for _,logData := range logBatch.Logs {
fmt.Println("开始写入日志.....")
fmt.Println(logData)
var file *os.File = f.file
f.checkSplitFile()
data, err := json.Marshal(logData)
if err != nil{
logger.Error("%s",err)
continue
}
fmt.Fprintf(file, "%s\n", string(data))
}
}
func (f *FileLogger) Close() {
f.file.Close()
}
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 sign in to comment