本篇文章给大家分享的是有关怎么理解sharding-jdbc的ShardingMasterSlaveRouter,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
创新互联专注于企业网络营销推广、网站重做改版、昆山网站定制设计、自适应品牌网站建设、H5场景定制、商城开发、集团公司官网建设、外贸营销网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为昆山等各大城市提供网站开发制作服务。
主要研究一下sharding-jdbc的ShardingMasterSlaveRouter
ShardingMasterSlaveRouter
incubator-shardingsphere-4.0.0-RC1/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/masterslave/ShardingMasterSlaveRouter.java
@RequiredArgsConstructor public final class ShardingMasterSlaveRouter { private final CollectionmasterSlaveRules; /** * Route Master slave after sharding. * * @param sqlRouteResult SQL route result * @return route result */ public SQLRouteResult route(final SQLRouteResult sqlRouteResult) { for (MasterSlaveRule each : masterSlaveRules) { route(each, sqlRouteResult); } return sqlRouteResult; } private void route(final MasterSlaveRule masterSlaveRule, final SQLRouteResult sqlRouteResult) { Collection toBeRemoved = new LinkedList<>(); Collection toBeAdded = new LinkedList<>(); for (TableUnit each : sqlRouteResult.getRoutingResult().getTableUnits().getTableUnits()) { if (!masterSlaveRule.getName().equalsIgnoreCase(each.getDataSourceName())) { continue; } toBeRemoved.add(each); String actualDataSourceName; if (isMasterRoute(sqlRouteResult.getSqlStatement().getType())) { MasterVisitedManager.setMasterVisited(); actualDataSourceName = masterSlaveRule.getMasterDataSourceName(); } else { actualDataSourceName = masterSlaveRule.getLoadBalanceAlgorithm().getDataSource( masterSlaveRule.getName(), masterSlaveRule.getMasterDataSourceName(), new ArrayList<>(masterSlaveRule.getSlaveDataSourceNames())); } toBeAdded.add(createNewTableUnit(actualDataSourceName, each)); } sqlRouteResult.getRoutingResult().getTableUnits().getTableUnits().removeAll(toBeRemoved); sqlRouteResult.getRoutingResult().getTableUnits().getTableUnits().addAll(toBeAdded); } private boolean isMasterRoute(final SQLType sqlType) { return SQLType.DQL != sqlType || MasterVisitedManager.isMasterVisited() || HintManager.isMasterRouteOnly(); } private TableUnit createNewTableUnit(final String actualDataSourceName, final TableUnit originalTableUnit) { TableUnit result = new TableUnit(actualDataSourceName, originalTableUnit.getDataSourceName()); result.getRoutingTables().addAll(originalTableUnit.getRoutingTables()); return result; } }
ShardingMasterSlaveRouter提供了route方法,返回sqlRouteResult,这里修改了sqlRouteResult的routingResult的tableUnits的TableUnit集合
SQLRouteResult
incubator-shardingsphere-4.0.0-RC1/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/SQLRouteResult.java
@RequiredArgsConstructor @Getter @Setter public final class SQLRouteResult { private final SQLStatement sqlStatement; private final GeneratedKey generatedKey; // For multiple thread read cached sqlStatement, clone limit on SQLRouteResult, because limit will be modified after cache // TODO need more good design here private Limit limit; private RoutingResult routingResult; private OptimizeResult optimizeResult; private final CollectionrouteUnits = new LinkedHashSet<>(); public SQLRouteResult(final SQLStatement sqlStatement) { this(sqlStatement, null); } }
SQLRouteResult拥有RoutingResult属性
RoutingResult
incubator-shardingsphere-4.0.0-RC1/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/type/RoutingResult.java
@Getter public class RoutingResult { private final TableUnits tableUnits = new TableUnits(); /** * Judge is route for single database and table only or not. * * @return is route for single database and table only or not */ public boolean isSingleRouting() { return 1 == tableUnits.getTableUnits().size(); } }
RoutingResult拥有TableUnits属性
TableUnits
incubator-shardingsphere-4.0.0-RC1/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/type/TableUnits.java
@Getter @ToString public final class TableUnits { private final CollectiontableUnits = new LinkedHashSet<>(); /** * Get all data source names. * * @return all data source names */ public Collection getDataSourceNames() { Collection result = new HashSet<>(tableUnits.size(), 1); for (TableUnit each : tableUnits) { result.add(each.getDataSourceName()); } return result; } /** * Get routing table via data source name and actual table name. * * @param dataSourceName data source name * @param actualTableName actual table name * @return routing table */ public Optional getRoutingTable(final String dataSourceName, final String actualTableName) { for (TableUnit each : tableUnits) { Optional result = each.getRoutingTable(dataSourceName, actualTableName); if (result.isPresent()) { return result; } } return Optional.absent(); } /** * Get actual tables group via data source name and logic tables' names. * * Actual tables in same group are belong one logic name. *
* * @param dataSourceName data source name * @param logicTableNames logic tables' names * @return actual tables group */ public List> getActualTableNameGroups(final String dataSourceName, final Set logicTableNames) { List > result = new ArrayList<>(); for (String logicTableName : logicTableNames) { Set actualTableNames = getActualTableNames(dataSourceName, logicTableName); if (!actualTableNames.isEmpty()) { result.add(actualTableNames); } } return result; } private Set getActualTableNames(final String dataSourceName, final String logicTableName) { Set result = new HashSet<>(tableUnits.size(), 1); for (TableUnit each : tableUnits) { result.addAll(each.getActualTableNames(dataSourceName, logicTableName)); } return result; } /** * Get map relationship between data source and logic tables via data sources' names. * * @param dataSourceNames data sources' names * @return map relationship between data source and logic tables */ public Map > getDataSourceLogicTablesMap(final Collection dataSourceNames) { Map > result = new HashMap<>(); for (String each : dataSourceNames) { Set logicTableNames = getLogicTableNames(each); if (!logicTableNames.isEmpty()) { result.put(each, logicTableNames); } } return result; } private Set getLogicTableNames(final String dataSourceName) { Set result = new HashSet<>(tableUnits.size(), 1); for (TableUnit each : tableUnits) { if (each.getDataSourceName().equalsIgnoreCase(dataSourceName)) { result.addAll(each.getLogicTableNames(dataSourceName)); } } return result; } }
TableUnits内部是TableUnit的集合
TableUnit
incubator-shardingsphere-4.0.0-RC1/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/type/TableUnit.java
@RequiredArgsConstructor @Getter @EqualsAndHashCode @ToString public final class TableUnit { private final String dataSourceName; private final String masterSlaveLogicDataSourceName; private final ListroutingTables = new LinkedList<>(); public TableUnit(final String dataSourceName) { this.dataSourceName = dataSourceName; masterSlaveLogicDataSourceName = dataSourceName; } /** * Get routing table via data source name and actual table name. * * @param dataSourceName data source name * @param actualTableName actual table name * @return routing table */ public Optional getRoutingTable(final String dataSourceName, final String actualTableName) { for (RoutingTable each : routingTables) { if (dataSourceName.equalsIgnoreCase(masterSlaveLogicDataSourceName) && each.getActualTableName().equalsIgnoreCase(actualTableName)) { return Optional.of(each); } } return Optional.absent(); } /** * Get actual tables' names via data source name. * * @param dataSourceName data source name * @param logicTableName logic table name * @return actual tables' names */ public Set getActualTableNames(final String dataSourceName, final String logicTableName) { Set result = new HashSet<>(routingTables.size(), 1); for (RoutingTable each : routingTables) { if (dataSourceName.equalsIgnoreCase(this.dataSourceName) && each.getLogicTableName().equalsIgnoreCase(logicTableName)) { result.add(each.getActualTableName()); } } return result; } /** * Get logic tables' names via data source name. * * @param dataSourceName data source name * @return logic tables' names */ public Set getLogicTableNames(final String dataSourceName) { Set result = new HashSet<>(routingTables.size(), 1); for (RoutingTable each : routingTables) { if (dataSourceName.equalsIgnoreCase(this.dataSourceName)) { result.add(each.getLogicTableName()); } } return result; } }
TableUnit包含了RoutingTable的集合
RoutingTable
incubator-shardingsphere-4.0.0-RC1/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/type/RoutingTable.java
@RequiredArgsConstructor @Getter @EqualsAndHashCode @ToString public final class RoutingTable { private final String logicTableName; private final String actualTableName; }
RoutingTable包含了logicTableName与actualTableName
小结
ShardingMasterSlaveRouter提供了route方法,返回sqlRouteResult,这里修改了sqlRouteResult的routingResult的tableUnits的TableUnit集合
以上就是怎么理解sharding-jdbc的ShardingMasterSlaveRouter,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。
文章标题:怎么理解sharding-jdbc的ShardingMasterSlaveRouter
本文路径:http://scyingshan.cn/article/joijhh.html