點選上方“Java技術驛站”,選擇“置頂公眾號”。
有內涵、有價值的文章第一時間送達!
精品專欄
本篇文章講解如何在ssm(spring、springmvc、mybatis)結構的程式上整合sharding-jdbc(版本為2.0.3)進行分庫分表;
假設分庫分錶行為如下:
-
將authuser表分到4個庫(user0~user_3)中;
-
其他表不進行分庫分表,保留在default_db庫中;
1. POM配置
以spring配置檔案為例,新增如下POM配置:
io.shardingjdbc
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:sharding="http://shardingjdbc.io/schema/shardingjdbc/sharding"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://shardingjdbc.io/schema/shardingjdbc/sharding
http://shardingjdbc.io/schema/shardingjdbc/sharding/sharding.xsd">
id="databaseStrategy" sharding-column="id"
precise-algorithm-class="com.crt.fin.ospsso.service.shardingjdbc.AuthUserDatabaseShardingAlgorithm" />
id="noneStrategy" />
id="shardingDataSource">
data-source-names="sj_ds_0,sj_ds_1,sj_ds_2,sj_ds_3,sj_ds_default"
default-data-source-name="sj_ds_default"
default-database-strategy-ref="noneStrategy"
default-table-strategy-ref="noneStrategy">
logic-table="auth_user" actual-data-nodes="sj_ds_${0..3}.auth_user"
database-strategy-ref="databaseStrategy"/>
說明:spring-sharding.xml配置的分庫分表規則:authuser表分到id為sjds${0..3}的四個庫中,表名保持不變;其他表在id為sjds_default庫中,不分庫也不分表;整合sharding-jdbc的核心就是將SqlSessionFactoryBean需要的dataSource屬性修改為
shardingDataSource
,把資料源交給sharding-jdbc處理;
另外,透過對比這裡和sharding-jdbc1.5.4.1版本的配置請戳連結:https://www.jianshu.com/p/602e24845ed3,差異還是比較大,大概提現在如下一些地方:
-
namespace由rdb改為sharding;
-
預設資料庫策略和預設表策略被設定為
節點的屬性,分別是default-database-strategy-ref
和default-table-strategy-ref
; -
預設資料源被設定為
節點的屬性,即default-data-source-name
; -
一些屬性變更,例如:actual-tables改為actual-data-nodes,database-strategy改為database-strategy-ref;
我們平常開發過程中,可以參考sharding-jdbc原始碼sharding-jdbc-core-spring-namespace模組中META.rdb目錄下的xml,作者對幾乎各種使用方式都有實體;
分庫邏輯 AuthUserDatabaseShardingAlgorithm
的程式碼很簡單,原始碼如下:
/**
* @author wangzhenfei9
* @version 1.0.0
* @since 2018年05月14日
*/
public class AuthUserDatabaseShardingAlgorithm implements PreciseShardingAlgorithm<Long> {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private static final int SHARDING_NUMBER = 4;
@Override
public String doSharding(final Collection<String> availableTargetNames,
final PreciseShardingValue<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();
}
}
這段程式碼參考sharding-jdbc原始碼中
PreciseShardingAlgorithm.java
介面的實現即可,例如PreciseModuloDatabaseShardingAlgorithm.java
;這裡和sharding-jdbc1.5.4.1版本的差異也比較大,sharding-jdbc1.5.4.1對於分庫或者分表sharding演演算法實現的介面是不一樣的,sharding-jdbc2.0.3將兩者合二為一,且只有一個方法,即doSharding();
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年05月14日
*/
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
【死磕Java併發】----- 死磕 Java 併發精品合集
【死磕Sharding-jdbc】---group by的SQL重寫為limit Integer.MAX_VALUE的無奈
END