go canal 监听mysql数据库

郭意
2023-12-01
package main

import (
	"errors"
	"fmt"

	"github.com/go-mysql-org/go-mysql/canal"
	"github.com/siddontang/go/log"
)

type MyEventHandler struct {
	canal.DummyEventHandler
}

func (h *MyEventHandler) OnRow(e *canal.RowsEvent) error {
	log.Infof("%s %v\n", e.Action, e.Rows)
	return nil
}

func (h *MyEventHandler) String() string {
	return "MyEventHandler"
}

//github.com/go-mysql-org/go-mysql/canal
func main() {
	fmt.Println(errors.New("hi"))

	cfg := canal.NewDefaultConfig()
	cfg.Addr = "127.0.0.1:3306"
	cfg.User = "root"
	cfg.Password = "zz123456*"
	// We only care table canal_test in test db
	cfg.Dump.TableDB = "feed_back"
	cfg.Dump.Tables = []string{"application"}
	cfg.Dump.ExecutionPath = "C:\\Program Files\\MySQL\\MySQL Server 8.0\\bin\\mysqldump.exe"
	c, err := canal.NewCanal(cfg)
	if err != nil {
		log.Fatal(err)
	}

	// Register a handler to handle RowsEvent
	c.SetEventHandler(&MyEventHandler{})

	// Start canal
	c.Run()
}

 类似资料: