DBã®ãã¼ã¿éãå¢ãã¦ããå ´åã®å¯¾çã®1ã¤ã¨ãã¦ã¦ã¼ã¶IDãªã©ããã¼ã«ãã¼ã¿ãã¼ã¹ãåå²ããã·ã£ã¼ãã£ã³ã°ã¨å¼ã°ããææ³ãããã¾ãããããJDBCã®ã¬ã¤ã¤ã§å®ç¾ãã¦ãã¾ãsharding-jdbcã¨ããã©ã¤ãã©ãªãè¦ã¤ãã¾ããã
sharding-jdbcã¯ä¸å½ã®dangdangï¼å½å½ï¼ã¨ããEC大æä¼æ¥ãéçºããOSSã§ãSQLããã¼ã¹ããSQLã«å«ã¾ããã·ã£ã¼ããã¼ãæ½åºãã¦æ¥ç¶å ã®ãã¼ã¿ãã¼ã¹ããåç §ãããã¼ãã«ãåãæ¿ãã¦ãããã¨ãããã®ã§ãã
使ã£ã¦ã¿ã
ã¾ãã¯pom.xmlã«ä»¥ä¸ã®ä¾åé¢ä¿ã追å ãã¾ãã
<dependency> <groupId>com.dangdang</groupId> <artifactId>sharding-jdbc-core</artifactId> <version>1.4.1</version> </dependency>
ä»åã¯ç°¡åãªä¾ã¨ãããã¨ã§ãCOMPANY_ID
ã¨ããã«ã©ã ãã·ã£ã¼ããã¼ã«æ¥ç¶ãããã¼ã¿ãã¼ã¹ãåãæ¿ãã¦ã¿ã¾ãã以ä¸ã®ãããªæãã§ãã¼ã¿ã½ã¼ã¹ãæºåãã¾ãã
// æ¥ç¶å ã®ãã¼ã¿ã½ã¼ã¹ãä½æ Map<String, DataSource> dataSources = new HashMap<>(); dataSources.put("ds_0", createDataSource(...)); dataSources.put("ds_1", createDataSource(...)); DataSourceRule dataSourceRule = new DataSourceRule(dataSources); // ãã¼ãã«ãã¨ã®ã«ã¼ã«ãä½æ TableRule companyTableRule = TableRule.builder("COMPANY") .dataSourceRule(dataSourceRule) .build(); // ã·ã£ã¼ãã£ã³ã°ã®ã«ã¼ã«ãä½æ ShardingRule shardingRule = ShardingRule.builder() .dataSourceRule(dataSourceRule) .tableRules(Arrays.asList(companyTableRule, deptTableRule)) .databaseShardingStrategy(new DatabaseShardingStrategy("COMPANY_ID", new CompanyIdShardingAlgorithm())) .build(); DataSource dataSource = ShardingDataSourceFactory.createDataSource(shardingRule);
CompanyIdShardingAlgorithm
ãæ¥ç¶å
ãã¼ã¿ãã¼ã¹ã決å®ããã¯ã©ã¹ã«ãªãã¾ããã·ã£ã¼ããã¼ã«ä½¿ç¨ããã«ã©ã ã¯=
ãBETWEEN
ãIN
ã§ã®æ¤ç´¢ããè¡ããã¨ãã§ãã¾ãããShardingAlgorithmã§ã¯ãããã®æ¤ç´¢æ¹æ³ã«å¯¾å¿ããã¡ã½ãããå®è£
ããå¿
è¦ãããã®ã§ãããä»åã¯=
ã§ã®æ¤ç´¢ããè¡ããªãã®ã§doEqualSharding()
ã¡ã½ããã®ã¿å®è£
ãã¦ãã¾ãã
public class CompanyIdShardingAlgorithm implements SingleKeyDatabaseShardingAlgorithm<String> { @Override public String doEqualSharding(Collection<String> availableTargetNames, ShardingValue<String> shardingValue) { switch(shardingValue.getValue()){ case "bizreach": return "ds_0"; default: return "ds_1"; } } @Override public Collection<String> doInSharding(Collection<String> availableTargetNames, ShardingValue<String> shardingValue) { throw new UnsupportedOperationException(); } @Override public Collection<String> doBetweenSharding(Collection<String> availableTargetNames, ShardingValue<String> shardingValue) { throw new UnsupportedOperationException(); } }
ãã¨ã¯ä½æãããã¼ã¿ã½ã¼ã¹ããgetConnection()
ã¡ã½ãããåå¾ãã¦SQLãå®è¡ããã°ã·ã£ã¼ããã¼ã«å¿ããDBã«å¯¾ãã¦SQLãçºè¡ããã¾ãã
ãã®ä»ã®æ©è½
sharding-jdbcã«ã¯ãã®ä»ã«ã以ä¸ã®ãããªæ©è½ãããã¾ãã
- ã·ã£ã¼ããã¼ã§ãæ¥ç¶ãããã¼ã¿ãã¼ã¹ã§ã¯ãªãåç §ãããã¼ãã«ãåãæ¿ããæ©è½ï¼SQLããªã©ã¤ãããã¦å®è¡ããã¾ãï¼
- ä¸æãªIDãçºè¡ããæ©è½ï¼ã·ã£ã¼ããã¾ããèªåæ¡çªã®ã«ã©ã ã«å¤ãå ¥ããã®ã«ä½¿ç¨ãã¾ãï¼
- ã¯ã¨ãªãè¤æ°ã®ã·ã£ã¼ãã«ã¾ãããå ´åã«
ORDER BY
ãGROUP BY
ãå¦çããæ©è½ï¼ãã¼ã¸å¦çã¨å¼ã¶ããã§ãï¼
ä¸æ¹ã§åè¿°ã®éãã·ã£ã¼ããã¼ã«å¯¾ããæ¤ç´¢æ¡ä»¶ã«å¶éããããç°ãªãã·ã£ã¼ããUNION
ã§ããªããªã©ã®å¶ç´ãããã¾ããæ£å¸¸ã«ã«ã¼ãã£ã³ã°ãè¡ãããªãSQLã®å ´åã§ãç¹ã«ã¨ã©ã¼ã«ã¯ãªãããSQLä¸ã§æåã«æ¤åºãããã·ã£ã¼ããã¼ã使ã£ã¦ã«ã¼ãã£ã³ã°ããã¦ããããã«è¦ãã¾ããããã§ããã°ã¨ã©ã¼ã«ãªã£ã¦ãããã¨å¬ããã§ããã
ã¾ã¨ã
sharding-jdbcã¯JDBCã®ã¬ã¤ã¤ã§ã·ã£ã¼ãã£ã³ã°ã解決ãã¦ããããããæ¢åã®æ§ã ãªORMããã¼ã¿ãã¼ã¹ã¢ã¯ã»ã¹ã©ã¤ãã©ãªããã®ã¾ã¾å©ç¨ã§ããã¨ãã大ããªã¡ãªãããããã¾ããå©ç¨å¯è½ãªSQLã«å¶éã¯ãããã®ã®ãããããã·ã£ã¼ããã¼ã«å¯¾ãã¦ãªãããªæ¤ç´¢ãè¡ãå¿ è¦ã¯ãªãã§ãããããè¤éãªã¬ãã¼ãã£ã³ã°ãå¿ è¦ã¨ãã£ãã±ã¼ã¹ã§ãªããã°ãã»ã©å°ããã¨ããªãããã§ãã
ç¾æç¹ã§ã·ã£ã¼ãã£ã³ã°ãä¸è¦ã ã¨ãã¦ããå°æ¥çã«ãã¼ã¿ã®å¢å ã«ä¼´ã£ã¦DBã®ã·ã£ã¼ãã£ã³ã°ãæ¤è¨ããå¯è½æ§ãããããã§ããã°ãæåãããããå ¥ãã¦ããã¨ããæãèããããã®ã§ã¯ãªãã§ããããã