写点什么

Redis 与 MySQL 数据双写一致性工程落地案例,java 最新技术百度云

用户头像
极客good
关注
发布于: 刚刚

5.查看 server 日志



6.查看 instance 的日志



canal 客户端(Java 编写业务程序)


1.建 module:canal_demo


2.改 POM


<?xml version="1.0" encoding="UTF-8"?>


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"


xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">


<modelVersion>4.0.0</modelVersion>


<parent>


<groupId>org.springframework.boot</groupId>


<artifactId>spring-boot-starter-parent</artifactId>


<version>2.3.5.RELEASE</version>


</parent>


<groupId>com.zzyy.study</groupId>


<artifactId>canal_demo</artifactId>


<version>0.0.1-SNAPSHOT</version>


<name>canal_demo</name>


<description>Demo project for Spring Boot</description>


<properties>


<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>


<maven.compiler.source>1.8</maven.compiler.source>


<maven.compiler.target>1.8</maven.compiler.target>


<junit.version>4.12</junit.version>


<log4j.version>1.2.17</log4j.version>


<lombok.version>1.16.18</lombok.version>


<mysql.version>5.1.47</mysql.version>


<druid.version>1.1.16</druid.version>


<mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version>


</properties>


<dependencies>


<dependency>


<groupId>com.alibaba.otter</groupId>


<artifactId>canal.client</artifactId>


<version>1.1.0</version>


</dependency>


<dependency>


<groupId>com.google.guava</groupId>


<artifactId>guava</artifactId>


<version>23.0</version>


</dependency>


<dependency>


<groupId>org.springframework.boot</groupId>


<artifactId>spring-boot-starter-web</artifactId>


</dependency>


<dependency>


<groupId>org.springframework.boot</groupId>


<artifactId>spring-boot-starter-actuator</artifactId>


</dependency>


<dependency>


<groupId>org.springframework.boot</groupId>


<artifactId>spring-boot-starter-data-redis</artifactId>


</dependency>


<dependency>


<groupId>org.apache.commons</groupId>


<artifactId>commons-pool2</artifactId>


</dependency>


<dependency>


<groupId>redis.clients</groupId>


<artifactId>jedis</artifactId>


<version>3.1.0</version>


</dependency>


<dependency>


<groupId>org.springframework.boot</groupId>


<artifactId>spring-boot-starter-aop</artifactId>


</dependency>


<dependency>


<groupId>org.redisson</groupId>


<artifactId>redisson</artifactId>


<version>3.13.4</version>


</dependency>


<dependency>


<groupId>mysql</groupId>


<artifactId>mysql-connector-java</artifactId>


<version>5.1.47</version>


</dependency>


<dependency>


<groupId>com.alibaba</groupId>


<artifactId>druid-spring-boot-starter</artifactId>


<version>1.1.10</version>


</dependency>


<dependency>


<groupId>com.alibaba</groupId>


<artifactId>druid</artifactId>


<version>${druid.version}</version>


</dependency>


<dependency>


<groupId>com.alibaba</groupId>


<artifactId>druid</artifactId>


<version>${druid.version}</version>


</dependency>


<dependency>


<groupId>org.mybatis.spring.boot</groupId>


<artifactId>mybatis-spring-boot-starter</artifactId>


<version>${mybatis.spring.boot.version}</version>


</dependency>


<dependency>


<groupId>org.springframework.boot</groupId>


<artifactId>spring-boot-starter-amqp</artifactId>


</dependency>


<dependency>


<groupId>junit</groupId>


<artifactId>junit</artifactId>


<version>${junit.version}</version>


</dependency>


<dependency>


<groupId>org.springframework.boot</groupId>


<artifactId>spring-boot-devtools</artifactId>


<scope>runtime</scope>


<optional>true</optional>


</dependency>


<dependency>


<groupId>org.springframework.boot</groupId>


<artifactId>spring-boot-starter-test</artifactId>


<scope>test</scope>


</dependency>


<dependency>


<groupId>log4j</groupId>


<artifactId>log4j</artifactId>


<version>${log4j.version}</version>


</dependency>


<dependency>


<groupId>org.projectlombok</groupId>


<artifactId>lombok</artifactId>


<version>${lombok.version}</version>


<optional>true</optional>


</dependency>


<dependency>


<groupId>cn.hutool</groupId>


<artifactId>hutool-all</artifactId>


<version>RELEASE</version>


<scope>compile</scope>


</dependency>


<dependency>


<groupId>com.alibaba</groupId>


<artifactId>fastjson</artifactId>


<version>1.2.73</version>


</dependency>


</dependencies>


<build>


<plugins>


<plugin>


<groupId>org.springframework.boot</groupId>


<artifactId>spring-boot-maven-plugin</artifactId>


</plugin>


</plugins>


</build>


</project>


3.写 YML server.port=5555


4.业务类 RedisUtils,如下:


package com.zzyy.study.util;


import redis.clients.jedis.Jedis;


import redis.clients.jedis.JedisPool;


import redis.clients.jedis.JedisPoolConfig;


/**


  • @auther zzyy


【一线大厂Java面试题解析+核心总结学习笔记+最新架构讲解视频+实战项目源码讲义】
浏览器打开:qq.cn.hn/FTf 免费领取
复制代码


@create 2020-10-11 14:33


*/


public class RedisUtils


{


public static JedisPool jedisPool;


static {


JedisPoolConfig jedisPoolConfig=new JedisPoolConfig();


jedisPoolConfig.setMaxTotal(20);


jedisPoolConfig.setMaxIdle(10);


jedisPool=new JedisPool(jedisPoolConfig,"192.168.111.147",6379);


}


public static Jedis getJedis() throws Exception {


if(null!=jedisPool){


return jedisPool.getResource();


}


throw new Exception("Jedispool is not ok");


}


/*public static void main(String[] args) throws Exception


{


try(Jedis jedis = RedisUtils.getJedis())


{


System.out.println(jedis);


jedis.set("k1","xxx2");


String result = jedis.get("k1");


System.out.println("-----result: "+result);


System.out.println(RedisUtils.jedisPool.getNumActive());//1


}catch (Exception e){


e.printStackTrace();


}


}*/


}


RedisCanalClientExample,如下:


package com.zzyy.study.t1;


import com.alibaba.fastjson.JSONObject;


import com.alibaba.otter.canal.client.CanalConnector;


import com.alibaba.otter.canal.client.CanalConnectors;


import com.alibaba.otter.canal.protocol.CanalEntry.*;


import com.alibaba.otter.canal.protocol.Message;


import com.zzyy.study.util.RedisUtils;


import org.springframework.beans.factory.annotation.Autowired;


import redis.clients.jedis.Jedis;


import java.net.InetSocketAddress;


import java.util.List;


import java.util.concurrent.TimeUnit;


/**


  • @auther zzyy

  • @create 2020-11-11 17:13


*/


public class RedisCanalClientExample


{


public static final Integer _60SECONDS = 60;


public static void main(String args[]) {


// 创建链接 canal 服务端


CanalConnector connector = CanalConnectors.newSingleConnector(new InetSocketAddress("192.168.111.147",


11111), "example", "", "");


int batchSize = 1000;


int emptyCount = 0;


try {


connector.connect();


//connector.subscribe(".\..");


connector.subscribe("db2020.t_user");


connector.rollback();


int totalEmptyCount = 10 * _60SECONDS;


while (emptyCount < totalEmptyCount) {


Message message = connector.getWithoutAck(batchSize); // 获取指定数量的数据


long batchId = message.getId();


int size = message.getEntries().size();


if (batchId == -1 || size == 0) {


emptyCount++;


try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }


} else {


emptyCount = 0;


printEntry(message.getEntries());


}


connector.ack(batchId); // 提交确认


// connector.rollback(batchId); // 处理失败, 回滚数据


}


System.out.println("empty too many times, exit");


} finally {


connector.disconnect();


}


}


private static void printEntry(List<Entry> entrys) {


for (Entry entry : entrys) {


if (entry.getEntryType() == EntryType.TRANSACTIONBEGIN || entry.getEntryType() == EntryType.TRANSACTIONEND) {


continue;


}


RowChange rowChage = null;


try {


rowChage = RowChange.parseFrom(entry.getStoreValue());


} catch (Exception e) {

用户头像

极客good

关注

还未添加个人签名 2021.03.18 加入

还未添加个人简介

评论

发布
暂无评论
Redis与MySQL数据双写一致性工程落地案例,java最新技术百度云