属性名 | 类型 | 说明 |
id | String | 集群标识 |
userName | String | 连接集群时的用户名 |
Password | String | 连接集群时的密码 |
dataSources | List<DataSourceConfig> | 集群中需要访问的数据源列表 |
partitions | List<Partition>; | 集群中包含的分区列表 |
属性名 | 类型 | 说明 |
id | String | 分区标识 |
mode | int | 分区类型,可以是主从,也可以是分表 |
Password | String | 连接集群时的密码 |
shards | List<Shard> | 分区中包含的分片列表 |
partitionRules | List<PartitionRule> | 分区规则,当进行处理的时候,路由到哪个分区执行 |
属性名 | 类型 | 说明 |
id | String | 分区标识 |
dataSourceId | String | 实际访问的数据库配置ID |
readWeight | int | 读权重,仅用于主从读写分离模式 |
writeWeight | int | 写权重,仅用于主从读写分离模式 |
shardRules | List<ShardRule> | 分片规则,当进行处理的时候,路由到哪个分片执行,仅用于分模式 |
tableMappings | List<TableMapping>; | 表名映射列表,仅用于同库不同表名分表模式 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
/** * 分布式Key获取器 * * @param <T> */ public interface ClusterKeyGenerator<T> { T getKey(String tableName); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
public interface ClusterManager { /** * 返回是否是分片语句 * * @param partition * @param sql * @return */ boolean isShardSql(Partition partition, String sql); /** * 添加语句处理器 * * @param statementProcessor */ void addStatementProcessor(StatementProcessor statementProcessor); /** * 返回语句处理器列表 * * @return */ List<StatementProcessor> getStatementProcessorList(); /** * 给某个集群的数据表产生主键 * * @param cluster * @param tableName * @param <T> * @return */ < T> T getPrimaryKey(Cluster cluster, String tableName); /** * 返回SQL对应的Statement * * @param sql * @return */ Statement getSqlStatement(String sql); /** * 添加集群 * * @param cluster */ void addCluster(Cluster cluster); /** * 获取集群 * * @param clusterId * @return */ Cluster getCluster(String clusterId); /** * 返回某个分区与sql是否匹配 * * @param partition * @param sql * @return */ boolean isMatch(Partition partition, String sql); /** * 返回某个分片是否匹配 * * @param shard * @param sql * @return */ boolean isMatch(Partition partition, Shard shard, String sql); /** * 返回分片执行语句 * * @param partition * @param shard * @param sql * @return */ String getSql(Partition partition, Shard shard, String sql); /** * 获取匹配的分区<br> * * @param clusterId * @param sql * @return */ Collection<Partition> getPartitions(String clusterId, String sql); /** * 获取匹配的首个分区 * * @param clusterId * @param sql * @return */ Partition getPartition(String clusterId, String sql); /** * 获取匹配的首个分区 * * @param cluster * @param sql * @return */ Partition getPartition(Cluster cluster, String sql); /** * 获取匹配的分区 * * @param cluster * @param sql * @return */ List<Partition> getPartitions(Cluster cluster, String sql); /** * 获取匹配的分片 * * @param partition * @param sql * @return */ List<Shard> getShards(Partition partition, String sql); /** * 返回分片均衡器 * * @return */ ShardBalance getShardBalance(); /** * 设置分片均衡器 * * @param balance */ void setShardBalance(ShardBalance balance); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
/** * 分区规则接口<br> * 规则参数在实现类中定义 * */ public interface PartitionRule { /** * 返回是否命中,如果有多个命中,则只用第一个进行处理 * * @param sql * @return */ boolean isMatch(String sql); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
/** * 分片规则 * */ public interface ShardRule { /** * 返回是否属于当前分片处理 * * @param sql * @return */ boolean isMatch(Partition partition, String sql); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
/** * 用于对SQL进行特殊处理并进行结果合并等<br> * <p/> * 比如sql语句是select count(*) from abc<br> * 则会到所有的shard执行,并对结果相加后返回 * */ public interface StatementProcessor { /** * 返回是否由此SQL处理器进行处理 * * @param sql * @return */ boolean isMatch(String sql); /** * 返回处理器转换过之后的SQL * * @param sql * @return */ String getSql(String sql); /** * 对结果进行合并 * * @param results * @return * @throws SQLException */ ResultSet combineResult(List<ResultSet> results) throws SQLException; } |
1 2 3 4 5 6 7 |
Class.forName("org.tinygroup.dbcluster.jdbc.TinyDriver"); Connection conn = DriverManager.getConnection("jdbc:dbcluster://cluster1", "username", "password"); Statement stmt = conn.createStatement(); stmt.execute(“select * from aaa”); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
CREATE TABLE `aaa0` ( `id` int(11) NOT NULL, `aaa` varchar(45) DEFAULT NULL, PRIMARY KEY (`id`) ); CREATE TABLE `aaa1` ( `id` int(11) NOT NULL, `aaa` varchar(45) DEFAULT NULL, PRIMARY KEY (`id`) ); CREATE TABLE `aaa2` ( `id` int(11) NOT NULL, `aaa` varchar(45) DEFAULT NULL, PRIMARY KEY (`id`) ); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public static void main(String[] args) throws Throwable { Class.forName("org.tinygroup.dbcluster.jdbc.TinyDriver"); Connection conn = DriverManager.getConnection("jdbc:dbcluster://cluster1", "username", "password"); Statement stmt = conn.createStatement(); String sql; //插入100条数据 for (int i = 0; i < 100; i++) { sql = "insert into aaa(id,aaa) values (" + clusterManager.getPrimaryKey(cluster, "aaa") + ",'ppp')"; boolean result = stmt.execute(sql); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Using shard:shard1 to execute sql:insert into aaa(id,aaa) values (1,'ppp') Using shard:shard2 to execute sql:insert into aaa(id,aaa) values (2,'ppp') Using shard:shard0 to execute sql:insert into aaa(id,aaa) values (3,'ppp') Using shard:shard1 to execute sql:insert into aaa(id,aaa) values (4,'ppp') Using shard:shard2 to execute sql:insert into aaa(id,aaa) values (5,'ppp') Using shard:shard0 to execute sql:insert into aaa(id,aaa) values (6,'ppp') ……. |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public static void main(String[] args) throws Throwable { Class.forName("org.tinygroup.dbcluster.jdbc.TinyDriver"); Connection conn = DriverManager.getConnection("jdbc:dbcluster://cluster1", "username", "password"); Statement stmt = conn.createStatement(); String sql = "select * from aaa order by id"; ResultSet resultSet = stmt.executeQuery(sql); while (resultSet.next()) { System.out.printf(" id: %d, aaa: %s \n", resultSet.getInt(1), resultSet.getString(2)); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Using shard:shard0 to execute sql:select * from aaa order by id Using shard:shard1 to execute sql:select * from aaa order by id Using shard:shard2 to execute sql:select * from aaa order by id id: 1, aaa: ppp id: 2, aaa: ppp id: 3, aaa: ppp id: 4, aaa: ppp id: 5, aaa: ppp id: 6, aaa: ppp …… |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public static void main(String[] args) throws Throwable { Class.forName("org.tinygroup.dbcluster.jdbc.TinyDriver"); Connection conn = DriverManager.getConnection("jdbc:dbcluster://cluster1", " username ", "password"); Statement stmt = conn.createStatement(); String sql = "delete from aaa"; stmt.execute(sql); } |
1 2 3 4 5 |
Using shard:shard0 to execute sql:delete from aaa Using shard:shard1 to execute sql:delete from aaa Using shard:shard2 to execute sql:delete from aaa |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
CREATE TABLE test0. aaa ( `id` int(11) NOT NULL, `aaa` varchar(45) DEFAULT NULL, PRIMARY KEY (`id`) ); CREATE TABLE test1. aaa( `id` int(11) NOT NULL, `aaa` varchar(45) DEFAULT NULL, PRIMARY KEY (`id`) ); CREATE TABLE test2. aaa( `id` int(11) NOT NULL, `aaa` varchar(45) DEFAULT NULL, PRIMARY KEY (`id`) ); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public static void main(String[] args) throws Throwable { Class.forName("org.tinygroup.dbcluster.jdbc.TinyDriver"); Connection conn = DriverManager.getConnection("jdbc:dbcluster://cluster1", "username", "password"); Statement stmt = conn.createStatement(); for (int i = 1; i <= 100; i++) { boolean result = stmt.execute(“select * from aaa”); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
Using shard:shard3 to execute sql:select * from aaa Using shard:shard2 to execute sql:select * from aaa Using shard:shard1 to execute sql:select * from aaa Using shard:shard1 to execute sql:select * from aaa Using shard:shard1 to execute sql:select * from aaa Using shard:shard1 to execute sql:select * from aaa Using shard:shard2 to execute sql:select * from aaa Using shard:shard1 to execute sql:select * from aaa Using shard:shard1 to execute sql:select * from aaa Using shard:shard1 to execute sql:select * from aaa Using shard:shard1 to execute sql:select * from aaa Using shard:shard1 to execute sql:select * from aaa Using shard:shard3 to execute sql:select * from aaa |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
Class.forName("org.tinygroup.dbcluster.jdbc.TinyDriver"); Connection conn = DriverManager.getConnection( "jdbc:dbcluster://cluster1", "luog", "123456"); Statement stmt = conn.createStatement(); String sql = "select * from aaa order by id"; ResultSet resultSet = stmt.executeQuery(sql); resultSet.absolute(10); System.out.printf(" id: %d, aaa: %s \n", resultSet.getInt(1), resultSet.getString(2)); while (resultSet.next()) { System.out.printf(" id: %d, aaa: %s \n", resultSet.getInt(1), resultSet.getString(2)); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
Using shard:shard0 to execute sql:select * from aaa order by id Using shard:shard1 to execute sql:select * from aaa order by id Using shard:shard2 to execute sql:select * from aaa order by id id: 10, aaa: ppp id: 11, aaa: ppp id: 12, aaa: ppp id: 13, aaa: ppp id: 14, aaa: ppp id: 15, aaa: ppp id: 16, aaa: ppp id: 17, aaa: ppp id: 18, aaa: ppp id: 19, aaa: ppp ……. |