阿里巴巴分布式数据库解决方案——Cobar中间件配置详解 (转)

阿里巴巴分布式数据库解决方案——Cobar中间件配置详解(转)  

据说该产品在阿里的B2B上已经运行3年之久,通过Cobar中间件,可以像传统数据库一样为分布式数据库集群提供海量数据服务,据称Cobar集群每天处理50亿次的SQL请求。
该中间件开源与2012年6月19日,可惜我现在才玩上。。。出于每次玩完一个东西之后都喜欢写点总结之类的,所以该文是我的一些心得体会以及碰到的问题的解决办法,当然阿里既然开源,也提供了 文档资料,不过这里并没有问题的解决办法,而且,我感觉这个文档有点问题,具体我后面再说。
1.软件需求
MYSQL:5.1以上
JDK:1.6以上
Cobar: 源码(我的是1.2.4版本)放在192.168.213.9号节点上
2.数据库准备:(即datanode)——我这是一个虚拟机,IP:192.168.213.201
建立数据库的脚本如下:

dbtest1——单独数据库,存放tb1的数据
dbtest2dbtest3——两个数据库,共同存放tb2的数据
create database dbtest1;
use dbtest1;
create table tb1(
id    int not null ,
gmt   datetime);
create database dbtest2;
use dbtest2;
create table tb2(
id    int not null ,
val   varchar (256));
create database dbtest3;
use dbtest3;
create table tb2(
id    int not null ,
val   varchar (256));

3.Cobar配置
解压缩之后就可以进入文件夹进行配置了(在节点192.168.213.9上配置)
修改schema.xml文件如下:(因为我的设置跟默认吻合,因此我只需要修改这个文件)

<?xml version="1.0" encoding="UTF-8"?> <!-- - Copyright 1999-2012 Alibaba Group. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. --> <!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.213.201:3306/dbtest1</location> <location>192.168.213.201:3306/dbtest2</location> <location>192.168.213.201:3306/dbtest3</location> </property> <property name="user">root</property> <property name="password"></property>

<property name="sqlMode">STRICT_TRANS_TABLES</property> </dataSource> </cobar:schema>

必要的解释:
1.我们从后往前看,最后定义的是数据源dataSource,其实这里可以定义多个,不同的数据源对应不同的数据库实例(注意数据库实例与数据库的区别,有兴趣的可以找本书看下),一个数据源可以包含多个数据库schema (这里schema和database是有区别的,有人比方说schema是仓库,database是房子,tb是房子里的床,很形象。不过这里我们可以看成就是database),比如我这里就是一个数据库实例,三个schema(仓库或者房子)。这里user和password我们后面再解释。
2.定义好了数据源之后,也就是生成了一个数组,然后我们将数组的每一个元素生成一个dataNode,这是一个非常棒的设计,这样就能把一个数据库实例中的多个数据库分离开来,下一步我么就要用。
3.第三步就是定义schema,也就是Cobar的初衷,生成一个底层有多个数据库组成的分布式数据库,但是用户看到的是跟传统数据库一样的一个数据库,看完定义之后也许 有人会问为啥没有tb1,这里解释一下:没有tb1其实是因为第一行已经说明了,第一行的意思是除了下面定义的表(如tb2)需要按既定的rule(如rule1)去对应的数据节点(dnTest2,dnTest3)路由找到相应数据之外,其余的表(tb1)都是在dataNode(dnTest1)上。而这里的dataNode定义在上一步中已经完成的。从定义上看,我们将要生成了一个名叫dbtest的schema,也即数据库,这就是Cobar的目标。
好了,配置完了之后就可以启动了

[root@localhost bin]# ./startup.sh "/usr/java/jdk1.6.0_31/bin/java" -Dcobar.home="/home/wuyanzan/source/cobar-server-1.2.6" -classpath "/home/wuyanzan/source/cobar-server-1.2.6/conf:/home/wuyanzan/source/cobar-server-1.2.6/lib/classes:/home/wuyanzan/source/cobar-server-1.2.6/lib/cobar-common-1.2.6.jar:/home/wuyanzan/source/cobar-server-1.2.6/lib/cobar-config-1.2.6.jar:/home/wuyanzan/source/cobar-server-1.2.6/lib/cobar-net-1.2.6.jar:/home/wuyanzan/source/cobar-server-1.2.6/lib/cobar-parser-1.2.6.jar:/home/wuyanzan/source/cobar-server-1.2.6/lib/cobar-route-1.2.6.jar:/home/wuyanzan/source/cobar-server-1.2.6/lib/cobar-server-1.2.6.jar:/home/wuyanzan/source/cobar-server-1.2.6/lib/log4j-1.2.16.jar:/home/wuyanzan/source/cobar-server-1.2.6/lib/slf4j-api-1.6.4.jar:/home/wuyanzan/source/cobar-server-1.2.6/lib/slf4j-log4j12-1.6.4.jar" -server -Xms1024m -Xmx1024m -Xmn256m -Xss128k -XX:+AggressiveOpts -XX:+UseBiasedLocking -XX:+UseFastAccessorMethods -XX:+DisableExplicitGC -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=75 com.alibaba.cobar.CobarStartup >> "/home/wuyanzan/source/cobar-server-1.2.6/logs/console.log" 2>&1 &

然后进logs文件夹查看启动情况

17:19:06,623 INFO =============================================== 17:19:06,624 INFO Cobar is ready to startup ... 17:19:06,624 INFO Startup processors ... 17:19:07,047 INFO Startup connector ... 17:19:07,049 INFO Initialize dataNodes ... 17:19:07,068 INFO dnTest1:0 init success 17:19:07,069 INFO dnTest3:0 init success 17:19:07,070 INFO dnTest2:0 init success 17:19:07,077 INFO CobarManager is started and listening on 9066 17:19:07,078 INFO CobarServer is started and listening on 8066 17:19:07,078 INFO =============================================== 17:19:07,113 INFO [thread=Processor1-H0,class=ServerConnection,host=127.0.0.1,port=57823,schema=null]'_HEARTBEAT_USER_' login success

说明启动成功,也就是说现在Cobar在192.168.213.9号节点上已经部署好了,对用户来说,该节点上已经有了一个mysql数据库schema,里面包含了两张表:tb1,tb2,database的名字可以从上面说到的schema.xml中看到(这里我是按默认设置,当然也可以修改),具体如下:

# mysql -h 192.168.213.9 -utest -ptest -P8066 -Ddbtest

mysql> show databases; +----------+ | DATABASE | +----------+ | dbtest | +----------+

mysql> use dbtest; Database changed mysql> show tables; +------------------+ | Tables_in_dbtest | +------------------+ | tb1 | | tb2 | +------------------+

看到没,Cobar让原本在虚拟机192.168.213.201上的三个数据库dbtest1,dbtest2,dbtest3从外表上看转移到了192.168.213.9上来了,而且合并成了一个数据库dbtest,完了之后我们还可以对dbtest进行操作,比如我们按资料上来:

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; +----+---------------------+ | id | gmt | +----+---------------------+ | 1 | 2012-11-07 08:32:48 | +----+---------------------+

mysql> select * from tb2; +-----+-------+ | id | val | +-----+-------+ | 1 | part1 | | 2 | part1 | | 513 | part2 | +-----+-------+

看到没,跟操作传统数据是一样一样的,但是我们再到192.68.213.201上去看看,看这里发生了什么

mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | dbtest1 | | dbtest2 | | dbtest3 | | mysql | | test | | tpcw2 | +--------------------+

mysql> use dbtest1; Database changed mysql> show tables; +-------------------+ | Tables_in_dbtest1 | +-------------------+ | tb1 | +-------------------+

mysql> select * from tb1; +----+---------------------+ | id | gmt | +----+---------------------+ | 1 | 2012-11-07 08:32:48 | +----+---------------------+

mysql> use dbtest2; Database changed mysql> show tables; +-------------------+ | Tables_in_dbtest2 | +-------------------+ | tb2 | +-------------------+

mysql> select * from tb2; +----+-------+ | id | val | +----+-------+ | 1 | part1 | | 2 | part1 | +----+-------+

mysql> use dbtest3; Database changed mysql> show tables; +-------------------+ | Tables_in_dbtest3 | +-------------------+ | tb2 | +-------------------+

mysql> select * from tb2; +-----+-------+ | id | val | +-----+-------+ | 513 | part2 | +-----+-------+

看,tb2因为是dbtest2,dbtest3共同存储,因此在192.168.213.9上的insert  tb2的操作就被分流写入到这两个数据库中了(这里Cobar用的是hash然后取模)。
是不是很强大。。。。。
4.多余的话:
跟阿里官方给出的例子不同,我自己弄的时候是将Cobar部署在于数据库不同的机子上,不过我也将Cobar单节点部署试用过了,需要说明的是,对于单节点部署,schema.xml的配置和多机部署不同,下面我一一给出解释:
1.单节点部署
我上面已经说过阿里给出的官方例子有点问题(我不知道我理解对没!!),他那个例子单机部署用的是本机IP,其实这有点多此一举,因为mysql默认是监听127.0.0.1的,所以对于单节点配置,schema.xml中数据源部分应该是这样:

<dataSource name="dsTest" type="mysql"> <property name="location"> <location>127.0.0.1:3306/dbtest1</location> <location>127.0.0.1:3306/dbtest2</location> <location>127.0.0.1:3306/dbtest3</location> </property> <property name="user">root</property> <property name="password">111111</property> <property name="sqlMode">STRICT_TRANS_TABLES</property> </dataSource>

然后启动之后直接进入即可

# mysql -h 127.0.0.1 -utest -ptest -P8066 -Ddbtest

至于你非要用本机IP可不可以,我下面会说到。
2.多机部署
跟单节点部署不同,多机部署涉及到mysql的远程连接的问题,当然阿里官方默认我们用户已经知道这些,好吧!
如我的用例,192.168.213.201当做数据库实际存放点,192.168.213.9只是Cobar虚拟的数据库,因此配置schema.xml在后者,也就是如下:

<dataSource name="dsTest" type="mysql"> <property name="location"> <location>192.168.213.201:3306/dbtest1</location> <location>192.168.213.201:3306/dbtest2</location> <location>192.168.213.201:3306/dbtest3</location> </property> <property name="user">root</property> <property name="password"></property> <property name="sqlMode">STRICT_TRANS_TABLES</property> </dataSource>

而这需要192.168.213.9能连接到192.168.213.201的mysql,这是需要配置的,我们需要给前者访问权限,这里需要用grant命令,好吧我当复习下功课:( 注意这里我设置登录密码为空,所有schema.xml的password选项填空

mysql>grant all on *.* to root@'192.168.213.9' identified by '';

即把该实例中所有数据库的所有操作权限(*.*)全部给192.168.213.9root用户,而且不用密码(identified by ''

然后我们就可以在database——mysql中的user表里查看了

mysql> select user, host,password from user; +------+-----------------------+-------------------------------------------+ | user | host | password | +------+-----------------------+-------------------------------------------+ | root | localhost | *FD571203974BA9AFE270FE62151AE967ECA5E0AA | | root | localhost.localdomain | *FD571203974BA9AFE270FE62151AE967ECA5E0AA | | | localhost.localdomain | | | | localhost | | | root | 192.168.213.201 | | | root | 192.168.213.9 | | | root | node9 | | +------+-----------------------+-------------------------------------------+

完了之后就可以像上面一样配置了。
好了,我们再来看下为啥我说用本机IP配置多此一举,我已经说过mysql默认允许localhost(127.0.0.1)登录的,如果你再用本机IP,你就需要再grant权限给192.168.213.201,这里我也不太确定,因为我这样弄了之后没成功,但是明明可以用127.0.0.1配置,为啥还要用本机IP呢?所以,我上面那样说。。。
好了,就白话这么多了,哦,对了,Cobar集群我还没用过,有时间再玩玩。。据说Cobar来源于Amoeba,后者是原阿里大牛陈思儒一个人开发的一个分布式数据库解决方案,在陈离职去盛大之后阿里召集人员进行改进,然后就有了Cobar。
按阿里的说法,分布式数据库中间件分为三个部分:Cobar——负责解决分布式数据库的性能,容量和高可用性,E&E(Erosa和Eromanga)——负责数据消费的时效性,Otter——负责跨机房的数据同步。可惜只开源了第一个。。。唉。。
以后努力进我大阿里啊。。。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值