-
Notifications
You must be signed in to change notification settings - Fork 0
/
MySqlReplicationGroupManager.java
87 lines (72 loc) · 4.34 KB
/
MySqlReplicationGroupManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.yucl.learn;
import com.mysql.cj.jdbc.ha.ReplicationConnectionGroupManager;
import com.mysql.cj.jdbc.jmx.ReplicationGroupManager;
import java.sql.*;
public class MySqlReplicationGroupManager {
//static ReplicationGroupManager replicationGroupManager = new ReplicationGroupManager();
private static String URL = "jdbc:mysql:replication://" +
"address=(type=master)(host=192.168.142.166)(port=3306),address=(type=slave)(host=192.168.142.167)(port=3306),address=(type=slave)(host=192.168.142.168)(port=3306)/test?" +
"replicationConnectionGroup=aa&allowMasterDownConnections=true&replicationEnableJMX=true&useSSL=false&connectTimeout=1";
public static void main(String[] args) throws Exception {
// replicationGroupManager.registerJmx();
for (int i = 0; i < 10000; i++) {
try (Connection conn = getNewConnection()) {
conn.setAutoCommit(false);
try (PreparedStatement s = conn.prepareStatement("insert into test.t2(b) values(?)")) {
s.setString(1, String.valueOf(i));
s.executeUpdate();
conn.commit();
System.out.println("commit");
}
} catch (SQLException e) {
System.out.println(e.getErrorCode());
autoPromoteSlaveToMaster();
e.printStackTrace();
}
Thread.sleep(1000);
//if(i==1){
// System.out.println("groups:"+replicationGroupManager.getRegisteredConnectionGroups());
//replicationGroupManager.promoteSlaveToMaster("aa","192.168.142.167:3306");
// replicationGroupManager.removeMasterHost("aa","192.168.142.166:3306");
// System.out.println("groups:"+loadBalanceConnectionGroupManager.getRegisteredConnectionGroups());
//ConnectionGroupManager.removeHost("aa", "192.168.142.166:3306");
//loadBalanceConnectionGroupManager.
//ReplicationConnectionGroup xx = ReplicationConnectionGroupManager.getConnectionGroup("aa");
//xx.promoteSlaveToMaster("192.168.142.167:3306");
// ReplicationConnectionGroupManager.removeMasterHost("aa","192.168.142.166:3306");
// ReplicationConnectionGroupManager.promoteSlaveToMaster("aa","192.168.142.167:3306");
// }
}
}
static void autoPromoteSlaveToMaster() throws SQLException {
// ReplicationConnectionGroupManager.getRegisteredReplicationConnectionGroups()
//String group = replicationGroupManager.getRegisteredConnectionGroups().split(",")[0];
//String hosts = replicationGroupManager.getMasterHostsList(group);
String group = ReplicationConnectionGroupManager.getRegisteredReplicationConnectionGroups().split(",")[0];
String hosts = ReplicationConnectionGroupManager.getMasterHosts(group).iterator().next();
System.out.println(hosts);
//replicationGroupManager.removeMasterHost(group, hosts);
ReplicationConnectionGroupManager.removeMasterHost(group, hosts);
try (Connection conn = getNewConnection()) {
conn.setAutoCommit(false);
try (PreparedStatement s = conn.prepareStatement("SELECT * FROM performance_schema.replication_group_members WHERE MEMBER_ID = (SELECT VARIABLE_VALUE FROM performance_schema.global_status WHERE VARIABLE_NAME= 'group_replication_primary_member')")) {
try (ResultSet rst = s.executeQuery()) {
if (rst.next()) {
String memberHost = rst.getString("MEMBER_HOST");
String memberPort = rst.getString("MEMBER_PORT");
System.out.println("member:" + memberHost + ":" + memberPort);
//replicationGroupManager.promoteSlaveToMaster(group, memberHost + ":" + memberPort);
ReplicationConnectionGroupManager.promoteSlaveToMaster(group, memberHost + ":" + memberPort);
}
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
static Connection getNewConnection() throws SQLException, ClassNotFoundException {
// Class.forName("com.mysql.jdbc.Driver");
//Class.forName("com.mysql.jdbc.ReplicationDriver");
return DriverManager.getConnection(URL, "root", "***");
}
}