{"id":10020435,"date":"2024-12-04T10:04:00","date_gmt":"2024-12-04T02:04:00","guid":{"rendered":"https:\/\/itmax.cn\/?p=10020435"},"modified":"2024-12-04T10:04:00","modified_gmt":"2024-12-04T02:04:00","slug":"%e4%b8%80%e7%a7%8d%e8%bd%bb%e9%87%8f%e5%88%86%e8%a1%a8%e6%96%b9%e6%a1%88-mybatis%e6%8b%a6%e6%88%aa%e5%99%a8%e5%88%86%e8%a1%a8%e5%ae%9e%e8%b7%b5","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10020435","title":{"rendered":"\u4e00\u79cd\u8f7b\u91cf\u5206\u8868\u65b9\u6848-MyBatis\u62e6\u622a\u5668\u5206\u8868\u5b9e\u8df5"},"content":{"rendered":"<p>\u4f5c\u8005\uff1a\u4eac\u4e1c\u96f6\u552e \u5f20\u5747\u6770<\/p>\n<h2>\u80cc\u666f<\/h2>\n<p>\u90e8\u95e8\u5185\u6709\u4e00\u4e9b\u4ebf\u7ea7\u522b\u6838\u5fc3\u4e1a\u52a1\u8868\u589e\u901f\u975e\u5e38\u5feb\uff0c\u589e\u91cf\u65e5\u5747100W\uff0c\u4f46\u7ebf\u4e0a\u4e1a\u52a1\u53ea\u4f9d\u8d56\u8fd1\u4e00\u5468\u7684\u6570\u636e\u3002\u968f\u7740\u6570\u636e\u91cf\u7684\u8fc5\u901f\u589e\u957f\uff0c\u6162SQL\u9891\u53d1\uff0c\u6570\u636e\u5e93\u6027\u80fd\u4e0b\u964d\uff0c\u7cfb\u7edf\u7a33\u5b9a\u6027\u53d7\u5230\u4e25\u91cd\u5f71\u54cd\u3002\u672c\u7bc7\u6587\u7ae0\uff0c\u5c06\u5206\u4eab\u5982\u4f55\u4f7f\u7528MyBatis\u62e6\u622a\u5668\u4f4e\u6210\u672c\u7684\u63d0\u5347\u6570\u636e\u5e93\u7a33\u5b9a\u6027\u3002<\/p>\n<\/p>\n<h2>\u4e1a\u754c\u5e38\u89c1\u65b9\u6848<\/h2>\n<p>\u9488\u5bf9\u51b7\u6570\u636e\u591a\u7684\u5927\u8868\uff0c\u5e38\u7528\u7684\u7b56\u7565\u6709\u4ee52\u79cd\uff1a<\/p>\n<ol>\n<li>\u5220\u9664\/\u5f52\u6863\u65e7\u6570\u636e\u3002<\/li>\n<li>\u5206\u8868\u3002<\/li>\n<\/ol>\n<h2>\u5f52\u6863\/\u5220\u9664\u65e7\u6570\u636e<\/h2>\n<p>\u5b9a\u671f\u5c06\u51b7\u6570\u636e\u79fb\u52a8\u5230\u5f52\u6863\u8868\u6216\u8005\u51b7\u5b58\u50a8\u4e2d\uff0c\u6216\u5b9a\u671f\u5bf9\u8868\u8fdb\u884c\u5220\u9664\uff0c\u4ee5\u51cf\u5c11\u8868\u7684\u5927\u5c0f\u3002\u6b64\u7b56\u7565\u903b\u8f91\u7b80\u5355\uff0c\u53ea\u9700\u8981\u7f16\u5199\u4e00\u4e2aJOB\u5b9a\u671f\u6267\u884cSQL\u5220\u9664\u6570\u636e\u3002\u6211\u4eec\u5f00\u59cb\u4e5f\u662f\u7528\u8fd9\u79cd\u65b9\u6848\uff0c\u4f46\u6b64\u65b9\u6848\u4e5f\u6709\u4e00\u4e9b\u526f\u4f5c\u7528\uff1a<\/p>\n<p>1.\u6570\u636e\u5220\u9664\u4f1a\u5f71\u54cd\u6570\u636e\u5e93\u6027\u80fd\uff0c\u5f15\u53d1\u6162sql\uff0c\u591a\u5f20\u8868\u5e76\u884c\u5220\u9664\uff0c\u6570\u636e\u5e93\u538b\u529b\u4f1a\u66f4\u5927\u3002<\/p>\n<p>2.\u9891\u7e41\u5220\u9664\u6570\u636e\uff0c\u4f1a\u4ea7\u751f\u6570\u636e\u5e93\u788e\u7247\uff0c\u5f71\u54cd\u6570\u636e\u5e93\u6027\u80fd\uff0c\u5f15\u53d1\u6162SQL\u3002<\/p>\n<p>\u7efc\u4e0a\uff0c\u6b64\u65b9\u6848\u6709\u4e00\u5b9a\u98ce\u9669\uff0c\u4e3a\u4e86\u89c4\u907f\u8fd9\u79cd\u98ce\u9669\uff0c\u6211\u4eec\u51b3\u5b9a\u91c7\u7528\u53e6\u4e00\u79cd\u65b9\u6848\uff1a\u5206\u8868\u3002<\/p>\n<\/p>\n<h2>\u5206\u8868<\/h2>\n<p>\u6211\u4eec\u51b3\u5b9a\u6309\u65e5\u671f\u5bf9\u8868\u8fdb\u884c\u6a2a\u5411\u62c6\u5206\uff0c\u5b9e\u73b0\u8ba9\u7cfb\u7edf\u6bcf\u5468\u751f\u6210\u4e00\u5f20\u5468\u671f\u8868\uff0c\u8868\u5185\u53ea\u5b58\u8fd1\u4e00\u5468\u7684\u6570\u636e\uff0c\u89c4\u907f\u5355\u8868\u8fc7\u5927\u5e26\u6765\u7684\u98ce\u9669\u3002<\/p>\n<\/p>\n<h3>\u5206\u8868\u65b9\u6848\u9009\u578b<\/h3>\n<p>\u7ecf\u8c03\u7814\uff0c\u8003\u86512\u79cd\u5206\u8868\u65b9\u6848\uff1aSharding-JDBC\u3001\u5229\u7528Mybatis\u81ea\u5e26\u7684\u62e6\u622a\u5668\u7279\u6027\u3002<\/p>\n<p>\u7ecf\u8fc7\u5bf9\u6bd4\u540e\uff0c\u51b3\u5b9a\u91c7\u7528Mybatis\u62e6\u622a\u5668\u6765\u5b9e\u73b0\u5206\u8868\uff0c\u539f\u56e0\u5982\u4e0b\uff1a<\/p>\n<p>1.JAVA\u751f\u6001\u4e2d\u5f88\u5e38\u7528\u7684\u5206\u8868\u6846\u67b6\u662fSharding-JDBC\uff0c\u867d\u7136\u529f\u80fd\u5f3a\u5927\uff0c\u4f46\u9700\u8981\u4e00\u5b9a\u7684\u63a5\u5165\u6210\u672c\uff0c\u5e76\u4e14\u5f88\u591a\u529f\u80fd\u6682\u65f6\u7528\u4e0d\u4e0a\u3002<\/p>\n<p>2.\u7cfb\u7edf\u672c\u8eab\u5df2\u7ecf\u5728\u4f7f\u7528Mybatis\u4e86\uff0c\u53ea\u9700\u8981\u6dfb\u52a0\u4e00\u4e2amybaits\u62e6\u622a\u5668\uff0c\u628aSQL\u8868\u540d\u66ff\u6362\u4e3a\u65b0\u7684\u5468\u671f\u8868\u5c31\u53ef\u4ee5\u4e86\uff0c\u6ca1\u6709\u63a5\u5165\u65b0\u6846\u67b6\u7684\u6210\u672c\uff0c\u5f00\u53d1\u6210\u672c\u4e5f\u4e0d\u9ad8\u3002<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000045533889\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" title=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\"><\/p>\n<h3>\u5206\u8868\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801<\/h3>\n<h4>\u5206\u8868\u914d\u7f6e\u5bf9\u8c61<\/h4>\n<pre><code>import lombok.AllArgsConstructor;\nimport lombok.Data;\nimport lombok.NoArgsConstructor;\n\nimport java.util.Date;\n\n@Data\n@AllArgsConstructor\n@NoArgsConstructor\npublic class ShardingProperty {\n    \/\/ \u5206\u8868\u5468\u671f\u5929\u6570\uff0c\u914d\u7f6e7\uff0c\u5c31\u662f\u4e00\u5468\u4e00\u5206\n    private Integer days;\n    \/\/ \u5206\u8868\u5f00\u59cb\u65e5\u671f\uff0c\u9700\u8981\u7528\u8fd9\u4e2a\u65e5\u671f\u8ba1\u7b97\u5468\u671f\u8868\u540d\n    private Date beginDate;\n    \/\/ \u9700\u8981\u5206\u8868\u7684\u8868\u540d\n    private String tableName;\n}\n<\/code><\/pre>\n<h4>\u5206\u8868\u914d\u7f6e\u7c7b<\/h4>\n<pre><code>import java.util.concurrent.ConcurrentHashMap;\n\npublic class ShardingPropertyConfig {\n\n    public static final ConcurrentHashMap&lt;String, ShardingProperty&gt; SHARDING_TABLE = new ConcurrentHashMap&lt;&gt;();\n\n    static {\n        ShardingProperty orderInfoShardingConfig = new ShardingProperty(15, DateUtils.string2Date(&quot;20231117&quot;), &quot;order_info&quot;);\n        ShardingProperty userInfoShardingConfig = new ShardingProperty(7, DateUtils.string2Date(&quot;20231117&quot;), &quot;user_info&quot;);\n\n        SHARDING_TABLE.put(orderInfoShardingConfig.getTableName(), orderInfoShardingConfig);\n        SHARDING_TABLE.put(userInfoShardingConfig.getTableName(), userInfoShardingConfig);\n    }\n}\n<\/code><\/pre>\n<h4>\u62e6\u622a\u5668<\/h4>\n<pre><code>import lombok.extern.slf4j.Slf4j;\nimport o2o.aspect.platform.function.template.service.TemplateMatchService;\nimport org.apache.commons.lang3.StringUtils;\nimport org.apache.ibatis.executor.statement.StatementHandler;\nimport org.apache.ibatis.mapping.BoundSql;\nimport org.apache.ibatis.mapping.MappedStatement;\nimport org.apache.ibatis.plugin.*;\nimport org.apache.ibatis.reflection.DefaultReflectorFactory;\nimport org.apache.ibatis.reflection.MetaObject;\nimport org.apache.ibatis.reflection.ReflectorFactory;\nimport org.apache.ibatis.reflection.factory.DefaultObjectFactory;\nimport org.apache.ibatis.reflection.factory.ObjectFactory;\nimport org.apache.ibatis.reflection.wrapper.DefaultObjectWrapperFactory;\nimport org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory;\nimport org.springframework.stereotype.Component;\n\nimport java.sql.Connection;\nimport java.time.LocalDateTime;\nimport java.time.format.DateTimeFormatter;\nimport java.util.Date;\nimport java.util.Properties;\n\n@Slf4j\n@Component\n@Intercepts({@Signature(type = StatementHandler.class, method = &quot;prepare&quot;, args = {Connection.class, Integer.class})})\npublic class ShardingTableInterceptor implements Interceptor {\n    private static final ObjectFactory DEFAULT_OBJECT_FACTORY = new DefaultObjectFactory();\n    private static final ObjectWrapperFactory DEFAULT_OBJECT_WRAPPER_FACTORY = new DefaultObjectWrapperFactory();\n    private static final ReflectorFactory DEFAULT_REFLECTOR_FACTORY = new DefaultReflectorFactory();\n    private static final String MAPPED_STATEMENT = &quot;delegate.mappedStatement&quot;;\n    private static final String BOUND_SQL = &quot;delegate.boundSql&quot;;\n    private static final String ORIGIN_BOUND_SQL = &quot;delegate.boundSql.sql&quot;;\n    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern(&quot;yyyyMMdd&quot;);\n    private static final String SHARDING_MAPPER = &quot;com.jd.o2o.inviter.promote.mapper.ShardingMapper&quot;;\n\n    private ConfigUtils configUtils = SpringContextHolder.getBean(ConfigUtils.class);\n\n    @Override\n    public Object intercept(Invocation invocation) throws Throwable {\n        boolean shardingSwitch = configUtils.getBool(&quot;sharding_switch&quot;, false);\n        \/\/ \u6ca1\u5f00\u542f\u5206\u8868 \u76f4\u63a5\u8fd4\u56de\u8001\u6570\u636e\n        if (!shardingSwitch) {\n            return invocation.proceed();\n        }\n\n        StatementHandler statementHandler = (StatementHandler) invocation.getTarget();\n        MetaObject metaStatementHandler = MetaObject.forObject(statementHandler, DEFAULT_OBJECT_FACTORY, DEFAULT_OBJECT_WRAPPER_FACTORY, DEFAULT_REFLECTOR_FACTORY);\n        MappedStatement mappedStatement = (MappedStatement) metaStatementHandler.getValue(MAPPED_STATEMENT);\n        BoundSql boundSql = (BoundSql) metaStatementHandler.getValue(BOUND_SQL);\n        String originSql = (String) metaStatementHandler.getValue(ORIGIN_BOUND_SQL);\n        if (StringUtils.isBlank(originSql)) {\n            return invocation.proceed();\n        }\n\n        \/\/ \u83b7\u53d6\u8868\u540d\n        String tableName = TemplateMatchService.matchTableName(boundSql.getSql().trim());\n        ShardingProperty shardingProperty = ShardingPropertyConfig.SHARDING_TABLE.get(tableName);\n        if (shardingProperty == null) {\n            return invocation.proceed();\n        }\n\n        \/\/ \u65b0\u8868\n        String shardingTable = getCurrentShardingTable(shardingProperty, new Date());\n        String rebuildSql = boundSql.getSql().replace(shardingProperty.getTableName(), shardingTable);\n        metaStatementHandler.setValue(ORIGIN_BOUND_SQL, rebuildSql);\n        if (log.isDebugEnabled()) {\n            log.info(&quot;rebuildSQL -&gt; {}&quot;, rebuildSql);\n        }\n\n        return invocation.proceed();\n    }\n\n    @Override\n    public Object plugin(Object target) {\n        if (target instanceof StatementHandler) {\n            return Plugin.wrap(target, this);\n        }\n        return target;\n    }\n\n    @Override\n    public void setProperties(Properties properties) {}\n\n    public static String getCurrentShardingTable(ShardingProperty shardingProperty, Date createTime) {\n        String tableName = shardingProperty.getTableName();\n        Integer days = shardingProperty.getDays();\n        Date beginDate = shardingProperty.getBeginDate();\n\n        Date date;\n        if (createTime == null) {\n            date = new Date();\n        } else {\n            date = createTime;\n        }\n        if (date.before(beginDate)) {\n            return null;\n        }\n        LocalDateTime targetDate = SimpleDateFormatUtils.convertDateToLocalDateTime(date);\n        LocalDateTime startDate = SimpleDateFormatUtils.convertDateToLocalDateTime(beginDate);\n        LocalDateTime intervalStartDate = DateIntervalChecker.getIntervalStartDate(targetDate, startDate, days);\n        LocalDateTime intervalEndDate = intervalStartDate.plusDays(days - 1);\n        return tableName + &quot;_&quot; + intervalStartDate.format(FORMATTER) + &quot;_&quot; + intervalEndDate.format(FORMATTER);\n    }\n}\n<\/code><\/pre>\n<h3>\u4e34\u754c\u70b9\u6570\u636e\u4e0d\u8fde\u7eed\u95ee\u9898<\/h3>\n<p>\u5206\u8868\u65b9\u6848\u67091\u4e2a\u96be\u70b9\u9700\u8981\u89e3\u51b3\uff1a<strong>\u5468\u671f\u4e34\u754c\u70b9\u6570\u636e\u4e0d\u8fde\u7eed<\/strong>\u3002\u4e3e\u4f8b\uff1a\u5047\u8bbe\u8981\u5bf9operate\/_log\uff08\u64cd\u4f5c\u65e5\u5fd7\u8868\uff09\u5927\u8868\u8fdb\u884c\u6a2a\u5411\u5206\u8868\uff0c\u6bcf\u5468\u4e00\u5f20\u8868\uff0c\u5206\u8868\u660e\u7ec6\u53ef\u770b\u4e0b\u9762\u8868\u683c\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th><strong>\u7b2c\u4e00\u5468\uff08operate\/_log\/_20240107\/_20240108\uff09<\/strong><\/th>\n<th><strong>\u7b2c\u4e8c\u5468\uff08operate\/_log\/_20240108\/_20240114\uff09<\/strong><\/th>\n<th><strong>\u7b2c\u4e09\u5468\uff08operate\/_log\/_20240115\/_20240121\uff09<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1\u67081\u53f7 \/~ 1\u67087\u53f7\u7684\u6570\u636e<\/td>\n<td>1\u67088\u53f7 \/~ 1\u670814\u53f7\u7684\u6570\u636e<\/td>\n<td>1\u670815\u53f7 \/~ 1\u670821\u53f7\u7684\u6570\u636e<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>1\u67088\u53f7\u5c31\u662f\u5206\u8868\u4e34\u754c\u70b9\uff0c8\u53f7\u9700\u8981\u5207\u6362\u5230\u7b2c\u4e8c\u5468\u7684\u8868\uff0c\u4f468\u53f70\u70b9\u521a\u5207\u6362\u7684\u65f6\u5019\uff0c\u8868\u5185\u6ca1\u6709\u4efb\u4f55\u6570\u636e\uff0c\u8fd9\u65f6\u5982\u679c\u4e1a\u52a1\u9700\u8981\u67e5\u8fd1\u4e00\u5468\u7684\u64cd\u4f5c\u65e5\u5fd7\u662f\u67e5\u4e0d\u5230\u7684\uff0c\u8fd9\u6837\u5c31\u4f1a\u5f15\u53d1\u7ebf\u4e0a\u95ee\u9898\u3002<\/p>\n<p>\u6211\u51b3\u5b9a\u91c7\u7528\u6570\u636e\u5197\u4f59\u7684\u65b9\u5f0f\u6765\u89e3\u51b3\u8fd9\u4e2a\u75db\u70b9\u3002\u6bcf\u4e2a\u5468\u671f\u8868\u90fd\u5197\u4f59\u4e00\u4efd\u4e0a\u4e2a\u5468\u671f\u7684\u6570\u636e\uff0c\u7528\u53cc\u500d\u6570\u636e\u91cf\u5b9e\u73b0\u6570\u636e\u6ed1\u52a8\u7684\u6548\u679c\uff0c\u6548\u679c\u89c1\u4e0b\u9762\u8868\u683c\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th><strong>\u7b2c\u4e00\u5468\uff08operate\/_log\/_20240107\/_20240108\uff09<\/strong><\/th>\n<th><strong>\u7b2c\u4e8c\u5468\uff08operate\/_log\/_20240108\/_20240114\uff09<\/strong><\/th>\n<th><strong>\u7b2c\u4e09\u5468\uff08operate\/_log\/_20240115\/_20240121\uff09<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>12\u670825\u53f7 \/~ 12\u670831\u53f7\u7684\u6570\u636e<\/td>\n<td>1\u67081\u53f7 \/~ 1\u67087\u53f7\u7684\u6570\u636e<\/td>\n<td>1\u67088\u53f7 \/~ 1\u670814\u53f7\u7684\u6570\u636e<\/td>\n<\/tr>\n<tr>\n<td>1\u67081\u53f7 \/~ 1\u67087\u53f7\u7684\u6570\u636e<\/td>\n<td>1\u67088\u53f7 \/~ 1\u670814\u53f7\u7684\u6570\u636e<\/td>\n<td>1\u670815\u53f7 \/~ 1\u670821\u53f7\u7684\u6570\u636e<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u6ce8\uff1a\u8868\u683c\u5185\u7b2c\u4e00\u884c\u6570\u636e\u5c31\u662f\u5197\u4f59\u7684\u4e0a\u4e2a\u5468\u671f\u8868\u7684\u6570\u636e\u3002<\/strong><\/p>\n<p>\u601d\u8def\u6709\u4e86\uff0c\u63a5\u4e0b\u6765\u5c31\u8981\u8003\u8651\u600e\u4e48\u5b9e\u73b0<strong>\u53cc\u5199\uff08\u6570\u636e\u5197\u4f59\u5230\u4e0b\u4e2a\u5468\u671f\u8868\uff09<\/strong> ,\u67092\u79cd\u65b9\u6848\uff1a<\/p>\n<p>1.\u5728SQL\u6267\u884c\u5b8c\u6210\u8fd4\u56de\u7ed3\u679c\u524d\u6dfb\u52a0\u903b\u8f91\uff08\u53ef\u4ee5\u7528AspectJ \u6216 mybatis\u62e6\u622a\u5668\uff09\uff0c\u5982\u679cSQL\u5185\u7684\u8868\u540d\u662f\u5f53\u524d\u5468\u671f\u8868\uff0c\u5c31\u628a\u8868\u540d\u66ff\u6362\u4e3a\u4e0b\u4e2a\u5468\u671f\u8868\uff0c\u7136\u540e\u518d\u6b21\u6267\u884cSQL\u3002<strong>\u6b64\u65b9\u6848\u5bf9\u4e1a\u52a1\u5f71\u54cd\u5927\uff0c\u76f8\u5f53\u4e8e\u4e32\u884c\u6267\u884c\u4e862\u6b21SQL\uff0c\u6709\u6027\u80fd\u635f\u8017\u3002<\/strong><\/p>\n<p>2.\u76d1\u542c\u589e\u91cfbinlog\uff0c\u4eac\u4e1c\u5185\u90e8\u6709\u73b0\u6210\u7684\u6570\u636e\u8ba2\u9605\u4e2d\u95f4\u4ef6DRC\uff0c\u8bfb\u8005\u4e5f\u53ef\u4ee5\u4f7f\u7528cannal\u7b49\u5f00\u6e90\u4e2d\u95f4\u4ef6\u6765\u4ee3\u66ffDRC\uff0c\u539f\u7406\u5927\u540c\u5c0f\u5f02\uff0c\u6b64\u65b9\u6848\u5bf9\u4e1a\u52a1\u65e0\u5f71\u54cd\u3002<\/p>\n<p>\u65b9\u6848\u5bf9\u6bd4\u540e\uff0c\u9009\u62e9\u4e86\u5bf9\u4e1a\u52a1\u6027\u80fd\u635f\u8017\u5c0f\u7684\u65b9\u6848\u4e8c\u3002<\/p>\n<\/p>\n<h4>\u76d1\u542cbinlog\u5e76\u53cc\u5199\u6d41\u7a0b\u56fe<\/h4>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000045533890\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" title=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" loading=\"lazy\"><\/p>\n<h4>\u76d1\u542cbinlog\u6570\u636e\u53cc\u5199\u6ce8\u610f\u70b9<\/h4>\n<p>1.\u63d0\u524d\u4e0a\u7ebf\u76d1\u542c\u7a0b\u5e8f\uff0c\u63d0\u524d\u628a\u8001\u8868\u6570\u636e\u540c\u6b65\u5230\u65b0\u7684\u5468\u671f\u8868\u3002\u5206\u8868\u524d\u53ea\u76d1\u542c\u8001\u8868binlog\u5c31\u53ef\u4ee5\uff0c\u5206\u8868\u524d\u53ea\u9700\u8981\u628a\u8001\u8868\u6570\u636e\u540c\u6b65\u5230\u65b0\u8868\u3002<\/p>\n<p>2.\u5207\u6362\u5230\u65b0\u8868\u7684\u4e34\u754c\u70b9\uff0c\u4e3a\u4e86\u907f\u514d\u4e22\u5931\u79ef\u538b\u7684\u8001\u8868binlog\uff0c\u9700\u8981\u540c\u65f6\u5904\u7406\u65b0\u8868binlog\u548c\u8001\u8868binlog\uff0c\u8fd9\u6837\u4f1a\u51fa\u73b0\u6b7b\u5faa\u73af\u540c\u6b65\u7684\u95ee\u9898\uff0c\u56e0\u4e3a\u8001\u8868\u9700\u8981\u540c\u6b65\u65b0\u8868\uff0c\u65b0\u8868\u53c8\u9700\u8981\u53cc\u5199\u8001\u8868\u3002\u4e3a\u4e86\u6253\u7834\u5faa\u73af\uff0c\u9700\u8981\u5148\u628a\u53cc\u5199\u8001\u8868\u6d88\u8d39\u5835\u4e0a\u8ba9\u6d88\u606f\u6682\u65f6\u79ef\u538b\uff0c\u5207\u6362\u65b0\u8868\u6210\u529f\u540e\uff0c\u518d\u6253\u5f00\u53cc\u5199\u6d88\u8d39\u3002<\/p>\n<\/p>\n<h4>\u76d1\u542cbinlog\u6570\u636e\u53cc\u5199\u4ee3\u7801<\/h4>\n<p><strong>\u6ce8\uff1a\u4e0b\u9762\u4ee3\u7801\u4e0d\u80fd\u76f4\u63a5\u7528\uff0c\u53ea\u63d0\u4f9b\u57fa\u672c\u601d\u8def<\/strong><\/p>\n<pre><code>\/**\n * \u76d1\u542cbinlog \uff0c\u5206\u8868\u53cc\u5199\uff0c\u89e3\u51b3\u6570\u636e\u4e34\u754c\u95ee\u9898\n*\/\n@Slf4j\n@Component\npublic class BinLogConsumer implements MessageListener {\n    \n    private MessageDeserialize deserialize = new JMQMessageDeserialize();\n\n    private static final String TABLE_PLACEHOLDER = &quot;%TABLE%&quot;;\n\n    @Value(&quot;${mq.doubleWriteTopic.topic}&quot;)\n    private String doubleWriteTopic;\n\n    @Autowired\n    private JmqProducerService jmqProducerService;\n\n\n    @Override\n    public void onMessage(List&lt;Message&gt; messages) throws Exception {\n        if (messages == null || messages.isEmpty()) {\n            return;\n        }\n        List&lt;EntryMessage&gt; entryMessages = deserialize.deserialize(messages);\n        for (EntryMessage entryMessage : entryMessages) {\n            try {\n                syncData(entryMessage);\n            } catch (Exception e) {\n                log.error(&quot;sharding sync data error&quot;, e);\n                throw e;\n            }\n        }\n    }\n\n    private void syncData(EntryMessage entryMessage) throws JMQException {\n        \/\/ \u6839\u636ebinlog\u5185\u7684\u8868\u540d\uff0c\u83b7\u53d6\u9700\u8981\u540c\u6b65\u7684\u8868\n        \/\/ 3\u79cd\u60c5\u51b5\uff1a\n        \/\/ 1\u3001\u8001\u8868\uff1a\u9700\u8981\u540c\u6b65\u5f53\u524d\u5468\u671f\u8868\uff0c\u548c\u4e0b\u4e2a\u5468\u671f\u8868\u3002\n        \/\/ 2\u3001\u5f53\u524d\u5468\u671f\u8868\uff1a\u9700\u8981\u540c\u6b65\u4e0b\u4e2a\u5468\u671f\u8868\uff0c\u548c\u8001\u8868\u3002\n        \/\/ 3\u3001\u4e0b\u4e2a\u5468\u671f\u8868\uff1a\u4e0d\u9700\u8981\u540c\u6b65\u3002\n        List&lt;String&gt; syncTables = getSyncTables(entryMessage.tableName, entryMessage.createTime);\n        \n        if (CollectionUtils.isEmpty(syncTables)) {\n            log.info(&quot;table {} is not need sync&quot;, tableName);\n            return;\n        }\n\n        if (entryMessage.getHeader().getEventType() == WaveEntry.EventType.INSERT) {\n            String insertTableSqlTemplate = parseSqlForInsert(rowData);\n            for (String syncTable : syncTables) {\n                String insertSql = insertTableSqlTemplate.replaceAll(TABLE_PLACEHOLDER, syncTable);\n                \/\/ \u53cc\u5199\u8001\u8868\u53d1Q\uff0c\u4e3a\u4e86\u907f\u514d\u51fa\u73b0\u540c\u6b65\u6b7b\u5faa\u73af\u95ee\u9898\n                if (ShardingPropertyConfig.SHARDING_TABLE.containsKey(syncTable)) {\n                    Long primaryKey = getPrimaryKey(rowData.getAfterColumnsList());\n                    sendDoubleWriteMsg(insertSql, primaryKey);\n                    continue;\n                }\n                mysqlConnection.executeSql(insertSql);\n            }\n            continue;\n        }\n    }\n<\/code><\/pre>\n<h2>\u6570\u636e\u5bf9\u6bd4<\/h2>\n<p>\u4e3a\u4e86\u4fdd\u8bc1\u65b0\u8868\u548c\u8001\u8868\u6570\u636e\u4e00\u81f4\uff0c\u9700\u8981\u7f16\u5199\u5bf9\u6bd4\u7a0b\u5e8f\uff0c\u5728\u4e0a\u7ebf\u524d\u8fdb\u884c\u6570\u636e\u5bf9\u6bd4\uff0c\u4fdd\u8bc1binlog\u540c\u6b65\u65e0\u95ee\u9898\u3002<\/p>\n<p>\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\u4e0d\u505a\u5c55\u793a\uff0c\u601d\u8def\uff1a\u65b0\u8868\u67e5\u8be2\u4e00\u5b9a\u91cf\u7ea7\u6570\u636e\uff0c\u8001\u8868\u67e5\u8be2\u76f8\u540c\u91cf\u7ea7\u6570\u636e\uff0c\u90fd\u8f6c\u6362\u6210JSON\uff0cequals\u5bf9\u6bd4\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000045533887\">https:\/\/segmentfault.com\/a\/1190000045533887<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f5c\u8005\uff1a\u4eac\u4e1c\u96f6\u552e \u5f20\u5747\u6770 \u80cc\u666f \u90e8\u95e8\u5185\u6709\u4e00\u4e9b\u4ebf\u7ea7\u522b\u6838\u5fc3\u4e1a\u52a1\u8868\u589e\u901f\u975e\u5e38\u5feb\uff0c\u589e\u91cf\u65e5\u5747100W\uff0c\u4f46\u7ebf\u4e0a\u4e1a\u52a1\u53ea\u4f9d\u8d56\u8fd1\u4e00\u5468 [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10020435","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020435","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10020435"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020435\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10020435"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10020435"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10020435"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}