http://code.alibabatech.com/wiki/display/cobar/Home
http://tech.it168.com/a2012/0625/1364/000001364072.shtml
概述
Cobar是關係型數據的分佈式處理系統,它可以在分佈式的環境下看上去像傳統數據庫一樣爲您提供海量數據服務。
- 產品在阿里巴巴B2B公司已經穩定運行了3年以上。
- 目前已經接管了3000+個MySQL數據庫的schema,爲應用提供數據服務。
- 據最近統計cobar集羣目前平均每天處理近50億次的SQL執行請求。
快速啓動
場景描述
- 系統對外提供的數據庫名是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,腳本如下:數據庫創建腳本#創建dbtest1
drop
database
if exists dbtest1;
create
database
dbtest1;
use dbtest1;
#在dbtest1上創建tb1
create
table
tb1(
id
int
not
null
,
gmt datetime);
#創建dbtest2
drop
database
if exists dbtest2;
create
database
dbtest2;
use dbtest2;
#在dbtest2上創建tb2
create
table
tb2(
id
int
not
null
,
val
varchar
(256));
#創建dbtest3
drop
database
if exists dbtest3;
create
database
dbtest3;
use dbtest3;
#在dbtest3上創建tb2
create
table
tb2(
id
int
not
null
,
val
varchar
(256));
步驟二:部署和配置Cobar
請確保機器上設置了JAVA環境變量JAVA_HOME |
- 下載Cobar壓縮文件並解壓,進入conf目錄可以看到schema.xml, rule.xml, server.xml等相關的配置文件
wget http:
//code
.alibabatech.com
/mvn/releases/com/alibaba/cobar/cobar-server/1
.2.4
/cobar-server-1
.2.4.
tar
.gz
tar
zxf cobar-server-1.2.4.
tar
.gz
cd
cobar-server-1.2.4
#可以看到bin,conf,lib,logs四個目錄
- schema.xml配置如下(注意:schema.xml包含MySQL的IP、端口、用戶名、密碼等配置,您需要按照註釋替換爲您的MySQL信息。)
schema.xml 配置
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<!DOCTYPE cobar:schema SYSTEM "schema.dtd">
<!-- 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
>
- rule.xml配置如下(本文僅以數字類型的id字段作爲拆分字段,將數據拆分到兩個庫中。)
rule.xml 配置
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<!DOCTYPE cobar:rule SYSTEM "rule.dtd">
<!-- 路由規則定義,定義什麼表,什麼字段,採用什麼路由算法。-->
<
tableRule
name
=
"rule1"
>
<
rule
>
<
columns
>id</
columns
>
<
algorithm
>
<![CDATA[ func1(${id})]]>
</
algorithm
>
</
rule
>
</
tableRule
>
<!-- 路由函數定義,應用在路由規則的算法定義中,路由函數可以自定義擴展。-->
<
function
name
=
"func1"
class
=
"com.alibaba.cobar.route.function.PartitionByLong"
>
<
property
name
=
"partitionCount"
>2</
property
>
<
property
name
=
"partitionLength"
>512</
property
>
</
function
>
</
cobar:rule
>
- server.xml配置如下
server.xml 配置
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<!DOCTYPE cobar:server SYSTEM "server.dtd">
<!--定義Cobar用戶名,密碼-->
<
user
name
=
"test"
>
<
property
name
=
"password"
>test</
property
>
<
property
name
=
"schemas"
>dbtest</
property
>
</
user
>
</
cobar:server
>
步驟三:啓動和使用Cobar
- 啓動Cobar,進入bin目錄可以看到Cobar的啓動、停止與重啓腳本
.
/startup
.sh
#Cobar進程名爲CobarStartup
- 查看logs目錄下stdout.log, 啓動成功日誌如下
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 ===============================================
- 訪問Cobar同訪問MySQL的方式完全相同, 常用訪問方式如下(注意:本文將Cobar部署在192.168.0.1這臺機器上,否則請替換爲您的Cobar所在IP,其他信息不變)
#命令行
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"
);
......
- SQL執行示例,執行語句時與使用傳統單一數據庫無區別
mysql>show databases; #dbtest1、dbtest2、dbtest3對用戶透明
+
----------+
|
DATABASE
|
+
----------+
| dbtest |
+
----------+
mysql>show tables; #dbtest中有兩張表tb1和tb2
+
-------------------+
| Tables_in_dbtest1 |
+
-------------------+
| tb1 |
| tb2 |
+
-------------------+
mysql>
insert
into
tb1 (id, gmt)
values
(1, now()); #向表tb1插入一條數據
mysql>
insert
into
tb2 (id, val)
values
(1,
"part1"
); #向表tb2插入一條數據
mysql>
insert
into
tb2 (id, val)
values
(2,
"part1"
), (513,
"part2"
); #向表tb2同時插入多條數據
mysql>
select
*
from
tb1; #查詢表tb1,驗證數據被成功插入
+
----+---------------------+
| id | gmt |
+
----+---------------------+
| 1 | 2012-06-12 15:00:42 |
+
----+---------------------+
mysql>
select
*
from
tb2; #查詢tb2,驗證數據被成功插入
+
-----+-------+
| id | val |
+
-----+-------+
| 1 | part1 |
| 2 | part1 |
| 513 | part2 |
+
-----+-------+
mysql>
select
*
from
tb2
where
id
in
(1, 513); #根據id查詢
+
-----+-------+
| id | val |
+
-----+-------+
| 1 | part1 |
| 513 | part2 |
+
-----+-------+
- 查看後端MySQL數據庫dbtest1,dbtest2和dbtest3,驗證數據分佈在不同的庫中