posts - 4,  comments - 0,  trackbacks - 0

          mysql主從復(fù)制配置見(jiàn): http://www.aygfsteel.com/howard-he/

          項(xiàng)目中使用mysql 主從復(fù)制,但是用程序?qū)崿F(xiàn)的讀寫(xiě)分離,代碼片段如下:

          1 public DataSource getDataSource(MethodType methodType) {
          2 if (methodType == MethodType.WRITE) {
          3 return getDataSource(MasterDataSources);
          4 } else {
          5 return getDataSource(SlaveDataSources);
          6 }
          7 }


          獲取數(shù)據(jù)源,首先你要確定MethodType 類(lèi)型,一個(gè)是讀,一個(gè)是寫(xiě)

          1public enum MethodType {
          2 READ, WRITE
          3}

          讀是獲取從庫(kù)數(shù)據(jù)源,寫(xiě)是獲取主庫(kù)數(shù)據(jù)源。

          這樣,就需要在jdbc配置配置兩個(gè)數(shù)據(jù)源(一主一從)

          還有另一種實(shí)現(xiàn)方式,不用程序來(lái)控制。mysql 驅(qū)動(dòng)包提供相應(yīng)的實(shí)現(xiàn) com.mysql.jdbc.ReplicationDriver.
          我寫(xiě)了一個(gè)簡(jiǎn)單的例子:

          1package com.howard.loadbalance;
          2
          3import java.sql.Connection;
          4import java.sql.ResultSet;
          5import java.sql.SQLException;
          6import java.sql.Statement;
          7import java.util.Properties;
          8import java.util.concurrent.ExecutorService;
          9import java.util.concurrent.Executors;
          10
          11import org.slf4j.Logger;
          12import org.slf4j.LoggerFactory;
          13
          14import com.mysql.jdbc.ReplicationDriver;
          15
          16public class MysqlTest {
          17
          18 public static class QueryRunnable implements Runnable {
          19
          20 protected final static Logger logger = LoggerFactory
          21 .getLogger(QueryRunnable.class);
          22
          23 @Override
          24 public void run() {
          25 logger.info("user size: " + this.query());
          26 }
          27
          28 public int query() {
          29 int count = 0;
          30 try {
          31 ReplicationDriver driver = new ReplicationDriver();
          32 Properties props = new Properties();
          33 props.put("roundRobinLoadBalance", "true");
          34 props.put("autoReconnect", "true");
          35 props.put("user", "core");
          36 props.put("password", "core");
          37 Connection conn = null;
          38 Statement stat = null;
          39 ResultSet res = null;
          40 try {
          41 conn = driver
          42 .connect(
          43 //注意url串中間不要有空格,因?yàn)閙ysql源碼對(duì)多個(gè)地址split時(shí)沒(méi)有trim.
          44 "jdbc:mysql:replication://127.0.0.1:3309,127.0.0.1:3306/core",
          45 props);
          46 //讀寫(xiě)分離標(biāo)記
          47 //當(dāng)設(shè)置true時(shí),只會(huì)在從庫(kù)查詢(xún)數(shù)據(jù)
          48 //當(dāng)設(shè)置false時(shí),會(huì)在主庫(kù)做更新刪除操作
          49// conn.setReadOnly(true);
          50 stat = conn.createStatement();
          51 res = stat.executeQuery("select count(1) from c_user");
          52 while (res.next())
          53 count = res.getInt(1);
          54 } finally {
          55 if (res != null)
          56 res.close();
          57 if (stat != null)
          58 stat.close();
          59 if (conn != null)
          60 conn.close();
          61 }
          62 } catch (SQLException e) {
          63 e.printStackTrace();
          64 }
          65 return count;
          66 }
          67 }
          68
          69 public static void main(String[] args) {
          70 // 創(chuàng)建線程池測(cè)試負(fù)載軍衡
          71 ExecutorService service = Executors.newCachedThreadPool();
          72 for (int i = 0; i < 10; i++) {
          73 service.execute(new QueryRunnable());
          74 }
          75 service.shutdown();
          76 }
          77
          78}
          79
          posted on 2011-05-17 13:19 賀斌 閱讀(2441) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          <2011年5月>
          24252627282930
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 德江县| 资阳市| 白河县| 新竹县| 库车县| 桂阳县| 永清县| 罗定市| 龙川县| 渝北区| 武鸣县| 修武县| 明溪县| 虹口区| 江西省| 涞源县| 措勤县| 民乐县| 邵东县| 呼伦贝尔市| 东安县| 扎兰屯市| 玉龙| 崇仁县| 白银市| 晋江市| 聂荣县| 乡宁县| 上思县| 西乌| 阜南县| 麻城市| 石嘴山市| 岳西县| 宁远县| 新宁县| 洪湖市| 滕州市| 镇安县| 剑河县| 申扎县|