因为tddl动态数据源强依赖diamond配置中心,该依赖项目已经开源.请到http://code.taobao.org/p/diamond/src/checkout代码,下载使用文档(http://code.taobao.org/p/diamond/wiki/index/)按步骤部署.
Jdk1.6安装.
Mysql5.1.x或者5.5.x安装
Maven2settings.xml修改(见附录)
Tddl动态数据源的开源代码中,有一个tddl-sample工程,该工程演示了如何使用tddl动态数据源.但需要一些配置支持,以下说明需要做的配置.
(1).建库qatest_normal_0:
dropdatabaseifexistsqatest_normal_0;
CREATEDATABASE`qatest_normal_0`
(2).在库qatest_normal_0中建表normaltbl_0001
USEqatest_normal_0;
CREATETABLE`normaltbl_0001`(
`pk`int(11)NOTNULL,
`id`int(11)DEFAULTNULL,
`gmt_create`dateDEFAULT’2010-12-17′,
`name`varchar(30)DEFAULTNULL,
`floatCol`float(9,3)DEFAULT’0.000′,
PRIMARYKEY(`pk`)
)ENGINE=InnoDBDEFAULTCHARSET=latin1;
(3).在库qatest_normal_0中创建tddl用户,授予读写数据库权限
CREATEUSER’tddl’@'%’IDENTIFIEDBY’tddl’;
GRANTInsert,Update,Select,DeleteONqatest_normal_0.*TO’tddl’@'%’;
Global配置
dataId:com.taobao.tddl.atom.global.qatest_normal_0
group:DEFAULT_GROUP
content:
ip=127.0.0.1
port=3306
dbName=qatest_normal_0
dbType=mysql
dbStatus=RW
App配置
dataId:com.taobao.tddl.atom.app.tddl_sample.qatest_normal_0
group:DEFAULT_GROUP
content:
userName=tddl
minPoolSize=1
maxPoolSize=2
idleTimeout=10
blockingTimeout=5
preparedStatementCacheSize=15
connectionProperties=characterEncoding=gbk
User配置(dataId最后一段’.tddl’和用户名紧相关,如果数据库用户名不是tddl,请修改这个dataId)
dataId:com.taobao.tddl.atom.passwd.qatest_normal_0.mysql.tddl
group:DEFAULT_GROUP
content:
encPasswd=xxxxxxx(密文,请用tddl-atom-datasource工程下的JbossPasswordDecode加密下明文密码)
<dependency>
<groupId>com.taobao.tddl</groupId>
<artifactId>tddl-group-datasource</artifactId>
<version>3.0.1.5.taobaocode-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.taobao.tddl</groupId>
<artifactId>tddl-atom-datasource</artifactId>
<version>3.0.1.5.taobaocode-SNAPSHOT</version>
</dependency>
TAtomDataSourcetAtomDataSource=newTAtomDataSource();
tAtomDataSource.setAppName(appName);//appName是当前业务的名称
tAtomDataSource.setDbKey(dbKey);//dbKey是dba告知业务的当前数据库实例的名字(用于标志唯一的数据库)
TAtomDataSource.init();
(1)建库qatest_normal_0
dropdatabaseifexistsqatest_normal_0;
CREATEDATABASE`qatest_normal_0`
(2)在库qatest_normal_0中建表normaltbl_0001
USEqatest_normal_0;
CREATETABLE`normaltbl_0001`(
`pk`int(11)NOTNULL,
`id`int(11)DEFAULTNULL,
`gmt_create`dateDEFAULT’2010-12-17′,
`name`varchar(30)DEFAULTNULL,
`floatCol`float(9,3)DEFAULT’0.000′,
PRIMARYKEY(`pk`)
)ENGINE=InnoDBDEFAULTCHARSET=latin1;
(3)建库qatest_normal_0_bac
dropdatabaseifexistsqatest_normal_0_bac;
CREATEDATABASE`qatest_normal_0_bac`;
(4)在库qatest_normal_0_bac中建表normaltbl_0001
USEqatest_normal_0_bac;
CREATETABLE`normaltbl_0001`(
`pk`int(11)NOTNULL,
`id`int(11)DEFAULTNULL,
`gmt_create`dateDEFAULT’2010-12-17′,
`name`varchar(30)DEFAULTNULL,
`floatCol`float(9,3)DEFAULT’0.000′,
PRIMARYKEY(`pk`)
)ENGINE=InnoDBDEFAULTCHARSET=latin1;
(5).在以上两个数据库中创建tddl用户,授予读写数据库权限
CREATEUSER’tddl’@'%’IDENTIFIEDBY’tddl’;
GRANTInsert,Update,Select,DeleteONqatest_normal_0.*TO’tddl’@'%’;
GRANTInsert,Update,Select,DeleteONqatest_normal_0_bac.*TO’tddl’@'%’;
(1)在diamond中配置Group一组对等的数据的读写权重
dataId:com.taobao.tddl.jdbc.group_V2.4.1_group_sample
group:DEFAULT_GROUP
content:
qatest_normal_0:r10w10,qatest_normal_0_bac:r10w0
(2)在diamond中配置Atom(qatest_normal_0)数据源
Global配置
dataId:com.taobao.tddl.atom.global.qatest_normal_0
group:DEFAULT_GROUP
content:
ip=127.0.0.1
port=3306
dbName=qatest_normal_0
dbType=mysql
dbStatus=RW
App配置
dataId:com.taobao.tddl.atom.app.tddl_sample.qatest_normal_0
group:DEFAULT_GROUP
content:
userName=tddl
minPoolSize=1
maxPoolSize=2
idleTimeout=10
blockingTimeout=5
preparedStatementCacheSize=15
connectionProperties=characterEncoding=gbk
User配置(dataId最后一段’.tddl’和用户名紧相关,如果数据库用户名不是tddl,请修改这个dataId)
dataId:com.taobao.tddl.atom.passwd.qatest_normal_0.mysql.tddl
group:DEFAULT_GROUP
content:
encPasswd=xxxxxxx(密文,请用tddl-atom-datasource工程下的JbossPasswordDecode加密下明文密码)
(3)在diamond中配置Atom(qatest_normal_0_bac)数据源
Global配置
dataId:com.taobao.tddl.atom.global.qatest_normal_0_bac
group:DEFAULT_GROUP
content:
ip=127.0.0.1
port=3306
dbName=qatest_normal_0_bac
dbType=mysql
dbStatus=WR
App配置
dataId:com.taobao.tddl.atom.app.tddl_sample.qatest_normal_0_bac
group:DEFAULT_GROUP
content:
userName=tddl
minPoolSize=1
maxPoolSize=2
idleTimeout=10
blockingTimeout=5
preparedStatementCacheSize=15
connectionProperties=characterEncoding=gbk
User配置(dataId最后一段’.tddl’和用户名紧相关,如果数据库用户名不是tddl,请修改这个dataId)
dataId:com.taobao.tddl.atom.passwd.qatest_normal_0_bac.mysql.tddl
group:DEFAULT_GROUP
content:
encPasswd=xxxxxxx(密文,请用tddl-atom-datasource工程下的JbossPasswordDecode加密下明文密码)
<dependency>
<groupId>com.taobao.tddl</groupId>
<artifactId>tddl-group-datasource</artifactId>
<version>3.0.1.5.taobaocode-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.taobao.tddl</groupId>
<artifactId>tddl-atom-datasource</artifactId>
<version>3.0.1.5.taobaocode-SNAPSHOT</version>
</dependency>
TGroupDataSourceds=newTGroupDataSource(dbGroupKey,appName);
ds.init();
请修改MAVEN/conf/settings.xml文件,确保使用正确的MavenRepository。
目前我们将Tddl动态数据源发布在淘蝌蚪的mavenrepository中。
<?xmlversion=”1.0″encoding=”UTF-8″?>
<settingsxmlns=”http://maven.apache.org/SETTINGS/1.0.0″
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”
http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd”>
……
<servers>
<server>
<id>taocodeReleases</id>
<username>admin</username>
<password>admintaocode321</password>
</server>
<server>
<id>taocodeSnapshots</id>
<username>admin</username>
<password>admintaocode321</password>
</server>
</servers>
…
<profiles>
……
<profile>
<id>opensource</id>
<repositories>
<repository>
<id>taocodeReleases</id>
<name>taocodenexus</name>
<url>http://mvnrepo.code.taobao.org/nexus/content/repositories/releases/</url>
</repository>
<repository>
<id>taocodeSnapshots</id>
<name>taocodenexus</name>
<url>http://mvnrepo.code.taobao.org/nexus/content/repositories/snapshots/</url>
</repository>
</repositories>
</profile>
</profiles>
……
<activeProfiles>
<activeProfile>opensource</activeProfile>
</activeProfiles>
</settings>