读书人

mysql 集群 jdbc配备

发布时间: 2012-10-24 14:15:58 作者: rapoo

mysql 集群 jdbc配置

项目中使用mysql 主从复制,但是用程序实现的读写分离,代码片段如下:

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


获取数据源,首先你要确定MethodType 类型,一个是读,一个是写

1mysql 集群 jdbc配备mysql 集群 jdbc配备public enum MethodType mysql 集群 jdbc配备{
2mysql 集群 jdbc配备 READ, WRITE
3mysql 集群 jdbc配备}

读是获取从库数据源,写是获取主库数据源。

这样,就需要在jdbc配置配置两个数据源(一主一从)

还有另一种实现方式,不用程序来控制。mysql 驱动包提供相应的实现 com.mysql.jdbc.ReplicationDriver.
我写了一个简单的例子:

1mysql 集群 jdbc配备package com.howard.loadbalance;
2mysql 集群 jdbc配备
3mysql 集群 jdbc配备import java.sql.Connection;
4mysql 集群 jdbc配备import java.sql.ResultSet;
5mysql 集群 jdbc配备import java.sql.SQLException;
6mysql 集群 jdbc配备import java.sql.Statement;
7mysql 集群 jdbc配备import java.util.Properties;
8mysql 集群 jdbc配备import java.util.concurrent.ExecutorService;
9mysql 集群 jdbc配备import java.util.concurrent.Executors;
10mysql 集群 jdbc配备
11mysql 集群 jdbc配备import org.slf4j.Logger;
12mysql 集群 jdbc配备import org.slf4j.LoggerFactory;
13mysql 集群 jdbc配备
14mysql 集群 jdbc配备import com.mysql.jdbc.ReplicationDriver;
15mysql 集群 jdbc配备
16mysql 集群 jdbc配备mysql 集群 jdbc配备public class MysqlTest mysql 集群 jdbc配备{
17mysql 集群 jdbc配备
18mysql 集群 jdbc配备mysql 集群 jdbc配备public static class QueryRunnable implements Runnable mysql 集群 jdbc配备{
19mysql 集群 jdbc配备
20mysql 集群 jdbc配备protected final static Logger logger = LoggerFactory
21mysql 集群 jdbc配备 .getLogger(QueryRunnable.class);
22mysql 集群 jdbc配备
23mysql 集群 jdbc配备 @Override
24mysql 集群 jdbc配备mysql 集群 jdbc配备public void run() mysql 集群 jdbc配备{
25mysql 集群 jdbc配备 logger.info("user size: " + this.query());
26mysql 集群 jdbc配备 }
27mysql 集群 jdbc配备
28mysql 集群 jdbc配备mysql 集群 jdbc配备public int query() mysql 集群 jdbc配备{
29mysql 集群 jdbc配备int count = 0;
30mysql 集群 jdbc配备mysql 集群 jdbc配备try mysql 集群 jdbc配备{
31mysql 集群 jdbc配备 ReplicationDriver driver = new ReplicationDriver();
32mysql 集群 jdbc配备 Properties props = new Properties();
33mysql 集群 jdbc配备 props.put("roundRobinLoadBalance", "true");
34mysql 集群 jdbc配备 props.put("autoReconnect", "true");
35mysql 集群 jdbc配备 props.put("user", "core");
36mysql 集群 jdbc配备 props.put("password", "core");
37mysql 集群 jdbc配备 Connection conn = null;
38mysql 集群 jdbc配备 Statement stat = null;
39mysql 集群 jdbc配备 ResultSet res = null;
40mysql 集群 jdbc配备mysql 集群 jdbc配备try mysql 集群 jdbc配备{
41mysql 集群 jdbc配备 conn = driver
42mysql 集群 jdbc配备 .connect(
43mysql 集群 jdbc配备//注意url串中间不要有空格,因为mysql源码对多个地址split时没有trim.
44mysql 集群 jdbc配备 "jdbc:mysql:replication://127.0.0.1:3309,127.0.0.1:3306/core",
45mysql 集群 jdbc配备 props);
46mysql 集群 jdbc配备//读写分离标记
47mysql 集群 jdbc配备//当设置true时,只会在从库查询数据
48mysql 集群 jdbc配备//当设置false时,会在主库做更新删除操作
49mysql 集群 jdbc配备// conn.setReadOnly(true);
50mysql 集群 jdbc配备 stat = conn.createStatement();
51mysql 集群 jdbc配备 res = stat.executeQuery("select count(1) from c_user");
52mysql 集群 jdbc配备while (res.next())
53mysql 集群 jdbc配备 count = res.getInt(1);
54mysql 集群 jdbc配备mysql 集群 jdbc配备 } finally mysql 集群 jdbc配备{
55mysql 集群 jdbc配备if (res != null)
56mysql 集群 jdbc配备 res.close();
57mysql 集群 jdbc配备if (stat != null)
58mysql 集群 jdbc配备 stat.close();
59mysql 集群 jdbc配备if (conn != null)
60mysql 集群 jdbc配备 conn.close();
61mysql 集群 jdbc配备 }
62mysql 集群 jdbc配备mysql 集群 jdbc配备 } catch (SQLException e) mysql 集群 jdbc配备{
63mysql 集群 jdbc配备 e.printStackTrace();
64mysql 集群 jdbc配备 }
65mysql 集群 jdbc配备return count;
66mysql 集群 jdbc配备 }
67mysql 集群 jdbc配备 }
68mysql 集群 jdbc配备
69mysql 集群 jdbc配备mysql 集群 jdbc配备public static void main(String[] args) mysql 集群 jdbc配备{
70mysql 集群 jdbc配备// 创建线程池测试负载军衡
71mysql 集群 jdbc配备 ExecutorService service = Executors.newCachedThreadPool();
72mysql 集群 jdbc配备mysql 集群 jdbc配备for (int i = 0; i < 10; i++) mysql 集群 jdbc配备{
73mysql 集群 jdbc配备 service.execute(new QueryRunnable());
74mysql 集群 jdbc配备 }
75mysql 集群 jdbc配备 service.shutdown();
76mysql 集群 jdbc配备 }
77mysql 集群 jdbc配备
78mysql 集群 jdbc配备}

读书人网 >其他数据库

热点推荐