簡(jiǎn)書 慢黑八
轉(zhuǎn)載請(qǐng)注明原創(chuàng)出處,謝謝榕订!
如果讀完覺(jué)得有收獲的話,歡迎點(diǎn)贊加關(guān)注
自Sharding JDBC進(jìn)入apache孵化之后(目前已經(jīng)正式更名為Sharding Sphere贩幻,本文中簡(jiǎn)稱SS)两嘴,使用SS進(jìn)行分庫(kù)分表操作的小伙伴日益增加,由于新舊版本在配置上差異比較大趣些,且資料較少贰您,本文主要講述如何在springboot下整合ss分表+zookeeper+druid的相關(guān)配置。
一舶替、配置maven與application.properties
maven 配置
<!-- sharding-jdbc-orchestration-spring-boot-starter -->
<!-- for spring boot -->
<dependency>
<groupId>io.shardingsphere</groupId>
<artifactId>sharding-jdbc-orchestration-spring-boot-starter</artifactId>
<version>3.0.0</version>
<dependency>
<groupId>io.shardingsphere</groupId>
<artifactId>sharding-orchestration-reg-zookeeper-curator</artifactId>
<version>3.0.0</version>
</dependency>
application.properties/application-[dev|test|prod].properties
## 多個(gè)ds杠园,用逗號(hào)分隔分別配置,例如:sharding.jdbc.datasource.names=ds1玲昧,ds2
##DataSourceUtil負(fù)責(zé)加載
sharding.jdbc.datasource.names=ds
sharding.jdbc.datasource.ds.type=com.alibaba.druid.pool.DruidDataSource
sharding.jdbc.datasource.ds.driver-class-name=com.mysql.jdbc.Driver
sharding.jdbc.datasource.ds.url=jdbc:mysql://localhost:3306/xxxx
sharding.jdbc.datasource.ds.username=root
sharding.jdbc.datasource.ds.password=
sharding.jdbc.datasource.ds.initial-size=8
sharding.jdbc.datasource.ds.min-idle=5
sharding.jdbc.datasource.ds.max-active=10
sharding.jdbc.datasource.ds.query-timeout=6000
sharding.jdbc.datasource.ds.transaction-query-timeout=6000
sharding.jdbc.datasource.ds.remove-abandoned-timeout=1800
sharding.jdbc.datasource.ds.filter-class-names=stat
sharding.jdbc.datasource.ds.filters=stat,config
sharding.jdbc.datasource.ds.testOnBorrow=false
## 以下為分表配置
sharding.jdbc.config.sharding.tables.t_order.actual-data-nodes=ds.t_order_$->{0..1}
sharding.jdbc.config.sharding.tables.t_order.table-strategy.inline.sharding-column=order_id
sharding.jdbc.config.sharding.tables.t_order.table-strategy.inline.algorithm-expression=t_order_$->{order_id.hashCode() % 2}
sharding.jdbc.config.sharding.tables.t_order_item.actual-data-nodes=ds.t_order_item_$->{0..1}
sharding.jdbc.config.sharding.tables.t_order_item.table-strategy.inline.sharding-column=order_id
sharding.jdbc.config.sharding.tables.t_order_item.table-strategy.inline.algorithm-expression=t_order_item_$->{order_id.hashCode() % 2}
sharding.jdbc.config.sharding.tables.t_order_item.key-generator-column-name=order_item_id
##如果有關(guān)聯(lián)表沒(méi)配置綁定有可能會(huì)出現(xiàn)笛卡爾積
sharding.jdbc.config.sharding.binding-tables[0]=t_order,t_order_item
sharding.jdbc.config.sharding.props.sql.show=true
##zk配置相關(guān)信息
sharding.jdbc.config.orchestration.name=sharding-sample-service
sharding.jdbc.config.orchestration.overwrite=true
sharding.jdbc.config.orchestration.registry.namespace=sharding-sample-service
sharding.jdbc.config.orchestration.registry.server-lists=localhost:2181
二孵延、sharding-jdbc-orchestration-spring-boot-starter中四個(gè)關(guān)鍵配置類
- starter啟動(dòng)配置類:
OrchestrationSpringBootConfiguration
- datasource配置類:
DataSourceUtil
- 分表策略配置類:
SpringBootShardingRuleConfigurationProperties
- orche公共配置類:
SpringBootOrchestrationConfigurationProperties
1尘应、OrchestrationSpringBootConfiguration這個(gè)類為sharding-jdbc-orchestration-spring-boot-starter配置自動(dòng)加載類吼虎,聚合類各項(xiàng)配置,可以看到這里的setDataSourceMap方法加載的都是sharding.jdbc.datasource.names
開(kāi)頭的配置
@Configuration
@EnableConfigurationProperties({SpringBootShardingRuleConfigurationProperties.class, SpringBootMasterSlaveRuleConfigurationProperties.class, SpringBootOrchestrationConfigurationProperties.class})
public class OrchestrationSpringBootConfiguration implements EnvironmentAware {
private final Map<String, DataSource> dataSourceMap = new LinkedHashMap<>();
@Autowired
private SpringBootShardingRuleConfigurationProperties shardingProperties;
@Autowired
private SpringBootMasterSlaveRuleConfigurationProperties masterSlaveProperties;
@Autowired
private SpringBootOrchestrationConfigurationProperties orchestrationProperties;
//.......略去一部分源碼
//......................
@Override
public final void setEnvironment(final Environment environment) {
setDataSourceMap(environment);
}
@SuppressWarnings("unchecked")
private void setDataSourceMap(final Environment environment) {
String prefix = "sharding.jdbc.datasource.";
String dataSources = environment.getProperty(prefix + "names");
if (StringUtils.isEmpty(dataSources)) {
return;
}
dataSources = dataSources.trim();
for (String each : dataSources.split(",")) {
try {
Map<String, Object> dataSourceProps = PropertyUtil.handle(environment, prefix + each, Map.class);
Preconditions.checkState(!dataSourceProps.isEmpty(), String.format("Wrong datasource [%s] properties!", each));
DataSource dataSource = DataSourceUtil.getDataSource(dataSourceProps.get("type").toString(), dataSourceProps);
dataSourceMap.put(each, dataSource);
} catch (final ReflectiveOperationException ex) {
throw new ShardingException("Can't find datasource type!", ex);
}
}
}
}
2、DataSourceUtil數(shù)據(jù)源配置類,這個(gè)類中最關(guān)鍵的一個(gè)方法是使用callSetterMethod方法洒疚,反射調(diào)用屬性的set方法進(jìn)行賦值操作坯屿。因?yàn)槲覀兪褂玫氖莄om.alibaba.druid.pool.DruidDataSource领跛,所以直接使用DruidDataSource屬性進(jìn)行配置即可撤奸。需要額外注意的是,這里面存在配置屬性轉(zhuǎn)換胧瓜,min-idle
這種配置對(duì)應(yīng)的是DruidDataSource類中的minIdle
public static DataSource getDataSource(final String dataSourceClassName, final Map<String, Object> dataSourceProperties) throws ReflectiveOperationException {
DataSource result = (DataSource) Class.forName(dataSourceClassName).newInstance();
for (Entry<String, Object> entry : dataSourceProperties.entrySet()) {
callSetterMethod(result, getSetterMethodName(entry.getKey()), null == entry.getValue() ? null : entry.getValue().toString());
}
return result;
}
3、SpringBootShardingRuleConfigurationProperties分庫(kù)分表配置類(sharding.jdbc.config.sharding
開(kāi)頭的配置),具體的配置信息取決于YamlShardingRuleConfiguration類中的成員變量幻妓。
@ConfigurationProperties(prefix = "sharding.jdbc.config.sharding")
public class SpringBootShardingRuleConfigurationProperties extends YamlShardingRuleConfiguration {
}
4劫拢、SpringBootOrchestrationConfigurationProperties公共信息配置類(sharding.jdbc.config.orchestration
開(kāi)頭的配置),具體的配置信息取決于YamlOrchestrationConfiguration類中的成員變量强胰。
@ConfigurationProperties(prefix = "sharding.jdbc.config.orchestration")
public class SpringBootOrchestrationConfigurationProperties extends YamlOrchestrationConfiguration {
}
清楚了以上4個(gè)配置類后偶洋,小伙伴們是不是就知道應(yīng)該如何配置sharding sphere了呢?小伙伴們可以根據(jù)配置類舉一反三玄窝,反推分庫(kù)分表的配置信息。
三帽氓、附錄