GBase JDBC 集群高可用负载均衡

如果我们希望把数据库连接请求平均分布到各个节点的上话,我们可以使用 GBase JDBC 集群高可用负载均衡功能(该功能需要 GBase JDBC8.3.81.53_build51.1 及以上版本。)。

高可用负载均衡是 GBaseJDBC 针对集群开发的接口层面的客户端负载均衡功能。该功能能把客户发送来的数据库连接请求分发到各个节点,在GBaseJDBC8.3.81.53_built51.1 版本中负载均衡策略为轮询。

假设有如下场景:

1、部署有一个 GBase 集群,三个节点 IP 如下:

192.168.1.56; 192.168.1.57;192.168.1.58;

如果我们希望把数据库连接请求均摊到三个节点时,创建连接时设置failoverEnable、hostList、gclusterId 三个参数即可。连接串写法如下:

String URL ="jdbc:gbase://192.168.1.56/test?user=gbase&password=gbase20110531&failoverEnable=true&hostList=192.168.1.57,192.168.1.58&gclusterId=gcl1"

其中 gclusterId 参数取值范围:必须以 a-z 任意字符开头的可以包含 a-z、0-9 所有字符长度为最大为 20 的字符串。

for (int I = 0; i < 9; i++) {
DriverManager.getConnection(SampleGBaseJDBCLoadbalance.URL);
}

上述代码会创建 9 个连接,假设三个节点都是可用状态,执行结果是每个节点分配到三个连接;如果其中一个节点不可用状态,GBaseJDBC 驱动会把 9个数据库连接请求分配到另外两个节点上。

示例代码如下:

package com.gbase.jdbc.simple;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class SampleGBaseJDBCLoadbalance {
/**
* 数据库连接串
* failoverEnable = true
* hostList=192.168.5.212,192.168.7.174
* gclusterId=gcl1
* 启用高可用负载均衡
*/
public static final String URL =
"jdbc:gbase://192.168.111.96/test?user=gbase&password=gbase20110531&
failoverEnable=true&hostList=192.168.5.212,192.168.7.174&gclusterId=
gcl1";
/**
* @param args
*/
public static void main(String[] args) {
try {
Class.forName("com.gbase.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//准备样例代码用到的表及数据
prepareTable();
//创建线程池
ExecutorService executorService =
Executors.newCachedThreadPool();
for (int i = 0; i < 50; i++) {
executorService.execute(new
SelectThread(String.valueOf(i)));
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
} 
}
executorService.shutdown();
try {
Thread.sleep(50000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("main thread finished");
}
private static void prepareTable() {
Connection conn = null;
Statement stm = null;
try {
conn = DriverManager.getConnection(URL);
stm = conn.createStatement();
stm.executeUpdate("drop table if exists
`loadbalance`");
stm.executeUpdate("create table loadbalance(a varchar
(10), b varchar (12))");
stm.executeUpdate("insert into test.loadbalance
values('a1', 'b1')");
stm.executeUpdate("insert into test.loadbalance
values('a2', 'b2')");
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (stm != null) {
try {
if (!stm.isClosed())
stm.close();
} catch (Exception e) {
}
}
}
}
}
class SelectThread implements Runnable {
private String threadName;
SelectThread(String n) {
threadName = n;
}
int i =0;
public void run() {
while (i < 20) {
i++;
work();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("end = " + threadName);
}
public void work() {
try {
Class.forName("com.gbase.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection conn = null;
Statement stm = null;
ResultSet rs = null;
String ip= null;
try {
conn =
DriverManager.getConnection(SampleGBaseJDBCLoadbalance.URL);
ip = (com.gbase.jdbc.GBaseConnection)conn).getHost();
System.out.println(this.threadName + " 线程: 获取连接
on " + ip);
stm = conn.createStatement();
rs = stm.executeQuery("select loadbalance.* , sleep(5)
from test.loadbalance");
while(rs.next()) {
System.out.println(rs.getString(1));
}
} catch (SQLException e) {
System.out.println(this.threadName + " 线程: 异常 on
"+ip);
e.printStackTrace();
} finally {
try {
rs.close();
} catch (SQLException e) {
}
try {
stm.close();
} catch (SQLException e) {
}
try {
conn.close();
} catch (SQLException e) {
}
}
}
}

注意:高可用负载均衡功能集包含高可用性,即当开始高可用负载均衡时高可用性同时开启。

你可能感兴趣的:(GBase,8a,负载均衡,服务器,java)