mysql 集群 jdbc配备

mysql 集群 jdbc配置

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

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


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

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

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

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

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

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