點選上方“Java技術驛站”,選擇“置頂公眾號”。
有內涵、有價值的文章第一時間送達!
本篇文章講解如何在ssm(spring、springmvc、mybatis)結構的程式上整合sharding-jdbc(版本為1.5.4.1)進行分庫分表; 假設分庫分錶行為如下:
-
將authuser表分到4個庫(user0~user_3)中;
-
其他表不進行分庫分表,保留在default_db庫中;
1. POM配置
以spring配置檔案為例,新增如下POM配置:
com.dangdang
此次整合sharding-jdbc以1.5.4.1版本為例,如果是2.x版本的sharding-jdbc,那麼需要將坐標
com.dangdang
修改為
;另外,如果是yaml配置,那麼需要將坐標
修改為
;
2. 配置資料源
spring-datasource.xml配置所有需要的資料源如下--authuser分庫分表後需要的4個庫user0~user3,以及不分庫分表的預設庫defaultdb:
xml version="1.0" encoding="UTF-8"?>
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
id="sj_ds_0" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
name="url" value="${sj_user_0.url}" />
name="username" value="${sj_user_0.username}" />
name="password" value="${sj_user_0.password}" />
init-method="init" destroy-method="close">
init-method="init" destroy-method="close">
init-method="init" destroy-method="close">
init-method="init" destroy-method="close">
properties配置檔案內容如下:
sj_user_0.driver=com.mysql.jdbc.Driver
sj_user_0.url=jdbc:mysql://localhost:3306/user_0
sj_user_0.username=root
sj_user_0.password=RootAfei_1
sj_user_1.driver=com.mysql.jdbc.Driver
sj_user_1.url=jdbc:mysql://localhost:3306/user_1
sj_user_1.username=root
sj_user_1.password=RootAfei_1
sj_user_2.driver=com.mysql.jdbc.Driver
sj_user_2.url=jdbc:mysql://localhost:3306/user_2
sj_user_2.username=root
sj_user_2.password=RootAfei_1
sj_user_3.driver=com.mysql.jdbc.Driver
sj_user_3.url=jdbc:mysql://localhost:3306/user_3
sj_user_3.username=root
sj_user_3.password=RootAfei_1
sj_default.driver=com.mysql.jdbc.Driver
sj_default.url=jdbc:mysql://localhost:3306/default_db
sj_default.username=root
sj_default.password=RootAfei_1
3. 整合sharding資料源
spring-sharding.xml配置如下:
xml version="1.0" encoding="UTF-8"?>
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:rdb="http://www.dangdang.com/schema/ddframe/rdb"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.dangdang.com/schema/ddframe/rdb
http://www.dangdang.com/schema/ddframe/rdb/rdb.xsd">
id="databaseStrategy" sharding-columns="id"
algorithm-class="com.crt.fin.ospsso.service.shardingjdbc.AuthUserDatabaseShardingAlgorithm" />
id="shardingDataSource">
data-sources="sj_ds_0,sj_ds_1,sj_ds_2,sj_ds_3,sj_ds_default"
default-data-source="sj_ds_default">
logic-table="auth_user" actual-tables="sj_ds_${0..3}.auth_user"
database-strategy="databaseStrategy"/>
說明:spring-sharding.xml配置的分庫分表規則:authuser表分到id為sjds${0..3}的四個庫中,表名保持不變;其他表在id為sjds_default庫中,不分庫也不分表;整合sharding-jdbc的核心就是將SqlSessionFactoryBean需要的dataSource屬性修改為
shardingDataSource
,把資料源交給sharding-jdbc處理;
分庫邏輯 AuthUserDatabaseShardingAlgorithm
的程式碼很簡單,原始碼如下:
/**
* @author wangzhenfei9
* @version 1.0.0
* @since 2018年02月08日
*/
public class AuthUserDatabaseShardingAlgorithm implements SingleKeyDatabaseShardingAlgorithm<Long> {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private static final int SHARDING_NUMBER = 4;
@Override
public String doEqualSharding(final Collection<String> availableTargetNames, final ShardingValue<Long> shardingValue) {
for (String each : availableTargetNames) {
if (each.endsWith(shardingValue.getValue() % SHARDING_NUMBER + "")) {
logger.debug("the target database name: {}", each);
return each;
}
}
throw new UnsupportedOperationException();
}
@Override
public Collection<String> doInSharding(final Collection<String> availableTargetNames, final ShardingValue<Long> shardingValue) {
Collection<String> result = new LinkedHashSet<>(availableTargetNames.size());
Collection<Long> values = shardingValue.getValues();
for (Long value : values) {
for (String each : availableTargetNames) {
if (each.endsWith(value % SHARDING_NUMBER + "")) {
result.add(each);
}
}
}
return result;
}
@Override
public Collection<String> doBetweenSharding(final Collection<String> availableTargetNames, final ShardingValue<Long> shardingValue) {
Collection<String> result = new LinkedHashSet<>(availableTargetNames.size());
Range<Long> range = shardingValue.getValueRange();
for (Long value = range.lowerEndpoint(); value <= range.upperEndpoint(); value++) {
for (String each : availableTargetNames) {
if (each.endsWith(value % SHARDING_NUMBER + "")) {
result.add(each);
}
}
}
return result;
}
}
這段程式碼參考sharding-jdbc原始碼中
DatabaseShardingAlgorithm.java
介面的實現即可,例如ModuloDatabaseShardingAlgorithm.java
;
4. 註意事項
無法識別sharding-jdbc分庫分表規則inline-expression問題,例如:
-
根本原因: 根本原因是spring把
${}
當做佔位符,${0..3}
這種運算式,spring會嘗試去properties檔案中找key為0..3
的屬性。但是這裡是sharding-jdbc分庫分表規則的inline運算式,需要spring忽略這種行為。否則會丟擲異常: java.lang.IllegalArgumentException: Could not resolve placeholder '0..3' in value "sjds${0..3}.auth_user" -
解決辦法: 配置:
或者:name="ignoreUnresolvablePlaceholders"value="true"/> ****** ignore-unresolvable="true"/>
5. Main測試
Main.java用來測試分庫分表是否OK,其原始碼如下:
/**
* @author wangzhenfei9
* @version 1.0.0
* @since 2018年02月08日
*/
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"/META-INF/spring/spring-*.xml");
// auth_user有進行分庫,
AuthUserMapper authUserMapper = context.getBean(AuthUserMapper.class);
AuthUser authUser = authUserMapper.selectByPrimaryKey(7L);
System.out.println("-----> The auth user: "+JSON.toJSONString(authUser));
// user_permission沒有分庫分表
UserPermissionMapper userPermissionMapper = context.getBean(UserPermissionMapper.class);
UserPermission userPermission = userPermissionMapper.selectPermissionByUsername("wangzhenfei", "FINANCE_WALLET");
System.out.println("-----< The user permission: "+JSON.toJSONString(userPermission));
}
}
AuthUserMapper.selectByPrimaryKey()和UserPermissionMapper.selectPermissionByUsername()的程式碼和沒有分庫分表的程式碼完全一樣;
6. 遺留問題
Main方法測試,或者啟動服務後的呼叫測試都沒有問題,但是透過junit測試用例訪問就會丟擲異常,作為一個待解決的遺留問題:
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected single matching bean but found 6: sj_ds_0,sj_ds_1,sj_ds_2,sj_ds_3,sj_ds_default,shardingDataSource
END