fix: adjustment directory structure
This commit is contained in:
194
proxy/canal/canal.go
Normal file
194
proxy/canal/canal.go
Normal file
@@ -0,0 +1,194 @@
|
||||
package canal
|
||||
|
||||
import (
|
||||
"github.com/go-mysql-org/go-mysql/canal"
|
||||
"github.com/go-mysql-org/go-mysql/mysql"
|
||||
"github.com/go-mysql-org/go-mysql/replication"
|
||||
mdb "proxy/Nmysql"
|
||||
rds "proxy/Nredis"
|
||||
"proxy/config"
|
||||
|
||||
//"github.com/siddontang/go-log/log"
|
||||
//"strconv"
|
||||
|
||||
"proxy/logger"
|
||||
)
|
||||
|
||||
type MyEventHandler struct {
|
||||
canal.DummyEventHandler
|
||||
}
|
||||
|
||||
// 监听数据记录
|
||||
func (h *MyEventHandler) OnRow(e *canal.RowsEvent) error {
|
||||
logger.CanalLog.Infof("OnRow: %s.%s %s %v", e.Table.Schema, e.Table.Name, e.Action, e.Rows)
|
||||
|
||||
/*if !config.Config.CanalServer.Standalone && rds.CheckIfRdbMaster() == false {
|
||||
logger.CanalLog.Warnf("not stand alone, and change to Slave!")
|
||||
cnl.Close()
|
||||
return nil
|
||||
}*/
|
||||
c := ParseAndFilterBinLog(e)
|
||||
updateRedisTable(c)
|
||||
for columnIndex, curColumn := range e.Table.Columns {
|
||||
logger.CanalLog.Debugf("row info: %v %v %v", curColumn.Name, columnIndex, e.Rows[len(e.Rows)-1][columnIndex])
|
||||
}
|
||||
//cnl.Close()// if slave
|
||||
return nil
|
||||
}
|
||||
|
||||
// 创建,更改,重命名或者删除表时触发,通常会需要清除与表相关的数据,如缓存。 It will be called before OnDDL.
|
||||
func (h *MyEventHandler) OnTableChanged(header *replication.EventHeader, schema string, table string) error {
|
||||
logger.CanalLog.Infof("OnTableChanged: %s %s", schema, table)
|
||||
return nil
|
||||
}
|
||||
|
||||
// 监听binlog日志的变化文件与记录的位置
|
||||
func (h *MyEventHandler) OnPosSynced(header *replication.EventHeader, pos mysql.Position, set mysql.GTIDSet, force bool) error {
|
||||
// if force == true, 立即同步位置
|
||||
rds.RdbSetBinLogPos(pos.Name, pos.Pos)
|
||||
logger.CanalLog.Infof("OnPosSynced: Name[%v] Pos[%v], force[%t]", pos.Name, pos.Pos, force)
|
||||
return nil
|
||||
}
|
||||
|
||||
// 当产生新的binlog日志后触发(在达到内存的使用限制后(默认为1GB),会开启另一个文件,每个新文件的名称后都会有一个增量.)
|
||||
func (h *MyEventHandler) OnRotate(header *replication.EventHeader, r *replication.RotateEvent) error {
|
||||
// record := fmt.Sprintf("On Rotate: %v \n", &mysql.Position{Name: string(r.NextLogName), Pos: uint32(r.Position)})
|
||||
// binlog的记录位置,新binlog的文件名
|
||||
logger.CanalLog.Infof("On Rotate: Pos[%v] NextLogName[%v] \n", r.Position, r.NextLogName)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Create alter drop truncate(删除当前表再新建一个一模一样的表结构)
|
||||
func (h *MyEventHandler) OnDDL(header *replication.EventHeader, nextPos mysql.Position, queryEvent *replication.QueryEvent) error {
|
||||
// binlog日志的变化文件与记录的位置
|
||||
logger.CanalLog.Infof("OnDDL: Name[%v] Pos[%v]\n", nextPos.Name, nextPos.Pos)
|
||||
logger.CanalLog.Infof("%v\n %v\n %v\n %v\n %v\n",
|
||||
queryEvent.ExecutionTime, // 猜是执行时间,但测试显示0
|
||||
string(queryEvent.Schema), // 库名
|
||||
string(queryEvent.Query), // 变更的sql语句
|
||||
string(queryEvent.StatusVars[:]), //测试显示乱码
|
||||
queryEvent.SlaveProxyID) // 从库代理ID?
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *MyEventHandler) String() string {
|
||||
return "MyEventHandler"
|
||||
}
|
||||
|
||||
var cnl *canal.Canal
|
||||
func StartMyCanal(addr string, username string, password string) {
|
||||
cfg := canal.NewDefaultConfig()
|
||||
cfg.Addr = addr //"192.168.1.211:3306"
|
||||
cfg.User = username
|
||||
cfg.Password = password
|
||||
// We only care table canal_test in test db
|
||||
cfg.Dump.TableDB = "boss"
|
||||
cfg.Dump.Tables = []string{"tb_prd_ofr_detail_inst_551",
|
||||
"tb_bil_tariff", "tb_prd_ofr", "tb_bil_evt_pricing_strategy", "config_area",
|
||||
"tb_bil_pricing_area", "ratable_history", "tb_bil_holiday_rel", "tb_bil_holiday"}//, "tb_prd_prd_inst_551"}
|
||||
cfg.IncludeTableRegex = make([]string, 0, 1)
|
||||
cfg.IncludeTableRegex = append(cfg.IncludeTableRegex, "boss\\.tb_prd_prd_inst_551")
|
||||
cfg.IncludeTableRegex = append(cfg.IncludeTableRegex, "boss\\.tb_prd_ofr_detail_inst_551")
|
||||
cfg.IncludeTableRegex = append(cfg.IncludeTableRegex, "boss\\.tb_bil_tariff")
|
||||
cfg.IncludeTableRegex = append(cfg.IncludeTableRegex, "boss\\.tb_prd_ofr")
|
||||
cfg.IncludeTableRegex = append(cfg.IncludeTableRegex, "boss\\.tb_bil_evt_pricing_strategy")
|
||||
cfg.IncludeTableRegex = append(cfg.IncludeTableRegex, "boss\\.config_area")
|
||||
cfg.IncludeTableRegex = append(cfg.IncludeTableRegex, "boss\\.tb_bil_pricing_area")
|
||||
cfg.IncludeTableRegex = append(cfg.IncludeTableRegex, "boss\\.ratable_history")
|
||||
cfg.IncludeTableRegex = append(cfg.IncludeTableRegex, "boss\\.tb_bil_holiday_rel")
|
||||
cfg.IncludeTableRegex = append(cfg.IncludeTableRegex, "boss\\.tb_bil_holiday")
|
||||
if config.Config.CronCfg.Enabled && config.Config.CronCfg.NtfSms != "" {
|
||||
cfg.IncludeTableRegex = append(cfg.IncludeTableRegex, "boss\\.tb_sms_info")
|
||||
}
|
||||
if config.Config.Rest.Enabled {
|
||||
cfg.Dump.Tables = append(cfg.Dump.Tables, "tb_sync_mobile")
|
||||
cfg.IncludeTableRegex = append(cfg.IncludeTableRegex, "boss\\.tb_sync_mobile")
|
||||
}
|
||||
|
||||
c, err := canal.NewCanal(cfg)
|
||||
if err != nil {
|
||||
logger.CanalLog.Errorf("NewCanal, err: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Register a handler to handle RowsEvent
|
||||
c.SetEventHandler(&MyEventHandler{})
|
||||
|
||||
var pos *mysql.Position
|
||||
if config.Config.CanalServer.Reinit {
|
||||
pos = QueryBinLogPos(c)
|
||||
if pos == nil {
|
||||
logger.CanalLog.Errorln("fail to get binlog position.")
|
||||
return
|
||||
}
|
||||
|
||||
logger.CanalLog.Infoln("start to sync mysql DB.")
|
||||
CurState = "loading"
|
||||
if config.Config.CanalServer.FlushBeforeInit {
|
||||
rds.ClrTariffAndBundle()
|
||||
}
|
||||
_ = mdb.LoadAcctTblFromMysql()
|
||||
_ = mdb.LoadAlertSmsFromMysql()
|
||||
//_ = mdb.LoadCreateAcctFromMysql()
|
||||
config.Config.CanalServer.Reinit = false
|
||||
config.SavePcfCfg()
|
||||
logger.CanalLog.Infoln("finish sync mysql DB...")
|
||||
CurState = "synchronize"
|
||||
} else {
|
||||
ret := rds.RdbGetBinLogPos()
|
||||
if ret == nil {
|
||||
logger.CanalLog.Warnln("fail to get redis binlog position, query current position from mysql.")
|
||||
pos = QueryBinLogPos(c)
|
||||
if pos == nil {
|
||||
logger.CanalLog.Errorln("fail to get binlog position.")
|
||||
return
|
||||
}
|
||||
} else {
|
||||
pos = &mysql.Position{Name: ret.File, Pos: ret.Position}
|
||||
}
|
||||
}
|
||||
|
||||
// Start canal
|
||||
cnl = c
|
||||
logger.CanalLog.Infof("Go run")
|
||||
err = c.RunFrom(*pos)
|
||||
if err != nil {
|
||||
logger.CanalLog.Errorf("RunFrom err: %v", err)
|
||||
} else {
|
||||
logger.CanalLog.Warnf("Canal exit!!!!!!")
|
||||
c.Close()
|
||||
}
|
||||
|
||||
/* 从头开始监听
|
||||
err = c.Run()
|
||||
if err != nil {
|
||||
logger.CanalLog.Infof("Run err: %v", err)
|
||||
}*/
|
||||
|
||||
// mysql-bin.000004, 1027
|
||||
// startPos := mysql.Position{Name: "mysql-bin.000004", Pos: 1027}
|
||||
// c.RunFrom(startPos)
|
||||
}
|
||||
|
||||
func QueryBinLogPos(c *canal.Canal) *mysql.Position {
|
||||
var pos mysql.Position
|
||||
ret, err := c.Execute("SHOW MASTER STATUS;")
|
||||
if err != nil {
|
||||
logger.CanalLog.Errorf("SHOW MASTER STATUS err: %v", err)
|
||||
return nil
|
||||
}
|
||||
pos.Name, err = ret.Resultset.GetStringByName(0, "File")
|
||||
if err != nil {
|
||||
logger.CanalLog.Errorf("Get binlog file name err: %v", err)
|
||||
return nil
|
||||
}
|
||||
posTmp, err := ret.Resultset.GetUintByName(0, "Position")
|
||||
if err != nil {
|
||||
logger.CanalLog.Errorf("Get binlog position err: %v", err)
|
||||
return nil
|
||||
}
|
||||
pos.Pos = uint32(posTmp)
|
||||
logger.CanalLog.Infof("Current Position: %v", pos)
|
||||
rds.RdbSetBinLogPos(pos.Name, pos.Pos)
|
||||
return &pos
|
||||
}
|
||||
197
proxy/canal/canal.go.bak
Normal file
197
proxy/canal/canal.go.bak
Normal file
@@ -0,0 +1,197 @@
|
||||
package canal
|
||||
|
||||
import (
|
||||
"github.com/go-mysql-org/go-mysql/canal"
|
||||
"github.com/go-mysql-org/go-mysql/mysql"
|
||||
"github.com/go-mysql-org/go-mysql/replication"
|
||||
mdb "proxy/Nmysql"
|
||||
rds "proxy/Nredis"
|
||||
"proxy/config"
|
||||
|
||||
//"github.com/siddontang/go-log/log"
|
||||
//"strconv"
|
||||
|
||||
"proxy/logger"
|
||||
)
|
||||
|
||||
type MyEventHandler struct {
|
||||
canal.DummyEventHandler
|
||||
}
|
||||
|
||||
// 监听数据记录
|
||||
func (h *MyEventHandler) OnRow(e *canal.RowsEvent) error {
|
||||
|
||||
logger.CanalLog.Infof("OnRow: %s.%s %s %v", e.Table.Schema, e.Table.Name, e.Action, e.Rows)
|
||||
|
||||
/*if !config.Config.CanalServer.Standalone && rds.CheckIfRdbMaster() == false {
|
||||
logger.CanalLog.Warnf("not stand alone, and change to Slave!")
|
||||
cnl.Close()
|
||||
return nil
|
||||
}*/
|
||||
c := ParseAndFilterBinLog(e)
|
||||
updateRedisTable(c)
|
||||
for columnIndex, curColumn := range e.Table.Columns {
|
||||
logger.CanalLog.Debugf("row info: %v %v %v", curColumn.Name, columnIndex, e.Rows[len(e.Rows)-1][columnIndex])
|
||||
}
|
||||
//cnl.Close()// if slave
|
||||
return nil
|
||||
}
|
||||
|
||||
// 创建,更改,重命名或者删除表时触发,通常会需要清除与表相关的数据,如缓存。 It will be called before OnDDL.
|
||||
func (h *MyEventHandler) OnTableChanged(schema string, table string) error {
|
||||
//
|
||||
logger.CanalLog.Infof("OnTableChanged: %s %s", schema, table)
|
||||
return nil
|
||||
}
|
||||
|
||||
// 监听binlog日志的变化文件与记录的位置
|
||||
func (h *MyEventHandler) OnPosSynced(pos mysql.Position, set mysql.GTIDSet, force bool) error {// update and save
|
||||
// if force == true, 立即同步位置
|
||||
rds.RdbSetBinLogPos(pos.Name, pos.Pos)
|
||||
logger.CanalLog.Infof("OnPosSynced: Name[%v] Pos[%v], force[%t]", pos.Name, pos.Pos, force)
|
||||
return nil
|
||||
}
|
||||
|
||||
// 当产生新的binlog日志后触发(在达到内存的使用限制后(默认为1GB),会开启另一个文件,每个新文件的名称后都会有一个增量.)
|
||||
func (h *MyEventHandler) OnRotate(r *replication.RotateEvent) error {
|
||||
// record := fmt.Sprintf("On Rotate: %v \n", &mysql.Position{Name: string(r.NextLogName), Pos: uint32(r.Position)})
|
||||
// binlog的记录位置,新binlog的文件名
|
||||
logger.CanalLog.Infof("On Rotate: Pos[%v] NextLogName[%v] \n", r.Position, r.NextLogName)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Create alter drop truncate(删除当前表再新建一个一模一样的表结构)
|
||||
func (h *MyEventHandler) OnDDL(nextPos mysql.Position, queryEvent *replication.QueryEvent) error {
|
||||
// binlog日志的变化文件与记录的位置
|
||||
logger.CanalLog.Infof("OnDDL: Name[%v] Pos[%v]\n", nextPos.Name, nextPos.Pos)
|
||||
logger.CanalLog.Infof("%v\n %v\n %v\n %v\n %v\n",
|
||||
queryEvent.ExecutionTime,// 猜是执行时间,但测试显示0
|
||||
string(queryEvent.Schema),// 库名
|
||||
string(queryEvent.Query),// 变更的sql语句
|
||||
string(queryEvent.StatusVars[:]),//测试显示乱码
|
||||
queryEvent.SlaveProxyID)// 从库代理ID?
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *MyEventHandler) String() string {
|
||||
return "MyEventHandler"
|
||||
}
|
||||
|
||||
var cnl *canal.Canal
|
||||
func StartMyCanal(addr string, username string, password string) {
|
||||
cfg := canal.NewDefaultConfig()
|
||||
cfg.Addr = addr//"192.168.1.211:3306"
|
||||
cfg.User = username
|
||||
cfg.Password = password
|
||||
// We only care table canal_test in test db
|
||||
cfg.Dump.TableDB = "boss"
|
||||
cfg.Dump.Tables = []string{"tb_prd_ofr_detail_inst_551",
|
||||
"tb_bil_tariff", "tb_prd_ofr", "tb_bil_evt_pricing_strategy", "config_area",
|
||||
"tb_bil_pricing_area", "ratable_history", "tb_bil_holiday_rel", "tb_bil_holiday"}//, "tb_prd_prd_inst_551"}
|
||||
cfg.IncludeTableRegex = make([]string, 0, 1)
|
||||
cfg.IncludeTableRegex = append(cfg.IncludeTableRegex, "boss\\.tb_prd_prd_inst_551")
|
||||
cfg.IncludeTableRegex = append(cfg.IncludeTableRegex, "boss\\.tb_prd_ofr_detail_inst_551")
|
||||
cfg.IncludeTableRegex = append(cfg.IncludeTableRegex, "boss\\.tb_bil_tariff")
|
||||
cfg.IncludeTableRegex = append(cfg.IncludeTableRegex, "boss\\.tb_prd_ofr")
|
||||
cfg.IncludeTableRegex = append(cfg.IncludeTableRegex, "boss\\.tb_bil_evt_pricing_strategy")
|
||||
cfg.IncludeTableRegex = append(cfg.IncludeTableRegex, "boss\\.config_area")
|
||||
cfg.IncludeTableRegex = append(cfg.IncludeTableRegex, "boss\\.tb_bil_pricing_area")
|
||||
cfg.IncludeTableRegex = append(cfg.IncludeTableRegex, "boss\\.ratable_history")
|
||||
cfg.IncludeTableRegex = append(cfg.IncludeTableRegex, "boss\\.tb_bil_holiday_rel")
|
||||
cfg.IncludeTableRegex = append(cfg.IncludeTableRegex, "boss\\.tb_bil_holiday")
|
||||
if config.Config.CronCfg.Enabled && config.Config.CronCfg.NtfSms != "" {
|
||||
|
||||
cfg.IncludeTableRegex = append(cfg.IncludeTableRegex, "boss\\.tb_sms_info")
|
||||
}
|
||||
if config.Config.Rest.Enabled {
|
||||
cfg.Dump.Tables = append(cfg.Dump.Tables, "tb_sync_mobile")
|
||||
cfg.IncludeTableRegex = append(cfg.IncludeTableRegex, "boss\\.tb_sync_mobile")
|
||||
}
|
||||
|
||||
c, err := canal.NewCanal(cfg)
|
||||
if err != nil {
|
||||
logger.CanalLog.Errorf("NewCanal, err: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Register a handler to handle RowsEvent
|
||||
c.SetEventHandler(&MyEventHandler{})
|
||||
|
||||
var pos *mysql.Position
|
||||
if config.Config.CanalServer.Reinit {
|
||||
pos = QueryBinLogPos(c)
|
||||
if pos == nil {
|
||||
logger.CanalLog.Errorln("fail to get binlog position.")
|
||||
return
|
||||
}
|
||||
|
||||
logger.CanalLog.Infoln("start to sync mysql DB.")
|
||||
CurState = "loading"
|
||||
if config.Config.CanalServer.FlushBeforeInit {
|
||||
rds.ClrTariffAndBundle()
|
||||
}
|
||||
_ = mdb.LoadAcctTblFromMysql()
|
||||
_ = mdb.LoadAlertSmsFromMysql()
|
||||
//_ = mdb.LoadCreateAcctFromMysql()
|
||||
config.Config.CanalServer.Reinit = false
|
||||
config.SavePcfCfg()
|
||||
logger.CanalLog.Infoln("finish sync mysql DB...")
|
||||
CurState = "synchronize"
|
||||
} else {
|
||||
ret := rds.RdbGetBinLogPos()
|
||||
if ret == nil {
|
||||
logger.CanalLog.Warnln("fail to get redis binlog position, query current position from mysql.")
|
||||
pos = QueryBinLogPos(c)
|
||||
if pos == nil {
|
||||
logger.CanalLog.Errorln("fail to get binlog position.")
|
||||
return
|
||||
}
|
||||
} else {
|
||||
pos = &mysql.Position{Name: ret.File, Pos: ret.Position}
|
||||
}
|
||||
}
|
||||
|
||||
// Start canal
|
||||
cnl = c
|
||||
logger.CanalLog.Infof("Go run")
|
||||
err = c.RunFrom(*pos)
|
||||
if err != nil {
|
||||
logger.CanalLog.Errorf("RunFrom err: %v", err)
|
||||
} else {
|
||||
logger.CanalLog.Warnf("Canal exit!!!!!!")
|
||||
c.Close()
|
||||
}
|
||||
|
||||
/* 从头开始监听
|
||||
err = c.Run()
|
||||
if err != nil {
|
||||
logger.CanalLog.Infof("Run err: %v", err)
|
||||
}*/
|
||||
|
||||
// mysql-bin.000004, 1027
|
||||
// startPos := mysql.Position{Name: "mysql-bin.000004", Pos: 1027}
|
||||
// c.RunFrom(startPos)
|
||||
}
|
||||
|
||||
func QueryBinLogPos(c *canal.Canal) *mysql.Position {
|
||||
var pos mysql.Position
|
||||
ret, err := c.Execute("SHOW MASTER STATUS;")
|
||||
if err != nil {
|
||||
logger.CanalLog.Errorf("SHOW MASTER STATUS err: %v", err)
|
||||
return nil
|
||||
}
|
||||
pos.Name, err = ret.Resultset.GetStringByName(0, "File")
|
||||
if err != nil {
|
||||
logger.CanalLog.Errorf("Get binlog file name err: %v", err)
|
||||
return nil
|
||||
}
|
||||
posTmp, err := ret.Resultset.GetUintByName(0, "Position")
|
||||
if err != nil {
|
||||
logger.CanalLog.Errorf("Get binlog position err: %v", err)
|
||||
return nil
|
||||
}
|
||||
pos.Pos = uint32(posTmp)
|
||||
logger.CanalLog.Infof("Current Position: %v", pos)
|
||||
rds.RdbSetBinLogPos(pos.Name, pos.Pos)
|
||||
return &pos
|
||||
}
|
||||
160
proxy/canal/client.go
Normal file
160
proxy/canal/client.go
Normal file
@@ -0,0 +1,160 @@
|
||||
package canal
|
||||
|
||||
import (
|
||||
//"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/golang/protobuf/proto"
|
||||
"github.com/withlin/canal-go/client"
|
||||
pbe "github.com/withlin/canal-go/protocol/entry"
|
||||
"proxy/logger"
|
||||
)
|
||||
|
||||
func ConectCanalServer(ipaddr string) {
|
||||
|
||||
// 192.168.199.17 替换成你的canal server的地址
|
||||
// example 替换成-e canal.destinations=example 你自己定义的名字
|
||||
connector := client.NewSimpleCanalConnector(ipaddr, 11111, "", "", "example", 60000, 60*60*1000)
|
||||
err := connector.Connect()
|
||||
if err != nil {
|
||||
logger.CanalLog.Errorln(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// https://github.com/alibaba/canal/wiki/AdminGuide
|
||||
//mysql 数据解析关注的表,Perl正则表达式.
|
||||
//
|
||||
//多个正则之间以逗号(,)分隔,转义符需要双斜杠(\\)
|
||||
//
|
||||
//常见例子:
|
||||
//
|
||||
// 1. 所有表:.* or .*\\..*
|
||||
// 2. canal schema下所有表: canal\\..*
|
||||
// 3. canal下的以canal打头的表:canal\\.canal.*
|
||||
// 4. canal schema下的一张表:canal\\.test1
|
||||
// 5. 多个规则组合使用:canal\\..*,mysql.test1,mysql.test2 (逗号分隔)
|
||||
|
||||
filter := "canal.instance.filter.regex=boss.tb_prd_prd_inst_551,boss.tb_prd_ofr_detail_inst_551,boss.tb_bil_tariff,boss.tb_prd_ofr,boss.tb_bil_evt_pricing_strategy,boss.config_area,boss.tb_bil_pricing_area,boss.ratable_history,boss.tb_bil_holiday_rel,boss.tb_bil_holiday"
|
||||
//err = connector.Subscribe(".*\\..*")
|
||||
err = connector.Subscribe(filter)
|
||||
if err != nil {
|
||||
logger.CanalLog.Errorln(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
var counter int= 0
|
||||
for {
|
||||
|
||||
message, err := connector.Get(100, nil, nil)
|
||||
if err != nil {
|
||||
logger.CanalLog.Errorln(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
batchId := message.Id
|
||||
if batchId == -1 || len(message.Entries) <= 0 {
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
counter++
|
||||
if counter == 300 {
|
||||
logger.CanalLog.Infoln("===Idle===")
|
||||
counter = 0
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
printEntry(message.Entries)
|
||||
counter = 0
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
func updateRedisTable(oneR *RecordChange) {
|
||||
var synType int
|
||||
if oneR.EventType == pbe.EventType_INSERT {
|
||||
synType = 1
|
||||
} else if oneR.EventType == pbe.EventType_DELETE {
|
||||
synType = 3
|
||||
} else {
|
||||
synType = 2
|
||||
}
|
||||
switch oneR.TableName {
|
||||
case TbAcctInfo:
|
||||
mdb.UpdateRedisAcctTable(synType, oneR.ChgAcctInfo.PrdInstId, oneR.ChgAcctInfo.ServiceNbr)
|
||||
case TbOfrDetail:
|
||||
mdb.UpdateRedisAcctTableOfr(synType, oneR.ChgOfrDetail.OfrDetailInstId)
|
||||
case TbTariff:
|
||||
mdb.UpdateRedisTariffTable(synType, oneR.ChgTariff.TariffId)
|
||||
case TbOfrInfo:
|
||||
mdb.UpdateRedisTariffTableByOfr(synType, oneR.ChgOfrInfo.OfrId)
|
||||
case TbPricingStrategy:
|
||||
mdb.UpdateRedisTariffTableByStrategy(synType, oneR.ChgPricingStrategy.EventPricingStrategyId)
|
||||
case TbConfigArea:// update prefix table, while update config_area table
|
||||
mdb.UpdateRedisPrefixTableByConfigArea(synType, oneR.ChgConfigArea.AreaId, oneR.ChgConfigArea.OldAreaCode, oneR.ChgConfigArea.NewAreaCode)
|
||||
case TbPricingArea:
|
||||
mdb.UpdateRedisPrefixTable(synType, oneR.ChgPricingArea.NewStrategyId, oneR.ChgPricingArea.NewAreaId)
|
||||
case TbRr:
|
||||
if rrId > 0 {
|
||||
mdb.UpdateRedisRrTable(synType, rrId, ratableVal, usedVal, beginTime, endTime)
|
||||
} else {
|
||||
if ofrId > 0 {// ofr: CALC_PRIORITY
|
||||
mdb.UpdateRdbRrOfrPriority(ofrId, tariffSeq)
|
||||
} else if strategyId > 0 {// strategy: EVENT_PRIORITY
|
||||
mdb.UpdateRdbRrStrategyPriority(strategyId, tariffSeq)
|
||||
}
|
||||
}
|
||||
case TbHoliday:
|
||||
mdb.UpdateRedisHolidayDisTableByTariffId(synType, TariffId, tariffSeq)
|
||||
case TbBilHoliday:
|
||||
mdb.UpdateRedisHolidayDisTableByHolidayId(synType, holidayId)
|
||||
default:
|
||||
}
|
||||
}*/
|
||||
|
||||
func printEntry(entrys []pbe.Entry) {
|
||||
|
||||
for _, entry := range entrys {
|
||||
if entry.GetEntryType() == pbe.EntryType_TRANSACTIONBEGIN || entry.GetEntryType() == pbe.EntryType_TRANSACTIONEND {
|
||||
continue
|
||||
}
|
||||
rowChange := new(pbe.RowChange)
|
||||
|
||||
err := proto.Unmarshal(entry.GetStoreValue(), rowChange)
|
||||
checkError(err)
|
||||
if rowChange != nil {
|
||||
eventType := rowChange.GetEventType()
|
||||
header := entry.GetHeader()
|
||||
logger.CanalLog.Infof("================> binlog[%s : %d],name[%s,%s], eventType: %s", header.GetLogfileName(), header.GetLogfileOffset(), header.GetSchemaName(), header.GetTableName(), header.GetEventType())
|
||||
|
||||
for _, rowData := range rowChange.GetRowDatas() {
|
||||
/*oneRecordChg := ParseAndFilterChange(eventType, header.GetTableName(), rowData)
|
||||
if oneRecordChg == nil {
|
||||
continue
|
||||
}
|
||||
updateRedisTable(oneRecordChg)*/
|
||||
if eventType == pbe.EventType_DELETE {
|
||||
printColumn(rowData.GetBeforeColumns())
|
||||
} else if eventType == pbe.EventType_INSERT {
|
||||
printColumn(rowData.GetAfterColumns())
|
||||
} else {
|
||||
logger.CanalLog.Infoln("-------> before")
|
||||
printColumn(rowData.GetBeforeColumns())
|
||||
logger.CanalLog.Infoln("-------> after")
|
||||
printColumn(rowData.GetAfterColumns())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func printColumn(columns []*pbe.Column) {
|
||||
for _, col := range columns {
|
||||
logger.CanalLog.Infof("%s : %s update= %t", col.GetName(), col.GetValue(), col.GetUpdated())
|
||||
}
|
||||
}
|
||||
|
||||
func checkError(err error) {
|
||||
if err != nil {
|
||||
logger.CanalLog.Errorf("Fatal error: %s", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
28
proxy/canal/client_fsm.go
Normal file
28
proxy/canal/client_fsm.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package canal
|
||||
|
||||
import (
|
||||
"proxy/logger"
|
||||
"time"
|
||||
)
|
||||
|
||||
var CurState string= "init"
|
||||
func CanalFsm(addr string, username string, password string) {
|
||||
t := time.NewTimer(time.Second * 2)
|
||||
defer t.Stop()
|
||||
for {
|
||||
<-t.C// wait for next tick
|
||||
|
||||
//if config.Config.CanalServer.Standalone || (!config.Config.CanalServer.Standalone && rds.CheckIfRdbMaster() == true) {
|
||||
StartMyCanal(addr, username, password)
|
||||
logger.CanalLog.Warnf("Exit Canal!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
|
||||
t.Reset(time.Second * 3)
|
||||
/*} else {
|
||||
CurState = "idle"
|
||||
if config.Config.CanalServer.Reinit {
|
||||
config.Config.CanalServer.Reinit = false
|
||||
config.SavePcfCfg()
|
||||
}
|
||||
t.Reset(time.Second * 2)
|
||||
}*/
|
||||
}
|
||||
}
|
||||
1102
proxy/canal/msg.go
Normal file
1102
proxy/canal/msg.go
Normal file
File diff suppressed because it is too large
Load Diff
114
proxy/canal/sync_redis.go
Normal file
114
proxy/canal/sync_redis.go
Normal file
@@ -0,0 +1,114 @@
|
||||
package canal
|
||||
|
||||
import (
|
||||
"github.com/go-mysql-org/go-mysql/canal"
|
||||
. "proxy/MsgDef"
|
||||
mdb "proxy/Nmysql"
|
||||
"proxy/logger"
|
||||
)
|
||||
|
||||
func updateRedisTable(c *RecordChange) {
|
||||
if c == nil {
|
||||
return
|
||||
}
|
||||
var synType int
|
||||
if c.EventType == canal.InsertAction {
|
||||
synType = 1
|
||||
} else if c.EventType == canal.DeleteAction {
|
||||
synType = 3
|
||||
} else {
|
||||
synType = 2
|
||||
}
|
||||
switch c.TableName {
|
||||
case TbAcctInfo:
|
||||
for _, row := range c.ChgAcctInfo {
|
||||
logger.CanalLog.Infof("entry updateRedisTable: TbAcctInfo, type[%d]", synType)
|
||||
mdb.UpdateRedisAcctTable(synType, &row)
|
||||
}
|
||||
case TbOfrDetail:
|
||||
for _, row := range c.ChgOfrDetail {
|
||||
logger.CanalLog.Infof("entry updateRedisTable: TbOfrDetail, type[%d]", synType)
|
||||
mdb.UpdateRedisAcctTableOfr(synType, &row)
|
||||
}
|
||||
case TbTariff:
|
||||
for _, row := range c.ChgTariff {
|
||||
logger.CanalLog.Infof("entry updateRedisTable: TbTariff, type[%d]", synType)
|
||||
mdb.UpdateRedisTariffTable(synType, &row)
|
||||
}
|
||||
case TbOfrInfo:
|
||||
for _, row := range c.ChgOfrInfo {
|
||||
logger.CanalLog.Infof("entry updateRedisTable: TbOfrInfo, type[%d]", synType)
|
||||
mdb.UpdateRedisTariffTableByOfr(synType, &row)
|
||||
}
|
||||
case TbPricingStrategy:
|
||||
for _, row := range c.ChgPricingStrategy {
|
||||
logger.CanalLog.Infof("entry updateRedisTable: TbPricingStrategy, type[%d]", synType)
|
||||
mdb.UpdateRedisTariffTableByStrategy(synType, &row)
|
||||
}
|
||||
case TbConfigArea:// update prefix table, while update config_area table
|
||||
for _, row := range c.ChgConfigArea {
|
||||
logger.CanalLog.Infof("entry updateRedisTable: TbConfigArea, type[%d]", synType)
|
||||
if synType == 3 {
|
||||
mdb.UpdateRedisPrefixTableByConfigArea(synType, &row)
|
||||
} else {
|
||||
mdb.UpdateRedisPrefixTableByConfigArea(synType, &row)
|
||||
}
|
||||
}
|
||||
case TbPricingArea:
|
||||
for _, row := range c.ChgPricingArea {
|
||||
logger.CanalLog.Infof("entry updateRedisTable: TbPricingArea, type[%d]", synType)
|
||||
if synType == 3 {
|
||||
mdb.UpdateRedisPrefixTable(synType, &row)
|
||||
} else {
|
||||
mdb.UpdateRedisPrefixTable(synType, &row)
|
||||
}
|
||||
}
|
||||
case TbRr:
|
||||
for _, row := range c.ChgRr {
|
||||
logger.CanalLog.Infof("entry updateRedisTable: TbRr, type[%d]", synType)
|
||||
//if row.Id > 0 {
|
||||
if synType == 1 {
|
||||
mdb.UpdateRedisRrTable(synType, &row)
|
||||
} else if synType == 3 {
|
||||
mdb.UpdateRedisRrTable(synType, &row)
|
||||
} else {
|
||||
mdb.UpdateRedisRrTable(synType, &row)
|
||||
}
|
||||
|
||||
/*} else {
|
||||
if ofrId > 0 {// ofr: CALC_PRIORITY
|
||||
mdb.UpdateRdbRrOfrPriority(ofrId, tariffSeq)
|
||||
} else if strategyId > 0 {// strategy: EVENT_PRIORITY
|
||||
mdb.UpdateRdbRrStrategyPriority(strategyId, tariffSeq)
|
||||
}
|
||||
}*/
|
||||
}
|
||||
case TbHoliday:
|
||||
for _, row := range c.ChgHoliday {
|
||||
logger.CanalLog.Infof("entry updateRedisTable: TbHoliday, type[%d]", synType)
|
||||
if synType == 1 {
|
||||
mdb.UpdateRedisHolidayDisTableByTariffId(synType, &row)
|
||||
} else if synType == 3 {
|
||||
mdb.UpdateRedisHolidayDisTableByTariffId(synType, &row)
|
||||
} else {
|
||||
mdb.UpdateRedisHolidayDisTableByTariffId(synType, &row)
|
||||
}
|
||||
}
|
||||
case TbBilHoliday:
|
||||
for _, row := range c.ChgBilHoliday {
|
||||
logger.CanalLog.Infof("entry updateRedisTable: TbBilHoliday, type[%d]", synType)
|
||||
mdb.UpdateRedisHolidayDisTableByHolidayId(synType, &row)
|
||||
}
|
||||
case TbSmsInfo:
|
||||
for _, row := range c.ChgAlertSms {
|
||||
logger.CanalLog.Infof("entry updateRedisTable: TbSmsInfo, type[%d]", synType)
|
||||
mdb.UpdateRedisAlertSms(synType, &row)
|
||||
}
|
||||
case TbSyncMobile:
|
||||
/*for _, row := range c.ChgSyncMobile {
|
||||
logger.CanalLog.Infof("entry updateRedisTable: TbSyncMobile, type[%d]", synType)
|
||||
mdb.UpdateRedisSyncMobile(synType, &row)
|
||||
}*/
|
||||
default:
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user