DBUtil教程之?dāng)?shù)據(jù)庫集群備份
在現(xiàn)在系統(tǒng)中,海量數(shù)據(jù)成為軟件工程師要面對(duì)的主要問題。很多人都把數(shù)據(jù)庫優(yōu)化集中在編程和數(shù)據(jù)庫的結(jié)構(gòu),再構(gòu)建一些索引和視圖,但是在編程和表結(jié)構(gòu)都到了極致的時(shí)候,數(shù)據(jù)庫優(yōu)化該何去何從。
Oracle公司現(xiàn)在有一個(gè)比較熱門的數(shù)據(jù)庫集群技術(shù),叫做rac。rac應(yīng)該是現(xiàn)在數(shù)據(jù)庫集群中最優(yōu)化和最熱門的技術(shù)。但是只是局限于同類型和同構(gòu)數(shù)據(jù)庫上面。
RAC是一個(gè)負(fù)載均衡的代表,它的集群主要是面對(duì)不是大而復(fù)雜的數(shù)據(jù)庫查詢,而是面對(duì)大量的連接。但是如果我們面對(duì)是一個(gè)異類異構(gòu)數(shù)據(jù)庫怎么解決。
近年來,有一種比較熱門的數(shù)據(jù)庫進(jìn)入我們生活,列式數(shù)據(jù)庫。它是一個(gè)高查詢性能,低插入性能的數(shù)據(jù)庫?,F(xiàn)在主要代表是sybaseiq,主要開源的 列式數(shù)據(jù)庫有monetdb 。但是列式數(shù)據(jù)庫問題很多,主要是插入慢、數(shù)據(jù)庫不穩(wěn)定、數(shù)據(jù)庫傳統(tǒng)語法支持不強(qiáng)、穩(wěn)定性低、線程低和擴(kuò)展性低。但是達(dá)到幾百萬行以上數(shù)據(jù)以后,列式數(shù)據(jù) 庫的查詢速度是mysql的10倍以上。
在一個(gè)大型系統(tǒng)中,行式和列式數(shù)據(jù)庫優(yōu)勢(shì)互補(bǔ),合理使用成為開發(fā)人員必須關(guān)心的問題。我們今天的集群也是主要面對(duì)這個(gè)問題。
集群原理:
在dbutil的集群中,我們是用了一個(gè)行式數(shù)據(jù)庫和若干個(gè)列式數(shù)據(jù)庫組成,每次更新數(shù)據(jù)的時(shí)候,我們是用廣播模式,把增刪改數(shù)據(jù)通過廣播模式,群 體更新所有的數(shù)據(jù)。當(dāng)查詢數(shù)據(jù)時(shí)候,我們會(huì)通過連接池模式,找出最少人使用的數(shù)據(jù),查詢數(shù)據(jù)。并且可以設(shè)置每天定時(shí)同步數(shù)據(jù),把存放在行式數(shù)據(jù)的數(shù)據(jù),定 時(shí)同步到所有的節(jié)點(diǎn)。
配置方法:
在我們之前幾個(gè)文章簡(jiǎn)單見過DBUtil的使用,配置方法和之前的教程大同小異。
1.配置xml
- <!-- 集群測(cè)試數(shù)據(jù)庫 -->
- <Resource jndi="jdbc/Cluster1" driverClass="com.mysql.jdbc.Driver"
- dbUserName="root" dbPassWord="root"
- jdbcUrl="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8"
- initSize="2" minPoolSize="1" maxPoolSize="10" maxStatements="50"
- maxIdleTime="60" />
- <Resource jndi="jdbc/Cluster2" driverClass="com.mysql.jdbc.Driver"
- dbUserName="root" dbPassWord="root"
- jdbcUrl="jdbc:mysql://127.0.0.1:3306/test2?useUnicode=true&characterEncoding=utf-8"
- initSize="2" minPoolSize="1" maxPoolSize="10" maxStatements="50"
- maxIdleTime="60" />
- <!-- 集群 -->
- <Cluster jndiCluster="jdbc/Cluster" mainDB="jdbc/Cluster1"
- synchronous="off" synchronousTime="00:00" protect="on">
- <Resource jndi="jdbc/Cluster1" />
- <Resource jndi="jdbc/Cluster2" />
- </Cluster>
配置xml也是首先配置2個(gè)數(shù)據(jù)庫節(jié)點(diǎn),然后加入集群。
- jndiCluster:是集群的唯一標(biāo)識(shí)符
- mainDB:集群的主數(shù)據(jù)庫,建議用mysql數(shù)據(jù)庫
- synchronous:同步數(shù)據(jù)
- synchronousTime:同步時(shí)間
- protect:保護(hù)模式(查詢數(shù)據(jù),不用主數(shù)據(jù)庫,只是獲取集群數(shù)據(jù)庫的數(shù)據(jù))
2.更新數(shù)據(jù)
- package com.shine.DBUtil;
- import com.shine.DBUtil.manage.DBManager;
- import com.shine.DBUtil.utils.ClusterList;
- public class ClusterUpdateExample {
- /**
- * 集群更新例子
- *
- * @param args
- */
- public static void main(String[] args) {
- System.out.println("ClusterUpdateExample...");
- DBUtil
- .getInstance()
- .init(
- "E:\\workspace\\JavaFramework2.5\\src\\com\\shine\\DBUtil\\config\\dbXml.xml");
- ClusterList list = DBManager.getInstance()
- .getClusterConnectionNameList("jdbc/Cluster");
- System.out.println("主數(shù)據(jù)庫為:" + list.getMainDB());
- String sql = "insert into test1(test) value('test')";
- DBUtil.getInstance().executeClusterUpdate("jdbc/Cluster", sql);
- }
- }
3.查詢數(shù)據(jù)
- package com.shine.DBUtil;
- import com.shine.DBUtil.model.DBModel;
- public class ClusterSelectExample {
- /**
- * 集群查詢
- *
- * @param args
- */
- public static void main(String[] args) {
- System.out.println("ClusterSelectExample...");
- DBUtil
- .getInstance()
- .init(
- "E:\\workspace\\JavaFramework2.5\\src\\com\\shine\\DBUtil\\config\\dbXml.xml");
- String sql = "select * from test1";
- String clusterJndi = "jdbc/Cluster";
- DBModel dbmodel1 = DBUtil.getInstance().executeClusterQuery(
- clusterJndi, sql);
- DBModel dbmodel2 = DBUtil.getInstance().executeClusterQuery(
- clusterJndi, sql);
- }
- }
實(shí)際上集群數(shù)據(jù)庫的同步最好是不要選擇定時(shí)同步,除非特殊需要,因?yàn)槿绻玫綌?shù)據(jù)庫集群,必然會(huì)有海量的數(shù)據(jù)。而DBUtil的定時(shí)同步是整庫同步,使用者應(yīng)該根據(jù)業(yè)務(wù)邏輯分表,部分同步比較實(shí)際。
原文鏈接:http://blog.csdn.net/arjick/article/details/6758872
【編輯推薦】