示例#1
0
文件: canal.go 项目: funkygao/gafka
func (this *Canal) Run(args []string) (exitCode int) {
	cfg := canal.NewDefaultConfig()

	cmdFlags := flag.NewFlagSet("canal", flag.ContinueOnError)
	cmdFlags.Usage = func() { this.Ui.Output(this.Help()) }
	cmdFlags.StringVar(&cfg.User, "user", "", "")
	cmdFlags.StringVar(&cfg.Password, "pass", "", "")
	cmdFlags.StringVar(&cfg.Addr, "dsn", "", "")
	cmdFlags.StringVar(&cfg.Dump.TableDB, "db", "", "")
	if err := cmdFlags.Parse(args); err != nil {
		return 1
	}

	cfg.Dump.Tables = []string{"logs"}

	c, err := canal.NewCanal(cfg)
	if err != nil {
		this.Ui.Error(err.Error())
		return
	}
	defer c.Close()

	c.RegRowsEventHandler(&myRowsEventHandler{})

	// Start canal
	c.Start()

	time.Sleep(time.Hour)

	return
}
示例#2
0
func main() {
	cfg := canal.NewDefaultConfig()
	cfg.Addr = fmt.Sprintf("%s:%d", *host, *port)
	cfg.User = *user
	cfg.Password = *password
	cfg.Flavor = *flavor
	cfg.DataDir = *dataDir

	cfg.ServerID = uint32(*serverID)
	cfg.Dump.ExecutionPath = *mysqldump
	cfg.Dump.DiscardErr = false

	c, err := canal.NewCanal(cfg)
	if err != nil {
		fmt.Printf("create canal err %v", err)
		os.Exit(1)
	}

	if len(*ignoreTables) == 0 {
		subs := strings.Split(*ignoreTables, ",")
		for _, sub := range subs {
			if seps := strings.Split(sub, "."); len(seps) == 2 {
				c.AddDumpIgnoreTables(seps[0], seps[1])
			}
		}
	}

	if len(*tables) > 0 && len(*tableDB) > 0 {
		subs := strings.Split(*tables, ",")
		c.AddDumpTables(*tableDB, subs...)
	} else if len(*dbs) > 0 {
		subs := strings.Split(*dbs, ",")
		c.AddDumpDatabases(subs...)
	}

	c.RegRowsEventHandler(&handler{})

	err = c.Start()
	if err != nil {
		fmt.Printf("start canal err %V", err)
		os.Exit(1)
	}

	sc := make(chan os.Signal, 1)
	signal.Notify(sc,
		os.Kill,
		os.Interrupt,
		syscall.SIGHUP,
		syscall.SIGINT,
		syscall.SIGTERM,
		syscall.SIGQUIT)

	<-sc

	c.Close()
}
func (r *River) newCanal() error {
	cfg := canal.NewDefaultConfig()
	cfg.Addr = r.c.MyAddr
	cfg.User = r.c.MyUser
	cfg.Password = r.c.MyPassword
	cfg.Flavor = r.c.Flavor
	cfg.DataDir = r.c.DataDir

	cfg.ServerID = r.c.ServerID
	cfg.Dump.ExecutionPath = r.c.DumpExec

	var err error
	r.canal, err = canal.NewCanal(cfg)
	return err
}