-
Notifications
You must be signed in to change notification settings - Fork 717
ZebraSample示例说明
junior_xin edited this page Dec 5, 2018
·
4 revisions
demo提供本地化的Zebra使用演示, 通过spring+zebra-dao的方式进行数据库访问,分别可以执行 shard, group, single的功能, 执行入口位于test目录下。
在src/main/resources目录下创建app.properties作为应用名配置
app.name=zebra_ut
在src/main/resource目录下创建region目录,其中包含2个文件center.json idc.json center.json
{
"regions" : [
{"region" : "region1", "centers" : [
{"center":"center1","priority":2, "idcs":[
{"idc" : "idc1", "desc" : "机房1"},
{"idc" : "idc2", "desc" : "机房2"},
{"idc" : "idc3", "desc" : "机房3"}
]},
{"center":"center2","priority":1,"idcs":[
{"idc" : "idc4", "desc" : "机房4"}
]}
]},
{"region" : "region2", "centers" : [
{"center":"center3","priority":1,"idcs":[
{"idc" : "idc5", "desc" : "机房5"},
{"idc" : "idc6", "desc" : "机房6"}
]}
]}
]
}
idc.json
{
"regions" : [
{"region" : "region1", "idcs" : [
{"idc" : "idc1", "net" : ["192.1", "192.2"], "desc" : "机房1"},
{"idc" : "idc2", "net" : ["192.3", "192.4"], "desc" : "机房2"},
{"idc" : "idc3", "net" : ["192.5"], "desc" : "机房3"},
{"idc" : "idc4", "net" : ["192.6"], "desc" : "机房4"}
]},
{"region" : "region2", "idcs" : [
{"idc" : "idc5", "net" : ["192.10", "192.11"], "desc" : "机房5"},
{"idc" : "idc6", "net" : ["192.20", "192.21"], "desc" : "机房6"}
]}
]
}
zebra一些系统配置项,包括限流的配置
zebra_sample.zebra.system.retryTimes=3
数据新建完毕后,在src/main/resources目录下创建数据库的配置文件(zebra抽象为jdbcRef,并根据jdbcref找到数据库相关配置,访问数据库),文件名必须以jdbcRef.properties格式命名,例如jdbcRef="sample",那么创建sample.properties配置文件
zebra.group.sample=\
<groupConfig>\
<singleConfig>\
<name>sample-n1</name>\
<writeWeight>0</writeWeight>\
<readWeight>-1</readWeight>\
</singleConfig>\
<singleConfig>\
<name>sample-n2</name>\
<writeWeight>-1</writeWeight>\
<readWeight>1</readWeight>\
</singleConfig>\
<singleConfig>\
<name>sample-n3</name>\
<writeWeight>-1</writeWeight>\
<readWeight>1</readWeight>\
</singleConfig>\
</groupConfig>
#zebra.ds.sample-n1
zebra.ds.sample-n1={\
"active" : "true",\
"url" : "jdbc:mysql://127.0.0.1:3306/sample?characterEncoding=UTF8&socketTimeout=60000",\
"username" : "root",\
"password" : "123456",\
"driverClass":"com.mysql.jdbc.Driver",\
"properties" : "idleConnectionTestPeriod=80&acquireRetryAttempts=50&acquireRetryDelay=300&maxStatements=1"\
}
#zebra.ds.sample-n2
zebra.ds.sample-n2={\
"active" : "true",\
"url" : "jdbc:mysql://127.0.0.1:3306/sample?characterEncoding=UTF8&socketTimeout=60000",\
"username" : "root",\
"password" : "123456",\
"driverClass":"com.mysql.jdbc.Driver",\
"properties" : "idleConnectionTestPeriod=80&acquireRetryAttempts=50&acquireRetryDelay=300&maxStatements=1"\
}
#zebra.ds.sample-n3
zebra.ds.sample-n3 = {\
"active" : "true",\
"url" : "jdbc:mysql://127.0.0.1:3306/sample?characterEncoding=UTF8&socketTimeout=60000",\
"username" : "root",\
"password" : "123456",\
"driverClass":"com.mysql.jdbc.Driver",\
"properties" : "idleConnectionTestPeriod=80&acquireRetryAttempts=50&acquireRetryDelay=300&maxStatements=1"\
}
zkserver=localhost:2181
在src/test/java/com/dianping/zebra/sample目录下存在3个sample类 分别演示了如何使用spring+zebra的方式访问数据库
GroupDataSourceSample是读写分离的示例
ShardDataSourceSample是分库分表的示例
SingleDataSourceSample是单数据源示例
create database zebra;
User表
CREATE TABLE `User` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`Name` varchar(32) DEFAULT NULL,
`Mis` varchar(20) NOT NULL ,
`Email` varchar(128) DEFAULT NULL,
`UpdateTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`CreateTime` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `IX_Mis` (`Mis`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Feed分表 表名为Feed0,Feed1,Feed2,Feed3
CREATE TABLE `Feed0` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`Uid` varchar(30) NOT NULL DEFAULT '',
`UpdateTime` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;