这篇来学习下Cobar的具体使用配置
场景:
系统对外提供的数据库名是UserCenter,并且其中有一张表u_user
该表的一部分数据被映射到物理数据库alicobar_test_master的u_user上,另外一部分数据被映射到物理数据库alicobar_test_shard的u_user上。
一.环境准备
OS:CentOS6.4 64位
DB:MySQL5.6.22
JDK:1.7
Cobar:1.2.7
MySQL-master 192.168.89.4 端口3306 用户名:alicobar 密码:alicobarpwd123
MySQL-backup 192.168.89.5 端口3306 用户名:alicobar 密码:alicobarpwd123
cobar-server 192.168.66.89
schema:alicobar_test_master、alicobar_test_shard,table:u_user 脚本如下:
CREATE DATABASE /*!32312 IF NOT EXISTS*/`alicobar_test_master` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `alicobar_test_master`;
/*Table structure for table `u_user` */
CREATE TABLE `u_user` (
`code` bigint(19) NOT NULL,
`mobile` varchar(32) DEFAULT NULL,
`email` varchar(64) DEFAULT NULL,
`nickname` varchar(64) DEFAULT NULL,
`password` varchar(32) NOT NULL,
`create_time` datetime NOT NULL,
`modify_time` datetime DEFAULT NULL,
`status` int(10) NOT NULL COMMENT '用户状态:1 = 启用;2 = 禁用;... ...',
PRIMARY KEY (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='--用户信息'
CREATE DATABASE /*!32312 IF NOT EXISTS*/`alicobar_test_shard` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `alicobar_test_shard`;
/*Table structure for table `u_user` */
CREATE TABLE `u_user` (
`code` bigint(19) NOT NULL,
`mobile` varchar(32) DEFAULT NULL,
`email` varchar(64) DEFAULT NULL,
`nickname` varchar(64) DEFAULT NULL,
`password` varchar(32) NOT NULL,
`create_time` datetime NOT NULL,
`modify_time` datetime DEFAULT NULL,
`status` int(10) NOT NULL COMMENT '用户状态:1 = 启用;2 = 禁用;... ...',
PRIMARY KEY (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='--用户信息';
二.部署和配置
MySQL主主同步就不再这里详细说明了,可以参考官网手册
1.安装JDK配置JAVA环境变量JAVA_HOME
下载JDK1.7 http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html
安装过程就不说了,下面简单说下安装后配置
vi /etc/profile 在文本末尾添加
export JAVA_HOME=/usr/java/jdk1.7.0_75
export JRE_HOME=/usr/java/jdk1.7.0_75/jre
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$JAVA_HOME/bin:$PATH
添加java_home环境变量后,使profile立即生效
source /etc/profile
2.安装cobar
下载地址:https://github.com/alibaba/cobar/wiki
下载Cobar压缩文件并解压,进入conf目录可以看到schema.xml, rule.xml, server.xml等相关的配置文件
schema.xml配置如下(注意:schema.xml包含MySQL的IP、端口、用户名、密码等配置,您需要按照注释替换为您的MySQL信息。)
[root@localhost conf]# more 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="UserCenter" dataNode="node1">
<table name="u_user" dataNode="node1,node2" rule="rule1" />
</schema>
<!-- 数据节点定义,数据节点由数据源和其他一些参数组织而成。-->
<dataNode name="node1">
<property name="dataSource">
<dataSourceRef>source-master[0]</dataSourceRef>
<dataSourceRef>source-backup[0]</dataSourceRef>
</property>
<!--Cobar与后端数据源连接池大小设置-->
<property name="poolSize">256</property>
<!--Cobar通过心跳来实现后端数据源HA,一旦主数据源心跳失败,便切换到备数据源上工作-->
<!--Cobar心跳是通过向后端数据源执行一条SQL语句,根据该语句的返回结果判断数据源的运行情况-->
<property name="heartbeatSQL">select user()</property>
</dataNode>
<dataNode name="node2">
<property name="dataSource">
<dataSourceRef>source-master[1]</dataSourceRef>
<dataSourceRef>source-backup[1]</dataSourceRef>
</property>
<!--Cobar与后端数据源连接池大小设置-->
<property name="poolSize">256</property>
<!--Cobar通过心跳来实现后端数据源HA,一旦主数据源心跳失败,便切换到备数据源上工作-->
<!--Cobar心跳是通过向后端数据源执行一条SQL语句,根据该语句的返回结果判断数据源的运行情况-->
<property name="heartbeatSQL">select user()</property>
</dataNode>
<!-- 数据源定义,数据源是一个具体的后端数据连接的表示。-->
<dataSource name="source-master" type="mysql">
<property name="location">
<location>192.168.89.4:3306/alicobar_test_master</location> <!--注意:替换为您的MySQL IP和Port-->
<location>192.168.89.4:3306/alicobar_test_shard</location>
</property>
<property name="user">alicobar</property> <!--注意:替换为您的MySQL用户名-->
<property name="password">alicobarpwd123</property> <!--注意:替换为您的MySQL密码-->
<property name="sqlMode">STRICT_TRANS_TABLES</property>
</dataSource>
<dataSource name="source-backup" type="mysql">
<property name="location">
<location>192.168.89.5:3306/alicobar_test_master</location>
<location>192.168.89.5:3306/alicobar_test_shard</location>
</property>
<property name="user">alicobar</property>
<property name="password">alicobarpwd123</property>
<property name="sqlMode">STRICT_TRANS_TABLES</property>
</dataSource>
</cobar:schema>
rule.xml配置如下(本文以数字类型的code字段作为拆分字段,将数据拆分到两个库中。)
more 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>code</columns>
<algorithm><![CDATA[ func1(${code}) ]]></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配置如下
more server.xml
<?xml version="1.0" encoding="UTF-8"?>
<!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">UserCenter</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>
三.启动和使用Cobar
1.启动Cobar,进入bin目录可以看到Cobar的启动、停止与重启脚本
报没有log目录,我们新建一个,再次启动
查看logs目录下stdout.log, 启动成功日志如下
[root@localhost logs]# more stdout.log
17:31:10,939 INFO ===============================================
17:31:10,940 INFO Cobar is ready to startup ...
17:31:10,940 INFO Startup processors ...
17:31:10,974 INFO Startup connector ...
17:31:10,975 INFO Initialize dataNodes ...
17:31:11,140 INFO node1:0 init success
17:31:11,142 INFO node2:0 init success
17:31:11,150 INFO CobarManager is started and listening on 9066
17:31:11,152 INFO CobarServer is started and listening on 8066
17:31:11,152 INFO ===============================================
访问Cobar同访问MySQL的方式完全相同
常用访问方式如下(注意:本文将Cobar部署在192.168.66.89这台机器上,否则请替换为您的Cobar所在IP,其他信息不变)
SQL执行语句时与使用传统单一数据库无区别
[root@localhost cobar-server-1.2.7]# mysql -h192.168.66.89 -utest -ptest -P8066 -DUserCenter
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.1.48-cobar-1.2.7 Cobar Server (ALIBABA)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+------------+
| DATABASE |
+------------+
| UserCenter |
+------------+
1 row in set (0.00 sec)
mysql> show tables;
+----------------------+
| Tables_in_UserCenter |
+----------------------+
| u_user |
+----------------------+
1 row in set (0.05 sec)
mysql>
至此配置完成!