阿里巴巴解决数据拆分的伪分布式数据库 中间件Cobar正式开源

来源:互联网 发布:amd优化软件 编辑:程序博客网 时间:2024/05/21 11:37

阿里巴巴于2012年6月19日,正式对外开源的数据库中间件Cobar,前身是早已经开源的Amoeba,不过其作者陈思儒离职去盛大之后,阿里巴巴内部考虑到Amoeba的稳定性、

性能和功能支持,以及其他因素,重新设立了一个项目组并且更换名称为Cobar,当时的开发人员是贺贤懋(备注:可能名字的字不对,实在有点久远,虽然当时一起合作),还

有一位百阿的同事也加入这个团队(备注:旺旺密码不记得了,所以他的名字也无法记起,看见请莫怪我这位记忆力不好的百阿同学),开发语言是Java,一开始只支持MySQL

数据库,并且用在新项目BRMMS(中文名称:商人社区,BRMMS是项目代号,一般只记得代号,实在参与过太多项目研发),后来也支持Oracle数据库,因为阿里巴巴中文站

的Offer数据库,需要从Oracle数据库+存储设备,迁移到MySQL+PC Server平台上,为保证用户数据的安全性,迁移过程是每128分之一切换的模式。虽然测试的非常严格,我

们几乎所有可能碰到的情况,甚至极端情况都测试过,但是依然碰过一些莫名其妙的问题,比如从MySQL双主复制模式,从主A切换为B,出现过某个小集群的应用程序连接确实

切换成功,但是又自己切换回来了,直到我离开也没有找出原因,不过后来再切换又从未出现过,Cobar开源对大家解决数据的垂直拆分和水平拆分,那是如虎贴翼,非常方便!

 

场景描述

Cobar是关系型数据的分布式处理系统,它可以在分布式的环境下像传统数据库一样为您提供海量数据服务。以下是快速启动场景:

  • 系统对外提供的数据库名是dbtest,并且其中有两张表tb1和tb2。
  • tb1表的数据被映射到物理数据库dbtest1的tb1上。
  • tb2表的一部分数据被映射到物理数据库dbtest2的tb2上,另外一部分数据被映射到物理数据库dbtest3的tb2上。
    如下图所示:

步骤一:环境准备

  • 软件准备

    操作系统: Linux或者Windows (推荐在Linux环境下运行Cobar)
    MySQL: http://www.mysql.com/downloads/ (推荐使用5.1以上版本)
    JDK: http://www.oracle.com/technetwork/java/javase/downloads/ (推荐使用1.6以上版本)
    Cobar: http://code.alibabatech.com/wiki/display/cobar/release/ (下载tar.gz或者zip文件)

数据准备

假设本文MySQL所在服务器IP为192.168.0.1,端口为3306,用户名为test,密码为空,我们需要创建schema:dbtest1、dbtest2、dbtest3,table:tb1、tb2,脚本如下:

数据库创建脚本:

?
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
#创建dbtest1
dropdatabase if exists dbtest1;
createdatabase dbtest1;
use dbtest1;
#在dbtest1上创建tb1
createtable tb1(
id   intnot null,
gmt   datetime);
 
#创建dbtest2
dropdatabase if exists dbtest2;
createdatabase dbtest2;
use dbtest2;
#在dbtest2上创建tb2
createtable tb2(
id   intnot null,
val  varchar(256));
 
#创建dbtest3
dropdatabase if exists dbtest3;
createdatabase dbtest3;
use dbtest3;
#在dbtest3上创建tb2
createtable tb2(
id   intnot null,
val  varchar(256));

步骤二:部署和配置Cobar

请确保机器上设置了JAVA环境变量JAVA_HOME 下载Cobar压缩文件并解压,进入conf目录可以看到schema.xml, rule.xml, server.xml等相关的配置文件

?
1
2
3
4
5
6
wget http://code.alibabatech.com/mvn/releases/com/alibaba/cobar/cobar-server/1.2.4/cobar-server-1.2.4.tar.gz
tarzxf cobar-server-1.2.4.tar.gz
cdcobar-server-1.2.4 #可以看到bin,conf,lib,logs四个目录
 
<strong>schema.xml配置如下&lt;span style="color:</strong> #ff0000;"&gt;(注意:schema.xml包含MySQL的IP、端口、用户名、密码等配置,您需要按照注释替换为您的MySQL信息。)&lt;/span&gt;
&lt;strong&gt;schema.xml 配置&lt;/strong&gt;:
?
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
<?xml version="1.0"encoding="UTF-8"?>
<!DOCTYPE cobar:schema SYSTEM "schema.dtd">
<cobar:schema xmlns:cobar="http://cobar.alibaba.com/">
 
  <!-- schema定义 -->
  <schema name="dbtest"dataNode="dnTest1">
    <table name="tb2"dataNode="dnTest2,dnTest3"rule="rule1"/>
  </schema>
 
  <!-- 数据节点定义,数据节点由数据源和其他一些参数组织而成。-->
  <dataNode name="dnTest1">
    <property name="dataSource">
      <dataSourceRef>dsTest[0]</dataSourceRef>
    </property>
  </dataNode>
  <dataNode name="dnTest2">
    <property name="dataSource">
      <dataSourceRef>dsTest[1]</dataSourceRef>
    </property>
  </dataNode>
  <dataNode name="dnTest3">
    <property name="dataSource">
      <dataSourceRef>dsTest[2]</dataSourceRef>
    </property>
  </dataNode>
 
  <!-- 数据源定义,数据源是一个具体的后端数据连接的表示。-->
  <dataSource name="dsTest"type="mysql">
    <property name="location">
      <location>192.168.0.1:3306/dbtest1</location> <!--注意:替换为您的MySQL IP和Port-->
      <location>192.168.0.1:3306/dbtest2</location> <!--注意:替换为您的MySQL IP和Port-->
      <location>192.168.0.1:3306/dbtest3</location> <!--注意:替换为您的MySQL IP和Port-->
    </property>
    <property name="user">test</property> <!--注意:替换为您的MySQL用户名-->
    <property name="password"></property> <!--注意:替换为您的MySQL密码-->
    <property name="sqlMode">STRICT_TRANS_TABLES</property>
  </dataSource>
</cobar:schema>
?
1
<strong>rule.xml配置如下<span style="color: #ff0000;">(本文仅以数字类型的id字段作为拆分字段,将数据拆分到两个库中。)</span></strong>
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0"encoding="UTF-8"?>
<!DOCTYPE cobar:rule SYSTEM "rule.dtd">
<cobar:rule xmlns:cobar="http://cobar.alibaba.com/">
  <!-- 路由规则定义,定义什么表,什么字段,采用什么路由算法。-->
  <tableRule name="rule1">
    <rule>
      <columns>id</columns>
      <algorithm><![CDATA[ func1(${id})]]></algorithm>
    </rule>
  </tableRule>
 
  <!-- 路由函数定义,应用在路由规则的算法定义中,路由函数可以自定义扩展。-->
  <functionname="func1"class="com.alibaba.cobar.route.function.PartitionByLong">
    <property name="partitionCount">2</property>
    <property name="partitionLength">512</property>
  </function>
</cobar:rule>
?
1
<strong>server.xml配置如下</strong>
?
1
2
3
4
5
6
7
8
9
10
<?xml version="1.0"encoding="UTF-8"?>
<!DOCTYPE cobar:server SYSTEM "server.dtd">
<cobar:server xmlns:cobar="http://cobar.alibaba.com/">
 
  <!--定义Cobar用户名,密码-->
  <user name="test">
    <property name="password">test</property>
    <property name="schemas">dbtest</property>
  </user>
</cobar:server>

步骤三:启动和使用Cobar

启动Cobar,进入bin目录可以看到Cobar的启动、停止与重启脚本

?
1
2
3
./startup.sh#Cobar进程名为CobarStartup
 
<strong>查看logs目录下stdout.log, 启动成功日志如下</strong>
?
1
2
3
4
5
6
7
8
9
10
11
12
13
10:54:19,264 INFO  ===============================================
10:54:19,265 INFO  Cobar is ready to startup ...
10:54:19,265 INFO  Startup processors ...
10:54:19,443 INFO  Startup connector ...
10:54:19,446 INFO  Initialize dataNodes ...
10:54:19,470 INFO  dnTest1:0 init success
10:54:19,472 INFO  dnTest3:0 init success
10:54:19,473 INFO  dnTest2:0 init success
10:54:19,481 INFO  CobarManager is started and listening on 9066
10:54:19,483 INFO  CobarServer is started and listening on 8066
10:54:19,484 INFO  ===============================================
 
<strong>访问Cobar同访问MySQL的方式完全相同, 常用访问方式如下</strong><span style="color: #ff0000;">(注意:本文将Cobar部署在192.168.0.1这台机器上,否则请替换为您的Cobar所在IP,其他信息不变)</span>
?
1
2
3
4
5
6
7
8
9
#命令行
mysql -h192.168.0.1 -utest -ptest -P8066 -Ddbtest
 
#JDBC(建议5.1以上的mysql driver版本)
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://192.168.0.1:8066/dbtest","test","test");
......
 
<strong>SQL执行示例,执行语句时与使用传统单一数据库无区别</strong>
?
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
mysql>show databases;                                                #dbtest1、dbtest2、dbtest3对用户透明
+----------+
|DATABASE|
+----------+
| dbtest   |
+----------+
 
mysql>show tables;                                                   #dbtest中有两张表tb1和tb2
+-------------------+
| Tables_in_dbtest1 |
+-------------------+
| tb1               |
| tb2               |
+-------------------+
 
mysql>insertinto tb1 (id, gmt) values(1, now());                   #向表tb1插入一条数据
mysql>insertinto tb2 (id, val) values(1, "part1");                 #向表tb2插入一条数据
mysql>insertinto tb2 (id, val) values(2, "part1"), (513, "part2"); #向表tb2同时插入多条数据
mysql>select* fromtb1;                                             #查询表tb1,验证数据被成功插入
+----+---------------------+
| id | gmt                 |
+----+---------------------+
|  1 | 2012-06-12 15:00:42 |
+----+---------------------+
 
mysql>select* fromtb2;                                             #查询tb2,验证数据被成功插入
+-----+-------+
| id  | val   |
+-----+-------+
|   1 | part1 |
|   2 | part1 |
| 513 | part2 |
+-----+-------+
 
mysql>select* fromtb2 whereid in(1, 513);                        #根据id查询
+-----+-------+
| id  | val   |
+-----+-------+
|   1 | part1 |
| 513 | part2 |
+-----+-------+
  • 查看后端MySQL数据库dbtest1,dbtest2和dbtest3,验证数据分布在不同的库中

产品约束

  • 使用JDBC时,推荐使用5.1以上版本Driver进行连接
  • 不支持跨库的关联操作:join、分页、排序、子查询。
  • 不支持rewriteBatchedStatements=true参数设置。默认为false
  • 不支持useServerPrepStmts=true参数设置。默认为false
  • BLOB, BINARY, VARBINARY字段不能使用。若特殊需求需要这三种字段,禁止使用PreparedStatement的setBlob()或setBinaryStream()方法设置参数。
  • 不支持SAVEPOINT操作。
  • 不支持SET语句的执行,事务和字符集设置语句除外
  • 对于拆分表(一个表的数据被映射到多个MySQL数据库),不能更新已有记录的拆分字段(分库字段)值
  • 只支持MySQL数据节点。
  • 对于拆分表,插入操作须给出列名,必须包含拆分字段。

源码下载地址:http://code.alibabatech.com/svn/cobar/

转致 http://www.mysqlops.com/2012/06/19/cobar-alibaba.html