共计 7570 个字符,预计需要花费 19 分钟才能阅读完成。
序
本文主要研究一下 sharding-jdbc 的 MasterSlaveRouter
MasterSlaveRouter
incubator-shardingsphere-4.0.0-RC1/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/masterslave/MasterSlaveRouter.java
@RequiredArgsConstructor
public final class MasterSlaveRouter {
private final MasterSlaveRule masterSlaveRule;
private final boolean showSQL;
/**
* Route Master slave.
*
* @param sql SQL
* @return data source names
*/
// TODO for multiple masters may return more than one data source
public Collection<String> route(final String sql) {Collection<String> result = route(new SQLJudgeEngine(sql).judge().getType());
if (showSQL) {SQLLogger.logSQL(sql, result);
}
return result;
}
private Collection<String> route(final SQLType sqlType) {if (isMasterRoute(sqlType)) {MasterVisitedManager.setMasterVisited();
return Collections.singletonList(masterSlaveRule.getMasterDataSourceName());
}
return Collections.singletonList(masterSlaveRule.getLoadBalanceAlgorithm().getDataSource(masterSlaveRule.getName(), masterSlaveRule.getMasterDataSourceName(), new ArrayList<>(masterSlaveRule.getSlaveDataSourceNames())));
}
private boolean isMasterRoute(final SQLType sqlType) {return SQLType.DQL != sqlType || MasterVisitedManager.isMasterVisited() || HintManager.isMasterRouteOnly();}
}
- MasterSlaveRouter 的 route 方法使用 masterSlaveRule 来进行路由
MasterSlaveRule
incubator-shardingsphere-4.0.0-RC1/sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/rule/MasterSlaveRule.java
@Getter
public class MasterSlaveRule {
private final String name;
private final String masterDataSourceName;
private final Collection<String> slaveDataSourceNames;
private final MasterSlaveLoadBalanceAlgorithm loadBalanceAlgorithm;
private final MasterSlaveRuleConfiguration masterSlaveRuleConfiguration;
public MasterSlaveRule(final String name, final String masterDataSourceName, final Collection<String> slaveDataSourceNames, final MasterSlaveLoadBalanceAlgorithm loadBalanceAlgorithm) {
this.name = name;
this.masterDataSourceName = masterDataSourceName;
this.slaveDataSourceNames = slaveDataSourceNames;
this.loadBalanceAlgorithm = null == loadBalanceAlgorithm ? new MasterSlaveLoadBalanceAlgorithmServiceLoader().newService() : loadBalanceAlgorithm;
masterSlaveRuleConfiguration = new MasterSlaveRuleConfiguration(name, masterDataSourceName, slaveDataSourceNames,
new LoadBalanceStrategyConfiguration(this.loadBalanceAlgorithm.getType(), this.loadBalanceAlgorithm.getProperties()));
}
public MasterSlaveRule(final MasterSlaveRuleConfiguration config) {name = config.getName();
masterDataSourceName = config.getMasterDataSourceName();
slaveDataSourceNames = config.getSlaveDataSourceNames();
loadBalanceAlgorithm = createMasterSlaveLoadBalanceAlgorithm(config.getLoadBalanceStrategyConfiguration());
masterSlaveRuleConfiguration = config;
}
private MasterSlaveLoadBalanceAlgorithm createMasterSlaveLoadBalanceAlgorithm(final LoadBalanceStrategyConfiguration loadBalanceStrategyConfiguration) {MasterSlaveLoadBalanceAlgorithmServiceLoader serviceLoader = new MasterSlaveLoadBalanceAlgorithmServiceLoader();
return null == loadBalanceStrategyConfiguration ? serviceLoader.newService() : serviceLoader.newService(loadBalanceStrategyConfiguration.getType(), loadBalanceStrategyConfiguration.getProperties());
}
/**
* Judge whether contain data source name.
*
* @param dataSourceName data source name
* @return contain or not.
*/
public boolean containDataSourceName(final String dataSourceName) {return masterDataSourceName.equals(dataSourceName) || slaveDataSourceNames.contains(dataSourceName);
}
}
- MasterSlaveRule 内置了 loadBalanceAlgorithm、masterSlaveRuleConfiguration
MasterSlaveLoadBalanceAlgorithm
incubator-shardingsphere-4.0.0-RC1/sharding-core/sharding-core-api/src/main/java/org/apache/shardingsphere/spi/masterslave/MasterSlaveLoadBalanceAlgorithm.java
public interface MasterSlaveLoadBalanceAlgorithm extends TypeBasedSPI {
/**
* Get data source.
*
* @param name master-slave logic data source name
* @param masterDataSourceName name of master data sources
* @param slaveDataSourceNames names of slave data sources
* @return name of selected data source
*/
String getDataSource(String name, String masterDataSourceName, List<String> slaveDataSourceNames);
}
- MasterSlaveLoadBalanceAlgorithm 接口继承了 TypeBasedSPI 接口,它定义了 getDataSource 方法;它有两个实现类分别是 RandomMasterSlaveLoadBalanceAlgorithm、RoundRobinMasterSlaveLoadBalanceAlgorithm
RandomMasterSlaveLoadBalanceAlgorithm
incubator-shardingsphere-4.0.0-RC1/sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/strategy/masterslave/RandomMasterSlaveLoadBalanceAlgorithm.java
@Getter
@Setter
public final class RandomMasterSlaveLoadBalanceAlgorithm implements MasterSlaveLoadBalanceAlgorithm {private Properties properties = new Properties();
@Override
public String getType() {return "RANDOM";}
@Override
public String getDataSource(final String name, final String masterDataSourceName, final List<String> slaveDataSourceNames) {return slaveDataSourceNames.get(new Random().nextInt(slaveDataSourceNames.size()));
}
}
- RandomMasterSlaveLoadBalanceAlgorithm 使用 Random().nextInt 来进行随机
RoundRobinMasterSlaveLoadBalanceAlgorithm
incubator-shardingsphere-4.0.0-RC1/sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/strategy/masterslave/RoundRobinMasterSlaveLoadBalanceAlgorithm.java
@Getter
@Setter
public final class RoundRobinMasterSlaveLoadBalanceAlgorithm implements MasterSlaveLoadBalanceAlgorithm {private static final ConcurrentHashMap<String, AtomicInteger> COUNTS = new ConcurrentHashMap<>();
private Properties properties = new Properties();
@Override
public String getType() {return "ROUND_ROBIN";}
@Override
public String getDataSource(final String name, final String masterDataSourceName, final List<String> slaveDataSourceNames) {AtomicInteger count = COUNTS.containsKey(name) ? COUNTS.get(name) : new AtomicInteger(0);
COUNTS.putIfAbsent(name, count);
count.compareAndSet(slaveDataSourceNames.size(), 0);
return slaveDataSourceNames.get(Math.abs(count.getAndIncrement()) % slaveDataSourceNames.size());
}
}
- RoundRobinMasterSlaveLoadBalanceAlgorithm 使用
Math.abs(count.getAndIncrement()) % slaveDataSourceNames.size()
进行 round robin
MasterSlaveRuleConfiguration
incubator-shardingsphere-4.0.0-RC1/sharding-core/sharding-core-api/src/main/java/org/apache/shardingsphere/api/config/masterslave/MasterSlaveRuleConfiguration.java
@Getter
public class MasterSlaveRuleConfiguration implements RuleConfiguration {
private final String name;
private final String masterDataSourceName;
private final Collection<String> slaveDataSourceNames;
private final LoadBalanceStrategyConfiguration loadBalanceStrategyConfiguration;
public MasterSlaveRuleConfiguration(final String name, final String masterDataSourceName, final Collection<String> slaveDataSourceNames) {this(name, masterDataSourceName, slaveDataSourceNames, null);
}
public MasterSlaveRuleConfiguration(final String name,
final String masterDataSourceName, final Collection<String> slaveDataSourceNames, final LoadBalanceStrategyConfiguration loadBalanceStrategyConfiguration) {Preconditions.checkArgument(!Strings.isNullOrEmpty(name), "Name is required.");
Preconditions.checkArgument(!Strings.isNullOrEmpty(masterDataSourceName), "MasterDataSourceName is required.");
Preconditions.checkArgument(null != slaveDataSourceNames && !slaveDataSourceNames.isEmpty(), "SlaveDataSourceNames is required.");
this.name = name;
this.masterDataSourceName = masterDataSourceName;
this.slaveDataSourceNames = slaveDataSourceNames;
this.loadBalanceStrategyConfiguration = loadBalanceStrategyConfiguration;
}
}
- MasterSlaveRuleConfiguration 定义了 name、masterDataSourceName、slaveDataSourceNames、loadBalanceStrategyConfiguration 属性
小结
MasterSlaveRouter 的 route 方法使用 masterSlaveRule 来进行路由;MasterSlaveRule 内置了 loadBalanceAlgorithm、masterSlaveRuleConfiguration;MasterSlaveLoadBalanceAlgorithm 接口继承了 TypeBasedSPI 接口,它定义了 getDataSource 方法;它有两个实现类分别是 RandomMasterSlaveLoadBalanceAlgorithm、RoundRobinMasterSlaveLoadBalanceAlgorithm
doc
- MasterSlaveRouter