[转]关系型数据的分布式处理系统Cobar

转载自:

http://code.alibabatech.com/wiki/display/cobar/Home

概述

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">
    < 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 >
  • rule.xml配置如下(本文仅以数字类型的id字段作为拆分字段,将数据拆分到两个库中。)
    rule.xml 配置
    <? 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 >
     
       <!-- 路由函数定义,应用在路由规则的算法定义中,路由函数可以自定义扩展。-->
       < 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: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的启动、停止与重启脚本
    . /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,验证数据分布在不同的库中

猜你喜欢

转载自topmanopensource.iteye.com/blog/1897865