mysql-router
[https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-Router/](https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-Router/)
mysql-router-2.0.3-linux-glibc2.12-x86-64bit.tar.gz

参考:https://cloud.tencent.com/developer/article/1697593
[root@MiWiFi-R3600-srv mysql-router]# cat /etc/mysql-route/mysqlrouter.conf
# MySQL Router sample configuration
#
# The following is a sample configuration file which shows
# most of the plugins available and most of their options.
#
# The paths used are defaults and should be adapted based
# on how MySQL Router was installed, for example, using the
# CMake option CMAKE_INSTALL_PREFIX
#
# The logging_folder is kept empty so message go to the
# console.
#
# The fabric_cache plugin only works with the MySQL-RPC
# protocol (by default running on port 32275)
#
[DEFAULT]
# 日志存放目录
logging_folder = /data/log/mysql-route
# 插件存放目录
plugin_folder = /usr/local/mysql-router/lib/mysqlrouter
# 配置文件存放目录
config_folder = /etc/mysql-route
# 运行目录
runtime_folder = /var/run
#[logger]
# 日志运行级别
level = INFO
#[fabric_cache]
#address = your_fabric_node.example.com:32275
#user =
[routing:basic_failover]
# To be more transparent, use MySQL Server port 3306
#bind_port = 7001
#mode = read-write
#destinations = mysql-server1:3306,mysql-server2
# 写节点地址
bind_address = 192.168.31.207
# 写节点端口
bind_port = 7001
# 模式,读写
mode = read-write
# 主节点地址:默认情况下第一台主数据库为写主库,当第一台主数据库DOWN机后,第二台数据库被提升为主库
destinations = 192.168.31.207:3306,192.168.31.28:3306,192.168.31.23:3306
#[routing:homepage_reads_fabric]
#bind_port = 7002
#destinations = fabric+cache:///group/homepage_group?allow_primary_reads=yes
#mode = read-only
# If no plugin is configured which starts a service, keepalive
# will make sure MySQL Router will not immediately exit. It is
# safe to remove once Router is configured.
[keepalive]
interval = 60
测试
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
Java
package com.minio.demo.testdb;
import org.apache.commons.lang3.StringUtils;
import java.sql.*;
import java.util.UUID;
/**
* @Author: LLL
* @Description:
* @Date: create in 2021/11/9 21:39
*/
public class test1 {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://192.168.31.207:7001/test?useSSL=false";
static final String USER = "root";
static final String PASS = "Zhongruan@2021";
public static void main(String[] args) {
for (int i = 0; i < 10000; i++) {
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>> " + i);
db2();
System.out.println();
System.out.println();
}
}
private static void db() {
Connection conn = null;
Statement stmt = null;
try {
// 注册 JDBC 驱动
Class.forName(JDBC_DRIVER);
// 打开链接
// System.out.println(" 连接数据库...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
// 执行查询
// System.out.println("======================================");
// System.out.println(" 实例化 Statement 对象...");
stmt = conn.createStatement();
String sql;
sql = "SELECT * FROM t_user";
ResultSet rs = stmt.executeQuery(sql);
// 展开结果集数据库
while (rs.next()) {
// 通过字段检索
int id = rs.getInt("id");
String name = rs.getString("name");
Integer age = rs.getInt("age");
String job = rs.getString("job");
if (StringUtils.isEmpty(name)) {
System.out.println("查不到数据...");
}
// 输出数据
System.out.print("ID: " + id);
System.out.print(", 名称: " + name);
System.out.print(", 年龄: " + age);
System.out.print(", 专业: " + job);
System.out.print("\n");
}
// 完成后关闭
rs.close();
stmt.close();
conn.close();
} catch (SQLException se) {
// 处理 JDBC 错误
se.printStackTrace();
} catch (Exception e) {
// 处理 Class.forName 错误
e.printStackTrace();
} finally {
// 关闭资源
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException se2) {
}// 什么都不做
try {
if (conn != null) {
conn.close();
}
} catch (SQLException se) {
se.printStackTrace();
}
}
// System.out.println("======================================");
}
private static void db2() {
Connection conn = null;
Statement stmt = null;
try {
// 注册 JDBC 驱动
Class.forName(JDBC_DRIVER);
// 打开链接
conn = DriverManager.getConnection(DB_URL, USER, PASS);
String sql = "insert into t_user(`id`, `name`, `age`, `job`) values(?,'小李', '18', 'java')";
PreparedStatement pstmt = conn.prepareStatement(sql);
String uuid = UUID.randomUUID().toString();
pstmt.setString(1, uuid);
int res = pstmt.executeUpdate();
if (res > 0) {
System.out.println(uuid + " 数据录入成功");
}
pstmt.close();//关闭资源
conn.close();//关闭资源
} catch (SQLException se) {
// 处理 JDBC 错误
se.printStackTrace();
} catch (Exception e) {
// 处理 Class.forName 错误
e.printStackTrace();
} finally {
// 关闭资源
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException se2) {
}// 什么都不做
try {
if (conn != null) {
conn.close();
}
} catch (SQLException se) {
se.printStackTrace();
}
}
// System.out.println("======================================");
}
}
网友评论