1: 安装docker
2: docker下载mysql镜像(这里装的是mysql 8.0.24)
3: docker下载canal镜像
安装结果如下:
安装步骤非常简单:
docker pull mysql:8.0.24
docker pull canal/canal-server
4: 启动mysql容器
启动
docker run -d --name mysql -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 mysql:8.0.24 配置访问: ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456'; ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456'; FLUSH PRIVILEGES; 创建用户canal: CREATE USER canal IDENTIFIED BY 'canal'; GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'canal'@'%'; ALTER USER 'canal'@'%' IDENTIFIED WITH mysql_native_password BY '123456'; -- GRANT ALL PRIVILEGES ON *.* TO 'canal'@'%' ; FLUSH PRIVILEGES;
5: 启动canal容器
启动容器
docker run -p 11111:11111 --name canal -d canal/canal-server
修改配置
进入容器:
docker exec -it canal /bin/bash
cd /home/admin/canal-server/conf/example
vi instance.properties
# position info (这个ip需要修改为云服务器的ip加端口)
canal.instance.master.address=47.115.24.184:3306
# username/password(这个地方需要修改为mysql新建的用户密码)
canal.instance.dbUsername=canal
canal.instance.dbPassword=123456
canal.instance.connectionCharset = UTF-8
# table regex (监听的表名: 这里使用了默认监听全部)
canal.instance.filter.regex=.*\\..*
cd /home/admin/canal-server/conf
vi canal.properties
# tcp bind ip (仅仅新增了一个canal.id=123)
canal.id=123
canal.ip =
# register ip to zookeeper
6: 这里是用了阿里云服务器, 需要在安全配置那开放端口.
7: 示例工程:
application.yml:
canal: server: 云服务器ip:11111 destination: example spring: application: name: canal-example datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://云服务器ip:3306/shop_goods?useSSL=false username: canal password: 123456 jooq: sql-dialect: mysql flyway: baseline-on-migrate: true
主要依赖:
top.javatool canal-spring-boot-starter 1.2.1-RELEASE org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-jooq mysql mysql-connector-java 8.0.21 org.flywaydb flyway-core org.springframework.kafka spring-kafka
@Component @CanalTable(value = "t_user") public class UserHandler implements EntryHandler{ private Logger logger = LoggerFactory.getLogger(UserHandler.class); @Override public void insert(User user) { logger.info("insert message {}", user); } @Override public void update(User before, User after) { logger.info("update before {} ", before); logger.info("update after {}", after); } @Override public void delete(User user) { logger.info("delete {}", user); } }
@SpringBootApplication public class CanalExampleApplication { public static void main(String[] args) { SpringApplication.run(CanalExampleApplication.class, args); } }
@Table(name = "t_user") public class User implements Serializable { /** * 主键 */ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; /** * 用户名 */ @Column(name = "user_name") private String userName; /** * 用户性别 */ private Integer gender; /** * 国家id */ @Column(name = "country_id") private Integer countryId; /** * 用户出生日期 */ private Date birthday; /** * 用户创建时间 */ @Column(name = "create_time") private Date createTime; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public Integer getGender() { return gender; } public void setGender(Integer gender) { this.gender = gender; } public Integer getCountryId() { return countryId; } public void setCountryId(Integer countryId) { this.countryId = countryId; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } @Override public String toString() { final StringBuilder sb = new StringBuilder("User{"); sb.append("id=").append(id); sb.append(", userName='").append(userName).append('\''); sb.append(", gender=").append(gender); sb.append(", countryId=").append(countryId); sb.append(", birthday=").append(birthday); sb.append(", createTime=").append(createTime); sb.append('}'); return sb.toString(); } }
然后启动工程,
修改数据库的表:
@CanalTable(value = "t_user") 这个是监听的表结构
t_user
然后会触发相应的方法!