C-JDBC
C-JDBC(Java 數(shù)據(jù)庫連接群集)作為開發(fā)源碼的數(shù)據(jù)庫群集中間件,可以讓任何應(yīng)用程序通過JDBC能夠透明的訪問數(shù)據(jù)庫群集。數(shù)據(jù)庫可以分布在多個(gè)節(jié)點(diǎn)并可以進(jìn)行數(shù)據(jù)復(fù)制,C-JDBC能夠均衡在這些節(jié)點(diǎn)之間的查詢負(fù)載。 C-JDBC通過軟件來實(shí)現(xiàn)RAIDb(廉價(jià)數(shù)據(jù)庫冗余陣列)。C-JDBC是GNU LGPL許可證下的ObjectWeb項(xiàng)目。
http://c-jdbc.objectweb.org/網(wǎng)站上的C-JDBC-Brochure-EN.pdf寫的內(nèi)容挺好。(C-JDBC)tutorial.pdf內(nèi)容也挺翔實(shí)。
大型應(yīng)用隨著用戶量訪問越來越大,增加數(shù)據(jù)庫存儲和做好數(shù)據(jù)庫冗余可以增加系統(tǒng)的可靠性和性能。
無需修改客戶端應(yīng)用程序,無需變更應(yīng)用服務(wù)器或數(shù)據(jù)庫服務(wù)器軟件。C-JDBC的JDBC driver把SQL請求轉(zhuǎn)給C-JDBC controller(負(fù)責(zé)在databases之間做負(fù)載均衡)
The database is distributed and replicated among several nodes and C-JDBC load balances the queries between these nodes.
就這一句話就太誘人了!
上周的某一個(gè)上午,搞定C-JDBC的安裝配置啦~
主要根據(jù)官方的user guide和這篇好文: http://huaronghu.spaces.live.com/blog/cns!B9A68E1C1CA857AD!288.entry
準(zhǔn)備工作:
環(huán)境:windows,postgreSQL,JDK
建立數(shù)據(jù)庫“們”,安裝若干db在若干server上,所以如果使用商業(yè)數(shù)據(jù)庫的話,就會需要好幾套的錢哪。
安裝C-JDBC:
下載了binary版的c-jdbc-2.0.2-bin.zip,解壓到作為C-JDBC server的機(jī)器上(e.g. C:\software\c-jdbc-2.0.2-bin),
設(shè)置環(huán)境變量,新增系統(tǒng)變量:CJDBC_HOME =C:\software\c-jdbc-2.0.2-bin
把實(shí)際使用的數(shù)據(jù)庫的JDBC driver(例如mysql-connector-java-3.1.12-bin.jar ,postgresql-8.0.309.jdbc2.jar)拷貝到c-jdbc/drivers 中
配置文件:
在c-jdbc-2.0.2-bin/config/virtualdatabase目錄中創(chuàng)建虛擬數(shù)據(jù)庫配置文件。
里面DatabaseBackend 標(biāo)簽中定義的就是被集群的數(shù)據(jù)庫。
試驗(yàn)時(shí),創(chuàng)建了文件:postgresql-raidb1-distribution.xml,使用了2個(gè)postgreSQL數(shù)據(jù)庫,采用了全復(fù)制的方法。
注意,如果采用全復(fù)制,所以在2個(gè)數(shù)據(jù)庫上建庫、建表的全部腳本都要完全相同,否則將會報(bào)出mismatch的錯(cuò)誤。
目前感覺配置里面最有用的配置就是<AuthenticationManager>,<DatabaseBackend>和<LoadBalancer>(還沒有仔細(xì)看)。
配置時(shí)可以參考已經(jīng)提供的例子,c-jdbc-2.0.2-bin/config/RAIDb-1-full-example.xml,里面對每一個(gè)屬性都有比較詳細(xì)的注釋,DatabaseBackend中還有各個(gè)開源數(shù)據(jù)庫連接的例子,挺貼心的。





















































?
在c-jdbc-2.0.2-bin/config/controller目錄中創(chuàng)建一個(gè)c-jdbc controller的配置文件。
試驗(yàn)時(shí),創(chuàng)建的配置文件為:uud-controller-distributed.xml











?
注意:Controller和VirtualMachine的配置文件不能采用相同的文件名
啟動(dòng):
在c-jdbc-2.0.2-bin\bin目錄下,運(yùn)行controller.bat -f ../config/controller/uud-controller-distributed.xml
unix用戶使用controller.sh來啟動(dòng)controller
截圖:(下回補(bǔ))
湊合看一下吧,控制臺的正常輸出:
C:\software\c-jdbc-2.0.2-bin\bin>controller.bat -f ../config/controller/uud-cont
roller-distributed.xml
2007-03-07 15:16:44,017 INFO? controller.core.Controller C-JDBC controller (2.0.
2)
2007-03-07 15:16:44,439 INFO? controller.core.Controller Loading configuration f
ile: ../config/controller/uud-controller-distributed.xml
2007-03-07 15:16:44,752 INFO? controller.core.Controller JMX is enabled
2007-03-07 15:16:44,830 INFO? controller.core.Controller Starting JMX server on
host: 10.10.0.155
2007-03-07 15:16:46,376 INFO? backend.DatabaseBackend.postgreSQLNode114 Adding c
onnection manager for virtual user "boss"
2007-03-07 15:16:46,501 INFO? backend.DatabaseBackend.postgreSQLNode155 Adding c
onnection manager for virtual user "boss"
2007-03-07 15:16:46,642 INFO? controller.RequestManager.myDB Request manager wil
l parse requests with the following granularity: TABLE
2007-03-07 15:16:46,657 WARN? controller.virtualdatabase.myDB No recovery log ha
s been configured, enabling backend without checkpoint.
2007-03-07 15:16:48,641 INFO? backend.DatabaseBackend.postgreSQLNode114 Detected
?backend as: PostgreSQL
2007-03-07 15:16:49,063 WARN? backend.DatabaseBackend.postgreSQLNode114 Statemen
t.getGeneratedKeys not supported.
2007-03-07 15:16:49,579 INFO? backend.DatabaseBackend.postgreSQLNode114 Gatherin
g database schema
2007-03-07 15:16:49,782 INFO? controller.RequestManager.myDB Setting new virtual
?database schema.
2007-03-07 15:16:49,782 INFO? cjdbc.controller.cache Setting new database schema
.
2007-03-07 15:16:49,782 INFO? controller.loadbalancer.RAIDb1 Adding blocking tas
k worker thread for backend postgreSQLNode114
2007-03-07 15:16:49,797 INFO? controller.loadbalancer.RAIDb1 Adding non blocking
?task worker thread for backend postgreSQLNode114
2007-03-07 15:16:49,797 INFO? controller.RequestManager.myDB Database backend po
stgreSQLNode114 is now enabled
2007-03-07 15:16:50,922 INFO? backend.DatabaseBackend.postgreSQLNode155 Detected
?backend as: PostgreSQL
2007-03-07 15:16:51,328 WARN? backend.DatabaseBackend.postgreSQLNode155 Statemen
t.getGeneratedKeys not supported.
2007-03-07 15:16:52,172 INFO? backend.DatabaseBackend.postgreSQLNode155 Gatherin
g database schema
2007-03-07 15:16:52,390 INFO? controller.RequestManager.myDB Virtual database sc
hema merged with new schema.
2007-03-07 15:16:52,390 INFO? cjdbc.controller.cache Merging new database schema
2007-03-07 15:16:52,390 INFO? controller.loadbalancer.RAIDb1 Adding blocking tas
k worker thread for backend postgreSQLNode155
2007-03-07 15:16:52,390 INFO? controller.loadbalancer.RAIDb1 Adding non blocking
?task worker thread for backend postgreSQLNode155
2007-03-07 15:16:52,406 INFO? controller.RequestManager.myDB Database backend po
stgreSQLNode155 is now enabled
2007-03-07 15:16:52,422 WARN? VirtualDatabaseWorkerThread.myDB.metadata Metadata
?key [getMaxColumnsInIndex] is not compatible. (Backends are: [jdbc:postgresql:/
/10.10.0.114:5432/clusterdb] and [jdbc:postgresql://10.10.0.155:5432/clu
sterdb] ; Values are:[32] and [0])
2007-03-07 15:16:52,422 INFO? controller.core.Controller Adding VirtualDatabase
myDB
2007-03-07 15:16:52,437 INFO? controller.core.Controller Waiting for connections
?on 0.0.0.0:25322
2007-03-07 15:16:52,469 INFO? controller.core.Controller Controller started on 2
007.03.07 10 at 03:16:52 下午 GMT+08:00
2007-03-07 15:16:52,484 INFO? controller.core.Controller Controller 10.10.0.
155:25322 ready, listening to requests ...
遇到過的問題:
1、
剛啟動(dòng)沒一會兒,就看到config文件讀取錯(cuò)誤,控制臺信息也就此結(jié)束。
解決方法:controller.bat -f filename中filename的相對路徑和文件名竟然都寫錯(cuò)了,汗。。。。
2、
啟動(dòng)時(shí),控制臺拋出ERROR以及exception,大意是connection failed, IP沒有配置到pg_hba.conf中
解決方法:雖然是連本機(jī)的一個(gè)數(shù)據(jù)庫,但是由于數(shù)據(jù)庫連接的url里面寫的是IP地址,所以仍然要把本機(jī)的IP地址配置到pg_hba.conf以允許其訪問。
3、
還是啟動(dòng)時(shí)拋錯(cuò)了,Connection test failed(org.postgresql.util.PSQLException:Backend start-up failed:FATAL:role "boss_user" does not exist.)
解決方法:原來是在自己寫的.xml配置文件中,DatabaseBackend標(biāo)簽里面寫的用戶名和密碼需要是真實(shí)數(shù)據(jù)庫的真實(shí)用戶名和密碼。
最后都整好了,但是遇到一個(gè)warn的log:
backend.DatabaseBackend.postgreSQLNode155 Statement.getGeneratedKeys not supported
至今不知道是什么問題,有什么影響.
代碼部分:
只需要修改應(yīng)用程序用到的jdbc driver的配置,把c-jdbc/drivers/目錄下的c-jdbc-driver.jar拷貝到應(yīng)用程序的工程里,并加入到class-path中。
之前是使用 Class.forName("com.somevendor.jdbcDriver.Driver") 去連接某特定的數(shù)據(jù)庫
現(xiàn)在是使用 Class.forName("org.objectweb.cjdbc.driver.Driver") 統(tǒng)一處理。
連接字符串:DriverManager.getConnection("jdbc:cjdbc://localhost:25322/mycjdbc") 取得數(shù)據(jù)庫連接。
c-jdbc server稱為c-jdbc controller,聆聽請求們
其他所有通過c-jdbc來訪問的DB們稱為db backends.
jdbc:cjdbc://host1:port1,host2:port2/database
host是指跑著c-jdbc controller的機(jī)器IP,port是指controller監(jiān)聽客戶端鏈接的端口,默認(rèn)端口號:25322
注意,不僅真正的DB安裝多個(gè)在多個(gè)機(jī)器上,c-jdbc controller也可以安在不同的機(jī)器上,這樣可以防止一個(gè)c-jdbc 壞掉帶來的風(fēng)險(xiǎn)。
例如:




然后可以在代碼中insert/update幾個(gè)記錄,再單獨(dú)到各個(gè)數(shù)據(jù)庫中去查一下,應(yīng)該都受到影響就對了。
問題:
1、管理C-JDBC,不知道C-JDBC需要啥管理,不過好像是已經(jīng)有一個(gè)簡單的 Desktop Application 可以控制相關(guān)的 administration 介面, 最重要的是, 他結(jié)合了 JMX , 可以讓整個(gè)監(jiān)控的環(huán)境更完整.. (等待嘗試)
2、不知道通過C-JDBC來操作速度如何
3、RAIdb的3種類型,采取哪個(gè)更好?把表分開在不同server上對聯(lián)表查應(yīng)該不會有影響吧?
其他:
1、Configuring C-JDBC with Jakarta Tomcat
Copy the c-jdbc-driver.jar file to the lib directory of your web application (for example: $TOMCAT_HOME/webapps/mywebapp/WEB-INF/lib).
There are many ways to obtain connections from a Tomcat application. Just ensure that you are using org.objectweb.cjdbc.driver.Driver as the driver class name and that the JDBC URL is a C-JDBC URL
2、Configuring C-JDBC with Hibernate
C-JDBC just has to be defined as any JDBC driver in Hibernate, leaving the syntax set to the proper database. Here is a configuration example to use Hibernate with a C-JDBC cluster made of Sybase backends:
## C-JDBC
hibernate.dialect???????????????? net.sf.hibernate.dialect.SybaseDialect
hibernate.connection.driver_class org.objectweb.cjdbc.driver.Driver
hibernate.connection.username???? user
hibernate.connection.password???? pass
hibernate.connection.url????????? jdbc:cjdbc://localhost:25322/test???????
posted on 2007-03-05 20:42 cerulean 閱讀(1420) 評論(2) 編輯 收藏 所屬分類: DB