Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the function of MasterSlaveRouter in sharding-jdbc

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you what the role of MasterSlaveRouter in sharding-jdbc is, the content is concise and easy to understand, it can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

MasterSlaveRouter

Incubator-shardingsphere-4.0.0-RC1/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/masterslave/MasterSlaveRouter.java

@ RequiredArgsConstructorpublic 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 route (final String sql) {Collection result = route (new SQLJudgeEngine (sql). Judge (). GetType ()); if (showSQL) {SQLLogger.logSQL (sql, result);} return result } private Collection 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 ();}}

The route method of MasterSlaveRouter uses masterSlaveRule for routing

MasterSlaveRule

Incubator-shardingsphere-4.0.0-RC1/sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/rule/MasterSlaveRule.java

Getterpublic class MasterSlaveRule {private final String name; private final String masterDataSourceName; private final Collection slaveDataSourceNames; private final MasterSlaveLoadBalanceAlgorithm loadBalanceAlgorithm; private final MasterSlaveRuleConfiguration masterSlaveRuleConfiguration; public MasterSlaveRule (final String name, final String masterDataSourceName, final Collection 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 has built-in loadBalanceAlgorithm and 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 slaveDataSourceNames);}

The MasterSlaveLoadBalanceAlgorithm interface inherits the TypeBasedSPI interface, which defines the getDataSource method; it has two implementation classes: RandomMasterSlaveLoadBalanceAlgorithm and 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@Setterpublic 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 slaveDataSourceNames) {return slaveDataSourceNames.get (new Random (). NextInt (slaveDataSourceNames.size ();}}

RandomMasterSlaveLoadBalanceAlgorithm uses Random (). NextInt to do random

RoundRobinMasterSlaveLoadBalanceAlgorithm

Incubator-shardingsphere-4.0.0-RC1/sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/strategy/masterslave/RoundRobinMasterSlaveLoadBalanceAlgorithm.java

@ Getter@Setterpublic final class RoundRobinMasterSlaveLoadBalanceAlgorithm implements MasterSlaveLoadBalanceAlgorithm {private static final ConcurrentHashMap 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 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 uses Math.abs (count.getAndIncrement ())% slaveDataSourceNames.size () for 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

@ Getterpublic class MasterSlaveRuleConfiguration implements RuleConfiguration {private final String name; private final String masterDataSourceName; private final Collection slaveDataSourceNames; private final LoadBalanceStrategyConfiguration loadBalanceStrategyConfiguration; public MasterSlaveRuleConfiguration (final String name, final String masterDataSourceName, final Collection slaveDataSourceNames) {this (name, masterDataSourceName, slaveDataSourceNames, null) } public MasterSlaveRuleConfiguration (final String name, final String masterDataSourceName, final Collection 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 defines name, masterDataSourceName, slaveDataSourceNames, loadBalanceStrategyConfiguration attributes

Summary

MasterSlaveRouter's route method uses masterSlaveRule for routing; MasterSlaveRule has built-in loadBalanceAlgorithm, and the masterSlaveRuleConfiguration;MasterSlaveLoadBalanceAlgorithm interface inherits the TypeBasedSPI interface, which defines the getDataSource method; it has two implementation classes: RandomMasterSlaveLoadBalanceAlgorithm and RoundRobinMasterSlaveLoadBalanceAlgorithm

The above content is what is the role of MasterSlaveRouter in sharding-jdbc. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report