Sfoglia il codice sorgente

新增分库分表 以及 广播表 单表

赵冬冬 3 anni fa
parent
commit
b41220eda2

+ 6 - 1
README.md

@@ -19,5 +19,10 @@
 
 #读写分离
     sharding-jdbc 只负责数据的路由不负责数据的同步自己做 mysql的binlog日志
-    
+#绑定表
+    取消join 产生的笛卡尔集
+    例如 order_(逻辑表)order_info_(逻辑表) id分库 user_id 水平分表 使用 user_id 进行关联  
+    则需要绑定表 
+    spring.shardingsphere.sharding.binding-tables= order_,order_info_
+
     

+ 1 - 1
src/main/java/com/xin/shardingspherejdbcdemo/config/sharding/ApplicationRunnerImpl.java

@@ -22,6 +22,6 @@ public class ApplicationRunnerImpl implements ApplicationRunner {
             System.out.print(arg + " ");
         }
         jdbcService.checkDb();
-        System.out.println();
+
     }
 }

+ 0 - 2
src/main/java/com/xin/shardingspherejdbcdemo/controller/JdbcController.java

@@ -11,8 +11,6 @@ import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.compress.utils.Lists;
 import org.apache.commons.compress.utils.Sets;
 import org.apache.ibatis.javassist.Modifier;
-import org.apache.shardingsphere.core.config.ShardingConfigurationException;
-import org.apache.shardingsphere.core.rule.DataNode;
 import org.apache.shardingsphere.core.rule.ShardingRule;
 import org.apache.shardingsphere.core.rule.TableRule;
 import org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.ShardingDataSource;

+ 16 - 0
src/main/java/com/xin/shardingspherejdbcdemo/entity/Dict.java

@@ -0,0 +1,16 @@
+package com.xin.shardingspherejdbcdemo.entity;
+
+import com.baomidou.mybatisplus.annotation.TableName;
+import lombok.Data;
+
+import java.util.Date;
+
+@TableName("t_dict")
+@Data
+public class Dict {
+    private Long id;
+    private String tName;
+    private String tKey;
+    private String tValue;
+    private Date createTime;
+}

+ 1 - 0
src/main/java/com/xin/shardingspherejdbcdemo/entity/Order.java

@@ -16,6 +16,7 @@ public class Order {
     private String orderNum;
     private BigDecimal money;
     private Integer year;
+    private String dictKey;
     @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
     private Date createTime;
 }

+ 4 - 4
src/main/java/com/xin/shardingspherejdbcdemo/entity/User.java

@@ -7,15 +7,15 @@ import lombok.Data;
 import java.util.Date;
 
 @Data
-@TableName("t_user_")
+@TableName("t_user")
 public class User {
-    /** */
+
     private Long id;
 
-    /** */
+
     private String name;
 
-    /** */
+
     private Integer age;
 
     private Integer year;

+ 9 - 0
src/main/java/com/xin/shardingspherejdbcdemo/mapper/DictMapper.java

@@ -0,0 +1,9 @@
+package com.xin.shardingspherejdbcdemo.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.xin.shardingspherejdbcdemo.entity.Dict;
+import org.apache.ibatis.annotations.Mapper;
+
+@Mapper
+public interface DictMapper extends BaseMapper<Dict> {
+}

+ 4 - 47
src/main/java/com/xin/shardingspherejdbcdemo/service/impl/JdbcServiceImpl.java

@@ -7,7 +7,7 @@ import com.xin.shardingspherejdbcdemo.utils.JdbcUtils;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.compress.utils.Sets;
 import org.apache.ibatis.javassist.Modifier;
-import org.apache.shardingsphere.core.config.ShardingConfigurationException;
+
 import org.apache.shardingsphere.core.rule.DataNode;
 import org.apache.shardingsphere.core.rule.ShardingRule;
 import org.apache.shardingsphere.core.rule.TableRule;
@@ -103,12 +103,10 @@ public class JdbcServiceImpl {
 
             }
 
-//        } catch (ShardingConfigurationException | NoSuchFieldException | IllegalAccessException | SQLException e) {
-//            log.error(String.format("逻辑表:%s 动态分表配置错误!"));
-//        }
-        } catch (Exception e) {
+        } catch (NoSuchFieldException | IllegalAccessException | SQLException e) {
             log.error(String.format("逻辑表:%s 动态分表配置错误!"));
         }
+
     }
 
 
@@ -120,7 +118,7 @@ public class JdbcServiceImpl {
         // 动态刷新:datasourceToTablesMapField
         Map<String, Collection<String>> datasourceToTablesMap = Maps.newHashMap();
         // 获取最新的DataNodes
-        List<DataNode> newDataNodesAll=new ArrayList<>();
+        List<DataNode> newDataNodesAll = new ArrayList<>();
         // 获取最新的actualTablesAll
         Set<String> actualTablesAll = Sets.newHashSet();
         // 获取最新的dataNodeIndexMap
@@ -161,47 +159,6 @@ public class JdbcServiceImpl {
         Field datasourceToTablesMapField = TableRule.class.getDeclaredField("datasourceToTablesMap");
         datasourceToTablesMapField.setAccessible(true);
         datasourceToTablesMapField.set(tableRule, datasourceToTablesMap);
-        System.out.println(tableRule);
-    }
-
-    /**
-     * 动态刷新数据源
-     */
-    private void dynamicRefreshDatasource(String dataSourceName, TableRule tableRule, List<DataNode> newDataNodes)
-            throws NoSuchFieldException, IllegalAccessException {
-        Set<String> actualTables = Sets.newHashSet();
-        Map<DataNode, Integer> dataNodeIndexMap = Maps.newHashMap();
-        AtomicInteger index = new AtomicInteger(0);
-        newDataNodes.forEach(dataNode -> {
-            actualTables.add(dataNode.getTableName());
-            if (index.intValue() == 0) {
-                dataNodeIndexMap.put(dataNode, 0);
-            } else {
-                dataNodeIndexMap.put(dataNode, index.intValue());
-            }
-            index.incrementAndGet();
-        });
-        // 动态刷新:actualDataNodesField
-        Field actualDataNodesField = TableRule.class.getDeclaredField("actualDataNodes");
-        Field modifiersField = Field.class.getDeclaredField("modifiers");
-        modifiersField.setAccessible(true);
-        modifiersField.setInt(actualDataNodesField, actualDataNodesField.getModifiers() & ~Modifier.FINAL);
-        actualDataNodesField.setAccessible(true);
-        actualDataNodesField.set(tableRule, newDataNodes);
-        // 动态刷新:actualTablesField
-        Field actualTablesField = TableRule.class.getDeclaredField("actualTables");
-        actualTablesField.setAccessible(true);
-        actualTablesField.set(tableRule, actualTables);
-        // 动态刷新:dataNodeIndexMapField
-        Field dataNodeIndexMapField = TableRule.class.getDeclaredField("dataNodeIndexMap");
-        dataNodeIndexMapField.setAccessible(true);
-        dataNodeIndexMapField.set(tableRule, dataNodeIndexMap);
-        // 动态刷新:datasourceToTablesMapField
-        Map<String, Collection<String>> datasourceToTablesMap = Maps.newHashMap();
-        datasourceToTablesMap.put(dataSourceName, actualTables);
-        Field datasourceToTablesMapField = TableRule.class.getDeclaredField("datasourceToTablesMap");
-        datasourceToTablesMapField.setAccessible(true);
-        datasourceToTablesMapField.set(tableRule, datasourceToTablesMap);
     }
 
 }

+ 47 - 3
src/main/java/com/xin/shardingspherejdbcdemo/test/MainTest.java

@@ -2,7 +2,11 @@ package com.xin.shardingspherejdbcdemo.test;
 
 
 import com.xin.shardingspherejdbcdemo.ShardingsphereJdbcStudyApplication;
+import com.xin.shardingspherejdbcdemo.config.util.SnowFlake;
+import com.xin.shardingspherejdbcdemo.entity.Dict;
+import com.xin.shardingspherejdbcdemo.mapper.DictMapper;
 import com.xin.shardingspherejdbcdemo.service.impl.ExcelServiceImpl;
+import com.xin.shardingspherejdbcdemo.utils.EntityUtils;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -11,23 +15,24 @@ import org.springframework.test.context.junit4.SpringRunner;
 
 import java.io.IOException;
 import java.text.ParseException;
+import java.util.Date;
 
 @RunWith(SpringRunner.class)
 @SpringBootTest(classes = ShardingsphereJdbcStudyApplication.class)
 public class MainTest {
 
 
-
     @Autowired
     ExcelServiceImpl excelService;
 
-//    @Test
+    //    @Test
 //    public void testBookService() throws IOException {
 //
 //        wordController.mean();
 //
 //    }
-
+    @Autowired
+    private DictMapper dictMapper;
 
     @Test
     public void testBookService() throws ParseException, IOException {
@@ -36,4 +41,43 @@ public class MainTest {
 
     }
 
+    @Test
+    public void DictAdd() {
+        //insertData(1L,"SUCCESS","成功");
+        insertData(2L,"ERROR","失败");
+    }
+
+    private void insertData(Long id,String key,String value) {
+        Dict dict = new Dict();
+        SnowFlake snowFlake = new SnowFlake(2, 3);
+        Date now = new Date();
+        int year = EntityUtils.dateTostr(now);
+        dict.setId(id);
+        dict.setTName(value);
+        dict.setTKey(key);
+        dict.setTValue(value);
+        dict.setCreateTime(now);
+        dictMapper.insert(dict);
+    }
+
+    @Test
+    public void DictUpDate() {
+        Dict dict = new Dict();
+        SnowFlake snowFlake = new SnowFlake(2, 3);
+        Date now = new Date();
+        int year = EntityUtils.dateTostr(now);
+        dict.setId(snowFlake.nextId());
+        dict.setTName("成功");
+        dict.setTKey("SUCCESS");
+        dict.setTValue("成功");
+        dict.setCreateTime(now);
+        dictMapper.updateById(dict);
+
+    }
+
+    @Test
+    public void DictDelete() {
+        dictMapper.deleteById(1L);
+    }
+
 }

+ 24 - 28
src/main/resources/application-dev.yml

@@ -15,20 +15,20 @@ mybatis:
 
 mydbinfo:
   list:
-    - shardingDbName: sharding0
+    - shardingDbName: sharding1
       ip: 47.108.115.128
       port: 3306
-      dbname: sharding0
+      dbname: sharding1
       username: root
       password: zdd755
-      tables: t_user_,t_order_
-    - shardingDbName: sharding1
+      tables: t_order_
+    - shardingDbName: sharding2
       ip: 47.108.115.128
       port: 3306
-      dbname: sharding1
+      dbname: sharding2
       username: root
       password: zdd755
-      tables: t_user_,t_order_
+      tables: t_order_
 
 spring:
   jackson:
@@ -39,7 +39,7 @@ spring:
   shardingsphere:
     datasource:
       #数据库别名
-      names: sharding0,sharding1
+      names: sharding0,sharding1,sharding2
       sharding0:
         type: com.alibaba.druid.pool.DruidDataSource
         driver-class-name: com.mysql.cj.jdbc.Driver
@@ -52,35 +52,31 @@ spring:
         url: jdbc:mysql://47.108.115.128:3306/sharding1?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowMultiQueries=true
         username: root
         password: zdd755
+      sharding2:
+        type: com.alibaba.druid.pool.DruidDataSource
+        driver-class-name: com.mysql.cj.jdbc.Driver
+        url: jdbc:mysql://47.108.115.128:3306/sharding2?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowMultiQueries=true
+        username: root
+        password: zdd755
     sharding:
       tables:
-        #逻辑表名
-        t_user_:
-          actual-data-nodes: sharding$->{0..1}.t_user_$->{2022}
-#          key-generator:
-#            column: id
-#            type: SNOWFLAKE
-          databaseStrategy:
-            inline:
-              shardingColumn: id
-              algorithmExpression: sharding${id % 2}
-          table-strategy:
-            standard:
-              sharding-column: year
-              precise-algorithm-class-name: com.xin.shardingspherejdbcdemo.config.sharding.TableShardingAlgorithm
+        #分库分表
         t_order_:
-          actual-data-nodes: sharding$->{0..1}.t_order_$->{2022}
-#          key-generator:
-#            column: id
-#            type: SNOWFLAKE
-          databaseStrategy:
+          actual-data-nodes: sharding$->{1..2}.t_order_$->{2022}
+          #分库策略 id取模
+          database-strategy:
             inline:
-              shardingColumn: id
-              algorithmExpression: sharding${id % 2}
+              sharding-column: id
+              algorithm-expression: sharding$->{(id % 2) + 1}
+          #分表策略 按年分表
           table-strategy:
             standard:
               sharding-column: year
               precise-algorithm-class-name: com.xin.shardingspherejdbcdemo.config.sharding.TableShardingAlgorithm
+      #默认数据库
+      default-data-source-name: sharding0
+      #广播规则列表
+      broadcast-tables: t_dict
     #打印sql
     props:
       sql:

+ 2 - 5
src/main/resources/mapper/UserMapper.xml

@@ -5,16 +5,13 @@
 
     <select id="selectOrderByid" resultType="java.util.Map">
         SELECT distinct
-            a.id,
-            a.name,
             b.order_num,
             b.money,
             b.create_time
         FROM
-            t_user_ a
-            LEFT JOIN t_order_ b ON a.id = b.user_id
+            t_order_ b
         WHERE
-            a.id=#{userId} order by b.create_time desc
+            b.user_id=#{userId} order by b.create_time desc
 
     </select>
 </mapper>