sharing-jdbc实现读写分离及分库分表
生活随笔
收集整理的這篇文章主要介紹了
sharing-jdbc实现读写分离及分库分表
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
需求:
分庫:按業(yè)務(wù)線business_id將不同業(yè)務(wù)線的訂單存儲在不同的數(shù)據(jù)庫上;
分表:按user_id字段將不同用戶的訂單存儲在不同的表上,為方便直接用非分片字段order_id查詢,可使用基因法;
讀寫分離:為緩解主庫的壓力,讀操作訪問從庫;
庫表SQL
-- 主庫 CREATE DATABASE `database_103`; CREATE DATABASE `database_112`;-- 從庫 CREATE DATABASE `database_slave_103`; CREATE DATABASE `database_slave_112`;--每個庫上分別建立如下表 CREATE TABLE `t_order_0` (`id` bigint(20) NOT NULL,`order_id` bigint(20) NOT NULL,`user_id` bigint(20) NOT NULL,`business_id` bigint(20) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8;CREATE TABLE `t_order_1` (`id` bigint(20) NOT NULL,`order_id` bigint(20) NOT NULL,`user_id` bigint(20) NOT NULL,`business_id` bigint(20) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8;CREATE TABLE `t_order_2` (`id` bigint(20) NOT NULL,`order_id` bigint(20) NOT NULL,`user_id` bigint(20) NOT NULL,`business_id` bigint(20) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; INSERT INTO `t_order` VALUES (1,1,112);CREATE TABLE `t_order_3` (`id` bigint(20) NOT NULL,`order_id` bigint(20) NOT NULL,`user_id` bigint(20) NOT NULL,`business_id` bigint(20) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8;pom.xml
<!-- sharding-jdbc --><dependency><groupId>com.dangdang</groupId><artifactId>sharding-jdbc-core</artifactId><version>1.4.2</version></dependency><dependency><groupId>com.dangdang</groupId><artifactId>sharding-jdbc-config-spring</artifactId><version>1.4.0</version></dependency>spring-database.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:rdb="http://www.dangdang.com/schema/ddframe/rdb"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.dangdang.com/schema/ddframe/rdbhttp://www.dangdang.com/schema/ddframe/rdb/rdb.xsd"><bean id="database_112" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"destroy-method="close"><property name="driverClassName" value="com.mysql.jdbc.Driver"/><property name="url" value="${jdbc.url.112}"></property><property name="username" value="${jdbc.username.112}"></property><property name="password" value="${jdbc.password.112}"></property><property name="maxActive" value="100"/><property name="initialSize" value="50"/><property name="maxWait" value="60000"/><property name="minIdle" value="5"/></bean><bean id="database_slave_112" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"destroy-method="close"><property name="driverClassName" value="com.mysql.jdbc.Driver"/><property name="url" value="${jdbc.url.slave.112}"></property><property name="username" value="${jdbc.username.slave.112}"></property><property name="password" value="${jdbc.password.slave.112}"></property><property name="maxActive" value="100"/><property name="initialSize" value="50"/><property name="maxWait" value="60000"/><property name="minIdle" value="5"/></bean><bean id="database_103" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"destroy-method="close"><property name="driverClassName" value="com.mysql.jdbc.Driver"/><property name="url" value="${jdbc.url.103}"></property><property name="username" value="${jdbc.username.103}"></property><property name="password" value="${jdbc.password.103}"></property><property name="maxActive" value="100"/><property name="initialSize" value="50"/><property name="maxWait" value="60000"/><property name="minIdle" value="5"/></bean><bean id="database_slave_103" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"destroy-method="close"><property name="driverClassName" value="com.mysql.jdbc.Driver"/><property name="url" value="${jdbc.url.slave.103}"></property><property name="username" value="${jdbc.username.slave.103}"></property><property name="password" value="${jdbc.password.slave.103}"></property><property name="maxActive" value="100"/><property name="initialSize" value="50"/><property name="maxWait" value="60000"/><property name="minIdle" value="5"/></bean><!--mybatis--><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="mapperLocations" value="classpath:xmlmapper/*.xml"/><property name="dataSource" ref="shardingDataSource"/><!-- 配置Mybatis配置文件 --><property name="configLocation" value="classpath:/mybatis/mybatis-config.xml"/></bean><bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"><constructor-arg index="0" ref="sqlSessionFactory"/></bean><!-- 注解Mapper scanner --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.lc.sharding.mybatismapper"/><property name="sqlSessionTemplateBeanName" value="sqlSession"/></bean><!-- 事務(wù)--><bean id="txManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="shardingDataSource"/></bean><tx:annotation-driven transaction-manager="txManager"/><!--讀寫分離--><rdb:master-slave-data-source id="rbb_112" master-data-source-ref="database_112"slave-data-sources-ref="database_slave_112"/><rdb:master-slave-data-source id="rbb_103" master-data-source-ref="database_103"slave-data-sources-ref="database_slave_103"/><!--分庫策略--><rdb:strategy id="databaseShardingStrategy" sharding-columns="business_id"algorithm-expression="rbb_${business_id.longValue()}"/><!--分表策略--><rdb:strategy id="tableShardingStrategy" sharding-columns="user_id,order_id"algorithm-class="com.lc.sharding.algorithm.MultipleKeysTableShardingAlgorithmImpl"/><rdb:data-source id="shardingDataSource"><rdb:sharding-rule data-sources="rbb_112,rbb_103"><rdb:table-rules><rdb:table-rule logic-table="t_order" actual-tables="t_order_${0..3}" database-strategy="databaseShardingStrategy"table-strategy="tableShardingStrategy"/></rdb:table-rules></rdb:sharding-rule><rdb:props><prop key="metrics.enable">true</prop><prop key="sql.show">true</prop></rdb:props></rdb:data-source> </beans基因法多列分片
public class MultipleKeysTableShardingAlgorithmImpl implements MultipleKeysTableShardingAlgorithm {public Collection<String> doSharding(Collection<String> tableNames, Collection<ShardingValue<?>> shardingValues) {List<String> shardingSuffix = new ArrayList<String>();long partId = 0;for (ShardingValue value : shardingValues) {if (value.getColumnName().equals("user_id")) {partId = ((Long) value.getValue()) % 4;break;} else if (value.getColumnName().equals("order_id")) {partId = ((Long) value.getValue()) % 4;break;}}for (String name : tableNames) {if (name.endsWith(partId + "")) {shardingSuffix.add(name);return shardingSuffix;}}return shardingSuffix;} }什么是基因法分片?
在訂單數(shù)據(jù)oid生成時,order_id末端加入分片基因,讓同一個user_id下的所有訂單都含有相同基因,落在同一個表上。
資料:https://mp.weixin.qq.com/s/PCzRAZa9n4aJwHOX-kAhtA
根據(jù)user_id生成order_id:
public long bulidOrderId(long userId) {//取用戶id后4位userId = userId & 15;//先取60位唯一idlong uniqueId = this.nextId();//唯一id左移4位、拼接userId后4位return (uniqueId << 4) | userId;} this.nextId();//使用雪花算法生成60位分布式唯一id:1位符號位+41位時間戳+5位workId+5位datacenterId+6位序列號+4位基因片?小結(jié)
數(shù)據(jù)分片:
- 支持分庫+分表;
- 可支持 = , BETWEEN,IN等多維度分片,也支持多分片鍵共用;
- 支持聚合,分組,排序,分頁,關(guān)聯(lián)等復(fù)雜查詢語句;
- 分片靈活,支持多分片鍵共用,支持inline表達式;
- 基于Hint的強制路由;
- 支持分布式主鍵
讀寫分離:
- 支持一主多從的讀寫分離;
- 支持分庫分表與讀寫分離共同使用
- 支持分布式生成全局主鍵。
柔性事務(wù):
- 最大努力到達型事務(wù)
分布式治理:
- 支持配置中心,可動態(tài)修改
- 支持客戶端熔斷和失效轉(zhuǎn)移
引用:http://shardingsphere.io/
轉(zhuǎn)載于:https://www.cnblogs.com/sidesky/p/10669403.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的sharing-jdbc实现读写分离及分库分表的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【python】BytesIO与串化
- 下一篇: 移动端与PC端页面布局区别