一、Sharding-jdbc介紹
1、簡介
Sharding-jdbc是當當網開源的一款客戶端代理中間件。Sharding-jdbc包含分庫分片和讀寫分離功能。對應用的代碼沒有侵入型,幾乎沒有任何改動,兼容主流orm框架,主流數據庫連接池。目前屬於Apache的孵化項目ShardingSphere。
Sharding-jdbc定位為輕量級Java框架,在Java的JDBC層提供的額外服務。 它使用客戶端直連數據庫,以jar包形式提供服務,無需額外部署和依賴,可理解為增強版的JDBC驅動,完全兼容JDBC和各種ORM框架。
適用於任何基於JDBC的ORM框架,如:JPA, Hibernate, Mybatis, Spring JDBC Template或直接使用JDBC。
支持任何第三方的數據庫連接池,如:DBCP, C3P0, BoneCP, Druid, HikariCP等。
支持任意實現JDBC規范的數據庫。目前支持MySQL,Oracle,SQLServer,PostgreSQL以及任何遵循SQL92標准的數據庫。
2、方案架構
二、Sharding-jdbc分庫分表
1、創建兩個數據庫land1、land2,分別包含t_user0,t_user1兩張表
DROP TABLE IF EXISTS `t_user0`; CREATE TABLE `t_user0` ( `id` bigint(20) NOT NULL, `name` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '名稱', `city_id` int(12) NULL DEFAULT NULL COMMENT '城市', `sex` tinyint(1) NULL DEFAULT NULL COMMENT '性別', `phone` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '電話', `email` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '郵箱', `create_time` timestamp(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0) ON UPDATE CURRENT_TIMESTAMP(0) COMMENT '創建時間', `password` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '密碼', PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8; DROP TABLE IF EXISTS `t_user0`; CREATE TABLE `t_user0` ( `id` bigint(20) NOT NULL, `name` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '名稱', `city_id` int(12) NULL DEFAULT NULL COMMENT '城市', `sex` tinyint(1) NULL DEFAULT NULL COMMENT '性別', `phone` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '電話', `email` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '郵箱', `create_time` timestamp(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0) ON UPDATE CURRENT_TIMESTAMP(0) COMMENT '創建時間', `password` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '密碼', PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8;
2、Springboot項目添加Maven依賴,Mybatisplus、Druid、Sharding-jdbc依賴
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.1.2</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.6</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.48</version> </dependency> <!-- sharding-sphere --> <dependency> <groupId>io.shardingsphere</groupId> <artifactId>sharding-jdbc-spring-boot-starter</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>io.shardingsphere</groupId> <artifactId>sharding-jdbc-spring-namespace</artifactId> <version>3.1.0</version> </dependency>
3、在application.xml中添加sharding-jdbc的分庫分表配置
server: port: 8800 spring: application: name: service-hi main: allow-bean-definition-overriding: true #允許覆蓋注冊 eureka: instance: prefer-ip-address: true #開啟顯示IP地址 instance-id: ${spring.cloud.client.ip-address}:${server.port} #eureka頁面顯示IP地址:端口號 client: serviceUrl: defaultZone: http://localhost:8761/eureka/ sharding: jdbc: datasource: names: ds0,ds1 #配置兩個數據源 ds0: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/land1?characterEncoding=utf-8&useUnicode=true&useSSL=true username: root password: root ds1: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/land2?characterEncoding=utf-8&useUnicode=true&useSSL=true username: root password: root
config:
sharding:
default-data-source-name: ds0 # 未配置分片規則的表將通過默認數據源定位
default-database-strategy: # 默認數據庫分片策略,同分庫策略
inline:
sharding-column: city_id
algorithm-expression: ds${city_id % 2}
default-table-strategy: # 默認表分片策略,同分表策略
inline:
shardingColumn: sex
algorithm-expression: t_user${sex % 2}
tables:
t_user: #t_user表
key-generator-column-name: id #主鍵
actual-data-nodes: ds${0..1}.t_user${0..1} #數據節點
database-strategy: #分庫策略
inline:
sharding-column: city_id
algorithm-expression: ds${city_id % 2}
table-strategy: #分表策略
inline:
shardingColumn: sex
algorithm-expression: t_user${sex % 2}
t_address: #t_address表
key-generator-column-name: id
actual-data-nodes: ds${0..1}.t_address
database-strategy:
inline:
shardingColumn: lit
algorithm-expression: ds${lit % 2}
props:
sql.show: true #是否顯示sharding-jdbc的sql路由
mybatis-plus:
type-aliases-package: com.landcode.service.hi.model
mapper-locations: classpath:mapper/*.xml
4、注意在mapper.xml sql配置文件中,表名使用user,而不是user0,user1。sharding-jdbc會根據配置的規則自動在對應的表執行sql
<select id="selectUserList" resultType="com.landcode.service.hi.model.User"> select <include refid="Base_Column_List" /> from t_user </select>
三、測試
1、查詢用戶
使用postman查詢user id為2的用戶
從sql路由可以看出,查詢了所有數據源,所有的user表。
2、插入用戶
使用postman插入city id為1,user id為5的用戶。
根據分庫策略,寫入到的庫應該是:land city id % 2,所以應該 land1
根據分表策略,寫入到的表應該是:user id%2,所以應該是 user1