Sharding Sphere3.0.0下的springboot+druid+分表配置详解

简书 慢黑八
转载请注明原创出处,谢谢!
如果读完觉得有收获的话,欢迎点赞加关注

自Sharding JDBC进入apache孵化之后(目前已经正式更名为Sharding Sphere,本文中简称SS),使用SS进行分库分表操作的小伙伴日益增加,由于新旧版本在配置上差异比较大,且资料较少,本文主要讲述如何在springboot下整合ss分表+zookeeper+druid的相关配置。

一、配置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

## 多个ds,用逗号分隔分别配置,例如:sharding.jdbc.datasource.names=ds1,ds2
##DataSourceUtil负责加载
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

##如果有关联表没配置绑定有可能会出现笛卡尔积
sharding.jdbc.config.sharding.binding-tables[0]=t_order,t_order_item
sharding.jdbc.config.sharding.props.sql.show=true

##zk配置相关信息
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中四个关键配置类

  1. starter启动配置类:OrchestrationSpringBootConfiguration
  2. datasource配置类:DataSourceUtil
  3. 分表策略配置类:SpringBootShardingRuleConfigurationProperties
  4. orche公共配置类:SpringBootOrchestrationConfigurationProperties
sharding-sphere 配置类

1、OrchestrationSpringBootConfiguration这个类为sharding-jdbc-orchestration-spring-boot-starter配置自动加载类,聚合类各项配置,可以看到这里的setDataSourceMap方法加载的都是sharding.jdbc.datasource.names开头的配置

@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数据源配置类,这个类中最关键的一个方法是使用callSetterMethod方法,反射调用属性的set方法进行赋值操作。因为我们使用的是com.alibaba.druid.pool.DruidDataSource,所以直接使用DruidDataSource属性进行配置即可。需要额外注意的是,这里面存在配置属性转换,min-idle这种配置对应的是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分库分表配置类(sharding.jdbc.config.sharding开头的配置),具体的配置信息取决于YamlShardingRuleConfiguration类中的成员变量。

@ConfigurationProperties(prefix = "sharding.jdbc.config.sharding")
public class SpringBootShardingRuleConfigurationProperties extends YamlShardingRuleConfiguration {
}

4、SpringBootOrchestrationConfigurationProperties公共信息配置类(sharding.jdbc.config.orchestration开头的配置),具体的配置信息取决于YamlOrchestrationConfiguration类中的成员变量。

@ConfigurationProperties(prefix = "sharding.jdbc.config.orchestration")
public class SpringBootOrchestrationConfigurationProperties extends YamlOrchestrationConfiguration {
}

清楚了以上4个配置类后,小伙伴们是不是就知道应该如何配置sharding sphere了呢?小伙伴们可以根据配置类举一反三,反推分库分表的配置信息。

三、附录