Dbsplit扩展了Spring的JdbcTemplate, 在JdbcTemplate上增加了分库分表,读写分离和失效转移等功能,并与Spring JDBC保持相同的风格,简单实用,避免外部依赖,不需要类似cobar的代理服务器,堪称可伸缩的Spring JdbcTemplate。
Dbsplit扩展了Spring的JdbcTemplate, 在JdbcTemplate上增加了分库分表,读写分离和失效转移等功能,并与Spring JDBC保持相同的风格,简单实用,避免外部依赖,不需要类似cobar的代理服务器,堪称可伸缩的Spring JdbcTemplate。
一方面,它对于单库单表扩展了JdbcTemplate模板, 使其成为一个简单的ORM框架,可以直接对领域对象模型进行持久和搜索操作,并且实现了读写分离。
另一方面,对于分库分表它与JdbcTemplate保持同样的风格,不但提供了一个简单的ORM框架,可以直接对领域对象模型进行持久和搜索操作,还是先了数据分片和读写分离等高级功能。
另外,扩展的Dbsplit保持与原有JdbcTemplate完全兼容,对于特殊需求,完全可以回溯到原有JdbcTemplate提供的功能,即使用JDBC的方式来解决,这里面体现了通用和专用原则,通用原则解决80%的事情,而专用原则解决剩余的20%的事情。
此项目也提供了一个方便的脚本,可以一次性的建立多库多表。
特别适合想知道互联网的分库分表是怎么实现的,也适合那些想把分库分表框架开箱即用的项目,更适合想学习互联网的小伙伴们。
如果你在寻找数据库分库分表的轻量级解决方案,请参考Dbsplit的实现和应用场景,它是一个兼容Spring JDBC的并且支持分库分表的轻量级的数据库中间件,使用起来简单方便,性能接近于直接使用JDBC,并且能够无缝的与Spring相结合,又具有很好的可维护性。
我们已经完整的实现了一个具有分库分表功能的框架dbsplit,现在,让我们提供一个示例演示在我们的应用中怎么来使用这个框架,大家也可以参考dbsplit项目中dbsplit-core/src/main/test中的源代码。
首先,假设我们应用中有个表需要增删改查,它的DDL脚本如下:
我们把这个DDL脚本保存到table.sql文件中,然后,我们需要准备好一个Mysql的数据库实例,实例端口为localhost:3307, 因为环境的限制,我们用着一个数据库实例来模拟两个数据库实例,两个数据库实例使用同一个端口,我们为TEST_TABLE设计了2个数据库实例、每个实例2个数据库、每个数据库4个表,共16个分片表。
我们使用脚本创建创建用于分片的多个数据库和表,脚本代码如下所示:
build-db-split.sh -i 'localhost:3307,localhost:3307' -m test_db -n table.sql -x 2 -y 4 -a test_user -b test_password -c root -d youarebest -l localhost -t
这里,需要提供系统root用户的用户名和密码。
然后,我们登录Mysql的命令行客户端,我们看到一共创建了4个数据库,前2个数据库属于数据库实例1,后2个数据库属于数据库实例2,每个数据库有4个表。
mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || test || test_db_0 || test_db_1 || test_db_2 || test_db_3 |+--------------------+6 rows in set (0.01 sec)mysql> use test_db_0;Database changedmysql> show tables;+---------------------+| Tables_in_test_db_0 |+---------------------+| TEST_TABLE_0 || TEST_TABLE_1 || TEST_TABLE_2 || TEST_TABLE_3 |+---------------------+4 rows in set (0.00 sec)
因此,一共我们创建了16个分片表。
然后,我们定义对应这个数据库表的领域对象模型,在这个领域对象模型中,我们不需要任何注解,这是一个绿色的POJO。
因为我们的应用程序需要保存这个实体,这就需要生成唯一的ID,发号器的设计和使用请参考第4章如何设计一款永不重复的高性能分布式发号器,这里我们需要配置一个发号器服务即可,代码如下所示。
接下来,我们在Spring环境中定义这个表的分片信息,这包括数据库名称、表名称、数据库分片数、表的分片数,以及读写分离等信息,本例中我们制定数据库前缀为test_db,数据库表名为TEST_TABLE,每个实例2个数据库,每个数据库4张表,分片采用采用水平下标策略,并且打开读写分离。
我们看到,这个splitTable引用了两个数据库实例节点:splitNode1和splitNode2,他们的声明如下:
每个数据库实例节点都引用了一个数据库主模板以及若干个数据库从模板,这是用来实现读写分离的,因为我们打开了读写分离设置,所有的读操作将由dbsplit路由到数据库的从模板上,数据库的主从模板的声明引用到我们生命的数据库,因为我们是在本地做测试,这些数据源都指向了本地的Mysql数据库localhost:3307。
到现在为止,我们定义好了表的分片信息,把我们把这个表加入到SplitTablesHolder的Bean中,代码如下所示:
接下来,我们就需要声明我们的SimpleSplitJdbcTemplate的Bean,它需要引用SplitTablesHolder的Bean,以及配置读写分离的策略,配置代码如下所示,
我们有了SimpleSplitJdbcTemplate的Bean,我们就可以把它导出给我们的服务层来使用了。这里我们通过一个测试用例来演示,在测试用例中初始化刚才我们配置的Spring环境,从Spring环境中获取SimpleSplitJdbcTemplate的Bean simpleSplitJdbcTemplate,然后,示例里面的方法插入TEST_TABLE的记录,然后,再把这条记录查询出来,代码如下所示。
这里介绍一个用于创建分库分表的脚本,这个脚本可以一次性的按照规则在多个mysql示例上创建多个数据库和表,以及在每一个数据库实例上创建一个统一的用户,并分配相应的权限给此用户。
Usage: $0 -i [INSTANCE_STR] -m [DB_PREFIX] -n [TABLE_SQL_FILE] -x [DB_SPLIT_NUM] -y [TABLE_SPLIT_NUM] -a [USER] -b [PASSWORD] -c [ROOT_USER] -d [ROOT_PASSWORD] -l [CONNECTION_HOST] -t Descriptions:-i : instances string.-m : db name.-n : table file name.-x : db number.-y : table number.-a : user name to be created.-b : password for the user name to be created.-c : root user.-d : password for root user.-l : for the connection host.-t : debug sql output.
Example1: $0 -i 'localhost:3306,localhost:3306' -m test_db -n table.sql -x 2 -y 2 -a test_user -b test_password -c root -d youarebest -l localhost -t Example2: $0 -i 'localhost:3306,localhost:3306' -m test_db -n table.sql -x 2 -y 2 -a test_user -b test_password -c root -d youarebest -l localhost
#!/bin/bashinsts=localhost:3306,localhost:3306db_prefix=test_dbtable_sql_file=table.sqldb_num=2table_num=2user_name=test_userpassword=test_passwordroot_user_name=rootroot_password=cooldebug=FALSEconn_host=localhostbuild_db() { inst=$1 inst_arr=(${inst//:/ }) host=${inst_arr[0]} port=${inst_arr[1]} db=$2 db_no=$3 echo 'info: building instance $inst db $db db no $db_no' for ((k=0;k<$table_num;k++)); do="" ="" ((table_no="$table_num*$db_no+$k))" ="" echo="" 'info:="" building="" instance="" $inst="" db="" $db="" db="" no="" $db_no="" table="" $table_no'="" ="" ="" ="" ="" ="" sql_command='sed ' s/''$index''/$table_no/g'="" ./$table_sql_file="" |="" tr="" -t="" '\n'="" '\0''="" ="" sql_create_table="`eval" '$sql_command'`="" ="" ="" ="" if="" [[="" $debug='TRUE' ]];="" then="" ="" ="" ="" echo="" 'create="" table="" sql:="" $sql_create_table'="" ="" fi="" ="" mysql="" -u$root_user_name="" -p$root_password="" -e="" '$sql_create_table'="" $db="" 2=""> /dev/null done }build_inst() { inst=$1 inst_arr=(${inst//:/ }) host=${inst_arr[0]} port=${inst_arr[1]} inst_no=$2 echo 'info: building instance $inst no $inst_no' sql_delete_user='delete from mysql.user where user = '$user_name'; flush privileges' if [[ $debug = 'TRUE' ]]; then echo 'Delete User SQL: $sql_delete_user' fi mysql -u$root_user_name -p$root_password -e '$sql_delete_user' 2> /dev/null mysql -u$root_user_name -p$root_password -e 'create user '$user_name'@'$conn_host' identified by '$password'' for ((j=0;j<$db_num;j++)); do="" ="" ((db_no="$db_num*$inst_no+$j))" ="" ="" ="" create_database_sql='drop database if exists ${db_prefix}_${db_no};create database ${db_prefix}_${db_no}' ="" ="" ="" if="" [[="" $debug='TRUE' ]];="" then="" ="" ="" echo="" 'create="" database="" sql:="" $create_database_sql'="" ="" fi="" ="" ="" ="" mysql="" -u$root_user_name="" -p$root_password="" -e="" '$create_database_sql'="" 2=""> /dev/null assign_rights_sql='grant all privileges on ${db_prefix}_${db_no}.* to '$user_name'@'$conn_host' identified by '$password';flush privileges' if [[ $debug = 'TRUE' ]]; then echo 'Assign Rights SQL: $assign_rights_sql' fi mysql -u$root_user_name -p$root_password -e 'assign_rights_sql' 2> /dev/null build_db $inst ${db_prefix}_${db_no} $db_no done }main() { echo 'properties: insts=$insts db_prefix=$db_prefix table_sql_file=$table_sql_file db_num=$db_num table_num=$table_num user_name=$user_name password=$password root_user_name=$root_user_name root_password=$root_password' insts_arr=(${insts//,/ }) insts_num=${#insts_arr[@]} for ((i=0;i<$insts_num;i++)); do="" ="" ="" build_inst="" ${insts_arr[$i]}="" $i="" ="" done}printusage(){cat="">$insts_num;i++));>< endofusagemessage="" ="" usage:="" $0="" -i="" [instance_str]="" -m="" [db_prefix]="" -n="" [table_sql_file]="" -x="" [db_split_num]="" -y="" [table_split_num]="" -a="" [user]="" -b="" [password]="" -c="" [root_user]="" -d="" [root_password]="" -l="" [connection_host]="" -t="" ="" ="" ="" descriptions:="" ="" -i="" :="" instances="" string.="" ="" -m="" :="" db="" name.="" ="" -n="" :="" table="" file="" name.="" ="" -x="" :="" db="" number.="" ="" -y="" :="" table="" number.="" ="" -a="" :="" user="" name="" to="" be="" created.="" ="" -b="" :="" password="" for="" the="" user="" name="" to="" be="" created.="" ="" -c="" :="" root="" user.="" ="" -d="" :="" password="" for="" root="" user.="" ="" -l="" :="" for="" the="" connection="" host.="" ="" -t="" :="" debug="" sql="" output.="" ="" ="" ="" example1:="" $0="" -i="" 'localhost:3306,localhost:3306'="" -m="" test_db="" -n="" table.sql="" -x="" 2="" -y="" 2="" -a="" test_user="" -b="" test_password="" -c="" root="" -d="" youarebest="" -l="" localhost="" -t="" ="" example2:="" $0="" -i="" 'localhost:3306,localhost:3306'="" -m="" test_db="" -n="" table.sql="" -x="" 2="" -y="" 2="" -a="" test_user="" -b="" test_password="" -c="" root="" -d="" youarebest="" -l="" localhost="" ="" endofusagemessage}invalidcommandsyntaxexit(){="" ="" ="" ="" echo="" 'invalid="" command\n`printusage`'="" ="" ="" ="" exit;}if="" [="" $#="" -eq="" 0="" ]then="" ="" echo="" '`printusage`'="" ="" exit="" 1fiwhile="" getopts="" 'i:m:n:x:y:a:b:c:d:l:t'="" argdo="" ="" ="" ="" case="" $arg="" in="" ="" ="" ="" ="" ="" ="" i)="" ="" ="" ="" ="" ="" ="" ="" insts="$OPTARG" ="" ="" ="" ="" ="" ="" ="" ;;="" ="" ="" ="" ="" ="" ="" m)="" ="" ="" ="" ="" ="" ="" ="" db_prefix="$OPTARG" ="" ="" ="" ="" ="" ="" ="" ;;="" ="" ="" ="" ="" ="" ="" n)="" ="" ="" ="" ="" ="" ="" ="" table_sql_file="$OPTARG" ="" ="" ="" ="" ="" ="" ="" ;;="" ="" ="" ="" ="" ="" ="" x)="" ="" ="" ="" ="" ="" ="" ="" db_num="$OPTARG" ="" ="" ="" ="" ="" ="" ="" ;;="" ="" ="" ="" ="" ="" ="" y)="" ="" ="" ="" ="" ="" ="" ="" table_num="$OPTARG" ="" ="" ="" ="" ="" ="" ="" ;;="" ="" ="" ="" ="" ="" ="" a)="" ="" ="" ="" ="" ="" ="" ="" user_name="$OPTARG" ="" ="" ="" ="" ="" ="" ="" ;;="" ="" ="" ="" ="" ="" ="" b)="" ="" ="" ="" ="" ="" ="" ="" password="$OPTARG" ="" ="" ="" ="" ="" ="" ="" ;;="" ="" ="" ="" ="" ="" ="" c)="" ="" ="" ="" ="" ="" ="" ="" root_user_name="$OPTARG" ="" ="" ="" ="" ="" ="" ="" ;;="" ="" ="" ="" ="" ="" ="" d)="" ="" ="" ="" ="" ="" ="" ="" root_password="$OPTARG" ="" ="" ="" ="" ="" ="" ="" ;;="" ="" ="" ="" ="" ="" ="" l)="" ="" ="" ="" ="" ="" ="" ="" conn_host="$OPTARG" ="" ="" ="" ="" ="" ="" ="" ;;="" ="" ="" ="" ="" ="" ="" t)="" ="" ="" ="" ="" ="" ="" ="" debug="TRUE" ="" ="" ="" ="" ="" ="" ="" ;;="" ="" ="" ="" ="" ="" ="" ="" ="" ="" ="" ="" ="" ="" echo="" '`invalidcommandsyntaxexit`'="" ="" ="" ="" ="" ="" ="" ="" exit="" 1="" ="" ="" ="" ="" ="" ="" ="" ;;="" ="" ="" ="">$db_num;j++));>$table_num;k++));>
这个脚本仅仅是一个示例,计划中,这个脚本需要支持三种分库分表的策略,数据库和表下标累积的策略,数据库和表下标归零的策略与两种混合策略, 当前脚本只支持第一种。
我们需要注意,这个建库脚本不支持建立主从关系,但是可以建立主库和从库后再手工建立主从关系。
联系客服