
com.github.javaclub.cdl.client.util.MasterSlaveSwitch Maven / Gradle / Ivy
The newest version!
package com.github.javaclub.cdl.client.util;
/**
* 主备访问动态切换, 如强一致性数据访问主库
*/
public class MasterSlaveSwitch {
private static ThreadLocal dsKey = new ThreadLocal<>();
public static void setMaster(){
dsKey.set("master");
}
public static void setSlave(){
dsKey.set("slave");
}
public static void clear(){
dsKey.remove();
}
public static boolean isMaster(){
String key = dsKey.get();
if(key != null && "master".equals(key)){
return true;
}
return false;
}
public static boolean isSlave(){
String key = dsKey.get();
if(key != null && "slave".equals(key)){
return true;
}
return false;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy