Cobar的配置及使用Demo

Cobar是基于MySQL的分布式数据库服务中间件,它可以让传统的数据库得到良好的线性扩展,并看上去还是一个数据库,对应用保持透明。

  • 产品在阿里巴巴稳定运行3年以上。
  • 接管了3000+个MySQL数据库的schema。
  • 集群日处理在线SQL请求50亿次以上。
  • 集群日处理在线数据流量TB级别以上。

官方Github地址:https://github.com/alibaba/cobar
Cobar下载地址:https://github.com/alibaba/cobar/releases

使用手册

【来自官方DOC: Cobar-Alibaba Open Sesame.pdf

目录结构

  • 基本目录
    下载最新Cobar压缩包,解压后,进入cobar-server-1.2.7目录,可以看到Cobar的主要目录如下:
bin     #包含Cobar的启动、重启、停止等脚本文件
conf    #包含Cobar所有配置文件
lib     #包含Cobar及其依赖的jar文件
logs    #包含Cobar所有日志文件(该目录需手动添加)
  • 启动脚本
    Cobar的所有启动和停止脚本全部放在bin目录中,进入bin目录,可以看到:
startup.sh    #Linux环境启动脚本
startup.bat   #Windows环境启动脚本
restart.sh    #Linux环境重启脚本
shutdown.sh   #Linux环境停止脚本
  • 配置文件
    Cobar的所有配置文件全部放在conf目录中,进入conf目录,可以看到:
server.xml    #Cobar系统、用户、集群等相关配置
schema.xml    #schema, dataNode, dataSource相关配置
rule.xml      #分布式规则定义
log4j.xml     #日志相关配置

Demo

在本机上进行测试,故MySQL服务器IP为127.0.0.1:3306,用户名和密码都为root。

Step1:数据准备,数据库创建脚本

需要创建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));

Step2:下载软件并解压

wget -c "https://github.com/alibaba/cobar/releases/download/v1.2.7/cobar-server-1.2.7.tar.gz" -O cobar-server-1.2.7.tar.gz
tar zxf cobar-server-1.2.7.tar.gz

Step3:配置

  • 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>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">root</property>
    <property name="sqlMode">STRICT_TRANS_TABLES</property>
  </dataSource>

</cobar:schema>

在schema.xml里主要配置了数据源以及数据节点,对于schema的配置里可以为table指定路由规则,至于路由规则的来源下面会有说明。如果所有数据源的用户名与密码相同可以配置在同一datasource节点下。系统会将其组装为一个数组,当为数据节点指定数据源时只需用数组下标以及数据源名称表示即可例如dsTest[1]。同时可以为数据节点指定心跳检测运行情况,当主数据源出现异常的时候便可切换到备数据源,遗憾的是当主数据源恢复正常的时候不能自动切换会主数据源,除非备数据源也发生异常。

  • rule.xml
    本文仅以数字类型的id字段作为拆分字段,将数据拆分到两个库中。
    源码里只提供了根据long型数据分区,根据String类型数据分区,根据FileMap分区。
<?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: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
<?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:server SYSTEM "server.dtd">
<cobar:server xmlns:cobar="http://cobar.alibaba.com/">
  
  <!-- 系统参数定义,服务端口、管理端口,处理器个数、线程池等。 -->
  <!--
  <system>
    <property name="serverPort">8066</property>
    <property name="managerPort">9066</property>
    <property name="initExecutor">16</property>
    <property name="timerExecutor">4</property>
    <property name="managerExecutor">4</property>
    <property name="processors">4</property>
    <property name="processorHandler">8</property>
    <property name="processorExecutor">8</property>
    <property name="clusterHeartbeatUser">_HEARTBEAT_USER_</property>
    <property name="clusterHeartbeatPass">_HEARTBEAT_PASS_</property>
  </system>
  -->

  <!-- 用户访问定义,用户名、密码、schema等信息。 -->
  <user name="test">
    <property name="password">test</property>
    <property name="schemas">dbtest</property>
  </user>
  <!--
  <user name="root">
    <property name="password"></property>
  </user>
  -->

  <!-- 集群列表定义,指定集群节点的主机和权重,用于集群间的心跳和客户端负载均衡。 -->
  <!-- 
  <cluster>
    <node name="cobar1">
      <property name="host">127.0.0.1</property>
      <property name="weight">1</property>
    </node>
  </cluster>
   -->
   
  <!-- 隔离区定义,可以限定某个主机上只允许某个用户登录。 -->
  <!--
  <quarantine>
    <host name="1.2.3.4">
      <property name="user">test</property>
    </host>
  </quarantine>
  -->

</cobar:server>

Step4:启动

运行bin目录下的startup.sh脚本,启动成功后,可打印查看logs目录下的日志文件:


日志文件(stdout.log)

Step5:连接数据库

并可通过mysql命令登录Cobar,指定主机为安装Cobar的Server,username和password则是Cobar server.xml里设置的用户名和密码

mysql -h127.0.0.1 -utest -ptest -P8066 -Ddbtest
  • 当前数据库如下图所示:


    查看数据库
  • 插入数据:
insert into tb1 (id, gmt) values (1, now());                   #向表tb1插入一条数据
insert into tb2 (id, val) values (1, "part1");                 #向表tb2插入一条数据
insert into tb2 (id, val) values (2, "part1"), (513, "part2"); #向表tb2同时插入多条数据

数据插入后在Cobar中的查询结果如下图所示:


数据插入结果
  • 查看后端MySQL数据库dbtest1,dbtest2和dbtest3,验证数据分布在不同的库中


    dbtest1.tb1

    dbtest2.tb2

    dbtest3.tb2

JDBC方式访问

支持用户使用JDBC连接池

jdbc:mysql://127.0.0.1:8066/testdb?user=test&password=test

也可以如下:

Class.forName("com.mysql.jdbc.Driver");
Connection conn =
DriverManager.getConnection("jdbc:mysql://127.0.0.1:8066/dbtest",
"test", "test") 
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,589评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,615评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,933评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,976评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,999评论 6 393
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,775评论 1 307
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,474评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,359评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,854评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,007评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,146评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,826评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,484评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,029评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,153评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,420评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,107评论 2 356

推荐阅读更多精彩内容