统一Canal 依赖的Spring 版本,升级ibatis 至mybatis (#3182)

* fix issue:#561 增加一个外部参数控制,允许位点不存在时自动重置到当前位点

* Add MariaDB GTID support

* 1. fix maven central repository url access error.(Update to https)
2. upgrade spring version to 5.0.5.RELEASE
3. upgrade ibatis to mybatis

* fix unit test run error
This commit is contained in:
文哥
2020-11-20 09:24:56 +08:00
committed by GitHub
parent a5c09c521e
commit 20add85ff6
27 changed files with 207 additions and 176 deletions

View File

@@ -54,8 +54,12 @@
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.apache.ibatis</groupId>
<artifactId>ibatis-sqlmap</artifactId>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>

View File

@@ -9,14 +9,14 @@ import javax.sql.DataSource;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.mybatis.spring.support.SqlSessionDaoSupport;
/**
* @author agapple 2017年10月14日 上午1:05:22
* @since 1.0.25
*/
@SuppressWarnings("deprecation")
public class MetaBaseDAO extends SqlMapClientDaoSupport {
public class MetaBaseDAO extends SqlSessionDaoSupport {
protected boolean isH2 = false;
@@ -24,7 +24,7 @@ public class MetaBaseDAO extends SqlMapClientDaoSupport {
Connection conn = null;
InputStream input = null;
try {
DataSource dataSource = getDataSource();
DataSource dataSource = getSqlSessionFactory().getConfiguration().getEnvironment().getDataSource();
conn = dataSource.getConnection();
String name = "mysql";
isH2 = isH2(conn);

View File

@@ -15,7 +15,7 @@ import com.google.common.collect.Maps;
public class MetaHistoryDAO extends MetaBaseDAO {
public Long insert(MetaHistoryDO metaDO) {
return (Long) getSqlMapClientTemplate().insert("meta_history.insert", metaDO);
return getSqlSession().getMapper(MetaHistoryMapper.class).insert(metaDO);
}
public List<MetaHistoryDO> findByTimestamp(String destination, Long snapshotTimestamp, Long timestamp) {
@@ -23,13 +23,13 @@ public class MetaHistoryDAO extends MetaBaseDAO {
params.put("destination", destination);
params.put("snapshotTimestamp", snapshotTimestamp == null ? 0L : snapshotTimestamp);
params.put("timestamp", timestamp == null ? 0L : timestamp);
return (List<MetaHistoryDO>) getSqlMapClientTemplate().queryForList("meta_history.findByTimestamp", params);
return getSqlSession().getMapper(MetaHistoryMapper.class).findByTimestamp(params);
}
public Integer deleteByName(String destination) {
HashMap params = Maps.newHashMapWithExpectedSize(2);
params.put("destination", destination);
return getSqlMapClientTemplate().delete("meta_history.deleteByName", params);
return getSqlSession().getMapper(MetaHistoryMapper.class).deleteByName(params);
}
/**
@@ -40,7 +40,7 @@ public class MetaHistoryDAO extends MetaBaseDAO {
long timestamp = System.currentTimeMillis() - interval * 1000;
params.put("timestamp", timestamp);
params.put("destination", destination);
return getSqlMapClientTemplate().delete("meta_history.deleteByTimestamp", params);
return getSqlSession().getMapper(MetaHistoryMapper.class).deleteByTimestamp(params);
}
protected void initDao() throws Exception {

View File

@@ -0,0 +1,15 @@
package com.alibaba.otter.canal.parse.inbound.mysql.tsdb.dao;
import java.util.List;
import java.util.Map;
public interface MetaHistoryMapper {
List<MetaHistoryDO> findByTimestamp(Map<String, Object> map);
long insert(MetaHistoryDO metaHistoryDO);
int deleteByName(Map<String, Object> map);
int deleteByTimestamp(Map<String, Object> map);
}

View File

@@ -6,7 +6,7 @@ import com.google.common.collect.Maps;
/**
* canal数据的存储
*
*
* @author wanshao 2017年7月27日 下午10:51:55
* @since 3.2.5
*/
@@ -14,25 +14,24 @@ import com.google.common.collect.Maps;
public class MetaSnapshotDAO extends MetaBaseDAO {
public Long insert(MetaSnapshotDO snapshotDO) {
return (Long) getSqlMapClientTemplate().insert("meta_snapshot.insert", snapshotDO);
return getSqlSession().getMapper(MetaSnapshotMapper.class).insert(snapshotDO);
}
public Long update(MetaSnapshotDO snapshotDO) {
return (Long) getSqlMapClientTemplate().insert("meta_snapshot.update", snapshotDO);
return getSqlSession().getMapper(MetaSnapshotMapper.class).update(snapshotDO);
}
public MetaSnapshotDO findByTimestamp(String destination, Long timestamp) {
HashMap params = Maps.newHashMapWithExpectedSize(2);
params.put("timestamp", timestamp == null ? 0L : timestamp);
params.put("destination", destination);
return (MetaSnapshotDO) getSqlMapClientTemplate().queryForObject("meta_snapshot.findByTimestamp", params);
return getSqlSession().getMapper(MetaSnapshotMapper.class).findByTimestamp(params);
}
public Integer deleteByName(String destination) {
HashMap params = Maps.newHashMapWithExpectedSize(2);
params.put("destination", destination);
return getSqlMapClientTemplate().delete("meta_snapshot.deleteByName", params);
return getSqlSession().getMapper(MetaSnapshotMapper.class).deleteByName(params);
}
/**
@@ -43,7 +42,7 @@ public class MetaSnapshotDAO extends MetaBaseDAO {
long timestamp = System.currentTimeMillis() - interval * 1000;
params.put("timestamp", timestamp);
params.put("destination", destination);
return getSqlMapClientTemplate().delete("meta_snapshot.deleteByTimestamp", params);
return getSqlSession().getMapper(MetaSnapshotMapper.class).deleteByTimestamp(params);
}
protected void initDao() throws Exception {

View File

@@ -0,0 +1,16 @@
package com.alibaba.otter.canal.parse.inbound.mysql.tsdb.dao;
import java.util.Map;
public interface MetaSnapshotMapper {
MetaSnapshotDO findByTimestamp(Map<String, Object> map);
long insert(MetaSnapshotDO metaSnapshotDO);
long update(MetaSnapshotDO metaSnapshotDO);
int deleteByName(Map<String, Object> map);
int deleteByTimestamp(Map<String, Object> map);
}

View File

@@ -29,16 +29,16 @@
<property name="useUnfairLock" value="true" />
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:tsdb/sql-map/sqlmap-config.xml"/>
</bean>
<bean id="metaHistoryDAO" class="com.alibaba.otter.canal.parse.inbound.mysql.tsdb.dao.MetaHistoryDAO">
<property name="sqlMapClient" ref="sqlMapClient"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<bean id="metaSnapshotDAO" class="com.alibaba.otter.canal.parse.inbound.mysql.tsdb.dao.MetaSnapshotDAO">
<property name="sqlMapClient" ref="sqlMapClient"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>

View File

@@ -29,16 +29,16 @@
<property name="useUnfairLock" value="true" />
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:tsdb/sql-map/sqlmap-config.xml"/>
</bean>
<bean id="metaHistoryDAO" class="com.alibaba.otter.canal.parse.inbound.mysql.tsdb.dao.MetaHistoryDAO">
<property name="sqlMapClient" ref="sqlMapClient"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<bean id="metaSnapshotDAO" class="com.alibaba.otter.canal.parse.inbound.mysql.tsdb.dao.MetaSnapshotDAO">
<property name="sqlMapClient" ref="sqlMapClient"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>

View File

@@ -32,16 +32,16 @@
<property name="useUnfairLock" value="true" />
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:tsdb/sql-map/sqlmap-config.xml"/>
</bean>
<bean id="metaHistoryDAO" class="com.alibaba.otter.canal.parse.inbound.mysql.tsdb.dao.MetaHistoryDAO">
<property name="sqlMapClient" ref="sqlMapClient"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<bean id="metaSnapshotDAO" class="com.alibaba.otter.canal.parse.inbound.mysql.tsdb.dao.MetaSnapshotDAO">
<property name="sqlMapClient" ref="sqlMapClient"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>

View File

@@ -1,8 +1,14 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings useStatementNamespaces="true"/>
<sqlMap resource="tsdb/sql-map/sqlmap_history.xml"/>
<sqlMap resource="tsdb/sql-map/sqlmap_snapshot.xml"/>
</sqlMapConfig>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAlias alias="metaHistoryDO" type="com.alibaba.otter.canal.parse.inbound.mysql.tsdb.dao.MetaHistoryDO"/>
<typeAlias alias="metaSnapshotDO" type="com.alibaba.otter.canal.parse.inbound.mysql.tsdb.dao.MetaSnapshotDO"/>
</typeAliases>
<mappers>
<mapper resource="tsdb/sql-map/sqlmap_history.xml"/>
<mapper resource="tsdb/sql-map/sqlmap_snapshot.xml"/>
</mappers>
</configuration>

View File

@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >
<sqlMap namespace="meta_history">
<typeAlias alias="metaHistoryDO" type="com.alibaba.otter.canal.parse.inbound.mysql.tsdb.dao.MetaHistoryDO"/>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.alibaba.otter.canal.parse.inbound.mysql.tsdb.dao.MetaHistoryMapper">
<sql id="allColumns">
<![CDATA[
gmt_create,gmt_modified,destination,binlog_file,binlog_offest,binlog_master_id,binlog_timestamp,use_schema,sql_schema,sql_table,sql_text,sql_type,extra
@@ -15,31 +14,31 @@
]]>
</sql>
<select id="findByTimestamp" parameterClass="java.util.Map" resultClass="metaHistoryDO">
<select id="findByTimestamp" parameterType="java.util.Map" resultType="metaHistoryDO">
select
<include refid="allVOColumns"/>
from meta_history a
<![CDATA[
where destination = #destination# and binlog_timestamp >= #snapshotTimestamp# and binlog_timestamp <= #timestamp#
where destination = #{destination} and binlog_timestamp >= #{snapshotTimestamp} and binlog_timestamp <= #{timestamp}
order by binlog_timestamp asc,id asc
]]>
</select>
<insert id="insert" parameterClass="metaHistoryDO">
<insert id="insert" parameterType="metaHistoryDO">
insert into meta_history (<include refid="allColumns"/>)
values(CURRENT_TIMESTAMP,CURRENT_TIMESTAMP,#destination#,#binlogFile#,#binlogOffest#,#binlogMasterId#,#binlogTimestamp#,#useSchema#,#sqlSchema#,#sqlTable#,#sqlText#,#sqlType#,#extra#)
values(CURRENT_TIMESTAMP,CURRENT_TIMESTAMP,#{destination},#{binlogFile},#{binlogOffest},#{binlogMasterId},#{binlogTimestamp},#{useSchema},#{sqlSchema},#{sqlTable},#{sqlText},#{sqlType},#{extra})
</insert>
<delete id="deleteByName" parameterClass="java.util.Map">
delete from meta_history
where destination=#destination#
<delete id="deleteByName" parameterType="java.util.Map">
delete from meta_history
where destination=#{destination}
</delete>
<delete id="deleteByTimestamp" parameterClass="java.util.Map">
<delete id="deleteByTimestamp" parameterType="java.util.Map">
<![CDATA[
delete from meta_history
where destination=#destination# and binlog_timestamp < #timestamp#
where destination=#{destination} and binlog_timestamp < #{timestamp}
]]>
</delete>
</sqlMap>
</mapper>

View File

@@ -1,9 +1,6 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >
<sqlMap namespace="meta_snapshot">
<typeAlias alias="metaSnapshotDO" type="com.alibaba.otter.canal.parse.inbound.mysql.tsdb.dao.MetaSnapshotDO"/>
<typeAlias alias="tableMetaSnapshotDO"
type="com.alibaba.middleware.jingwei.biz.dataobject.CanalTableMetaSnapshotDO"/>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.alibaba.otter.canal.parse.inbound.mysql.tsdb.dao.MetaSnapshotMapper">
<sql id="allColumns">
<![CDATA[
gmt_create,gmt_modified,destination,binlog_file,binlog_offest,binlog_master_id,binlog_timestamp,data,extra
@@ -16,48 +13,36 @@
]]>
</sql>
<select id="findByTimestamp" parameterClass="java.util.Map" resultClass="metaSnapshotDO">
select <include refid="allVOColumns"/>
<![CDATA[
<select id="findByTimestamp" parameterType="java.util.Map" resultType="metaSnapshotDO">
select <include refid="allVOColumns"/>
<![CDATA[
from meta_snapshot a
where destination = #destination# and binlog_timestamp < #timestamp#
where destination = #{destination} and binlog_timestamp < #{timestamp}
order by binlog_timestamp desc,id desc
limit 1
]]>
</select>
<select id="findByTimestampOnDerby" parameterClass="java.util.Map" resultClass="metaSnapshotDO">
select * FROM (
select ROW_NUMBER() OVER() AS rownum, <include refid="allVOColumns"/>
<![CDATA[
from meta_snapshot a
where destination = #destination# and binlog_timestamp < #timestamp#
order by binlog_timestamp desc,id desc
) AS tmp
WHERE rownum <= 5
]]>
</select>
<insert id="insert" parameterClass="metaSnapshotDO">
<insert id="insert" parameterType="metaSnapshotDO">
insert into meta_snapshot (<include refid="allColumns"/>)
values(CURRENT_TIMESTAMP,CURRENT_TIMESTAMP,#destination#,#binlogFile#,#binlogOffest#,#binlogMasterId#,#binlogTimestamp#,#data#,#extra#)
values(CURRENT_TIMESTAMP,CURRENT_TIMESTAMP,#{destination},#{binlogFile},#{binlogOffest},#{binlogMasterId},#{binlogTimestamp},#{data},#{extra})
</insert>
<update id="update" parameterClass="metaSnapshotDO">
<update id="update" parameterType="metaSnapshotDO">
update meta_snapshot set gmt_modified=now(),
binlog_file=#binlogFile#,binlog_offest=#binlogOffest#,binlog_master_id=#binlogMasterId#,binlog_timestamp=#binlogTimestamp#,data=#data#,extra=#extra#
where destination=#destination# and binlog_timestamp=0
binlog_file=#{binlogFile},binlog_offest=#{binlogOffest},binlog_master_id=#{binlogMasterId},binlog_timestamp=#{binlogTimestamp},data=#{data},extra=#{extra}
where destination=#{destination} and binlog_timestamp=0
</update>
<delete id="deleteByName" parameterClass="java.util.Map">
delete from meta_snapshot
where destination=#destination#
<delete id="deleteByName" parameterType="java.util.Map">
delete from meta_snapshot
where destination=#{destination}
</delete>
<delete id="deleteByTimestamp" parameterClass="java.util.Map">
<delete id="deleteByTimestamp" parameterType="java.util.Map">
<![CDATA[
delete from meta_snapshot
where destination=#destination# and binlog_timestamp < #timestamp# and binlog_timestamp > 0
where destination=#{destination} and binlog_timestamp < #{timestamp} and binlog_timestamp > 0
]]>
</delete>
</sqlMap>
</mapper>