sharding-jdbc读写分离


首先需要mysql数据库实现主从复制。
注意,sharding-jdbc只负责主从数据库的路由分发,不负责主从数据库之间的复制,复制由mysql实现。

1.配置文件

#分片规则
#数据源 、《《读写分离》》 数据源声明
spring.shardingsphere.datasource.names=m1,m2,m0,s0
spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.m1.url=jdbc:mysql://192.168.230.145:3306/order_db_1?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
spring.shardingsphere.datasource.m1.username=root
spring.shardingsphere.datasource.m1.password=root
spring.shardingsphere.datasource.m2.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m2.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.m2.url=jdbc:mysql://192.168.230.145:3306/order_db_2?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
spring.shardingsphere.datasource.m2.username=root
spring.shardingsphere.datasource.m2.password=root
# 《《读写分离》》 数据源
spring.shardingsphere.datasource.m0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.m0.url=jdbc:mysql://192.168.230.145:3306/user-db?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
spring.shardingsphere.datasource.m0.username=root
spring.shardingsphere.datasource.m0.password=root
spring.shardingsphere.datasource.s0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.s0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.s0.url=jdbc:mysql://192.168.230.147:3306/user-db?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
spring.shardingsphere.datasource.s0.username=root
spring.shardingsphere.datasource.s0.password=root

#分库策略
spring.shardingsphere.sharding.tables.c_order.database-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.tables.c_order.database-strategy.inline.algorithm-expression=m$->{user_id%2+1}
# 《《读写分离》》 分库策略
spring.shardingsphere.sharding.master-slave-rules.db0.master-data-source-name=m0
spring.shardingsphere.sharding.master-slave-rules.db0.slave-data-source-names=s0

#指定表数据分布情况,配置数据节点
spring.shardingsphere.sharding.tables.c_order.actual-data-nodes=m$->{1..2}.c_order_$->{1..2}
# 《《读写分离》》 指定表数据分布情况,配置数据节点
spring.shardingsphere.sharding.tables.c_user.actual-data-nodes=db0.c_user

#主键生成策略:雪花算法
spring.shardingsphere.sharding.tables.c_order.key-generator.column=order_id
spring.shardingsphere.sharding.tables.c_order.key-generator.type=SNOWFLAKE

#分表策略
spring.shardingsphere.sharding.tables.c_order.table-strategy.inline.sharding-column=order_id
spring.shardingsphere.sharding.tables.c_order.table-strategy.inline.algorithm-expression=c_order_$->{order_id % 2 + 1}

#输出日志
spring.shardingsphere.props.sql.show=true

#公共表,广播表
spring.shardingsphere.sharding.broadcast-tables=c_dict

2.sql执行语句

@Insert("insert into c_user (id,name) value (#{id},#{name})")
void insertUser(@Param("id")Integer id, @Param("name")String name);

@Select("select * from c_user where id = #{id}")
Map selectUser(@Param("id")Integer id);

3.测试类

	@Test
    public void testMasterSlaveDistrict(){
        cUserMapper.insertUser(1,"jack");
    }

    @Test
    public void testMasterSlaveDistrictRead(){
        Map map = cUserMapper.selectUser(1);
        System.out.println(map);
    }

4.执行结果

2021-05-15 21:05:18.302 DEBUG 16748 --- [           main] c.dt.demo.mapper.CUserMapper.insertUser  : ==>  Preparing: insert into c_user (id,name) value (?,?)
2021-05-15 21:05:18.330 DEBUG 16748 --- [           main] c.dt.demo.mapper.CUserMapper.insertUser  : ==> Parameters: 1(Integer), jack(String)
2021-05-15 21:05:18.910  INFO 16748 --- [           main] ShardingSphere-SQL                       : Rule Type: sharding
2021-05-15 21:05:18.911  INFO 16748 --- [           main] ShardingSphere-SQL                       : Logic SQL: insert into c_user (id,name) value (?,?)
2021-05-15 21:05:18.911  INFO 16748 --- [           main] ShardingSphere-SQL                       : SQLStatement: InsertStatement(super=DMLStatement(super=AbstractSQLStatement(type=DML, tables=Tables(tables=[Table(name=c_user, alias=Optional.absent())]), routeConditions=Conditions(orCondition=OrCondition(andConditions=[AndCondition(conditions=[])])), encryptConditions=Conditions(orCondition=OrCondition(andConditions=[])), sqlTokens=[TableToken(tableName=c_user, quoteCharacter=NONE, schemaNameLength=0), SQLToken(startIndex=19)], parametersIndex=2, logicSQL=insert into c_user (id,name) value (?,?)), deleteStatement=false, updateTableAlias={}, updateColumnValues={}, whereStartIndex=0, whereStopIndex=0, whereParameterStartIndex=0, whereParameterEndIndex=0), columnNames=[id, name], values=[InsertValue(columnValues=[org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@498c535d, org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@16ee9aa7])])
2021-05-15 21:05:18.911  INFO 16748 --- [           main] ShardingSphere-SQL                       : Actual SQL: m0 ::: insert into c_user  (id, name) VALUES (?, ?) ::: [1, jack]
2021-05-15 21:05:18.945 DEBUG 16748 --- [           main] c.dt.demo.mapper.CUserMapper.insertUser  : <==    Updates: 1

2021-05-15 21:05:51.762 DEBUG 3704 --- [           main] c.dt.demo.mapper.CUserMapper.selectUser  : ==>  Preparing: select * from c_user where id = ?
2021-05-15 21:05:51.793 DEBUG 3704 --- [           main] c.dt.demo.mapper.CUserMapper.selectUser  : ==> Parameters: 1(Integer)
2021-05-15 21:05:52.377  INFO 3704 --- [           main] ShardingSphere-SQL                       : Rule Type: sharding
2021-05-15 21:05:52.378  INFO 3704 --- [           main] ShardingSphere-SQL                       : Logic SQL: select * from c_user where id = ?
2021-05-15 21:05:52.378  INFO 3704 --- [           main] ShardingSphere-SQL                       : SQLStatement: SelectStatement(super=DQLStatement(super=AbstractSQLStatement(type=DQL, tables=Tables(tables=[Table(name=c_user, alias=Optional.absent())]), routeConditions=Conditions(orCondition=OrCondition(andConditions=[])), encryptConditions=Conditions(orCondition=OrCondition(andConditions=[])), sqlTokens=[TableToken(tableName=c_user, quoteCharacter=NONE, schemaNameLength=0)], parametersIndex=1, logicSQL=select * from c_user where id = ?)), containStar=true, firstSelectItemStartIndex=7, selectListStopIndex=7, groupByLastIndex=0, items=[StarSelectItem(owner=Optional.absent())], groupByItems=[], orderByItems=[], limit=null, subqueryStatement=null, subqueryStatements=[], subqueryConditions=[])
2021-05-15 21:05:52.378  INFO 3704 --- [           main] ShardingSphere-SQL                       : Actual SQL: s0 ::: select * from c_user where id = ? ::: [1]
2021-05-15 21:05:52.420 DEBUG 3704 --- [           main] c.dt.demo.mapper.CUserMapper.selectUser  : <==      Total: 1
{name=jack, id=1}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
课程简介 随着互联网的发展,软件的规模在逐渐变大,用关系型数据库如何存储和处理大规模的业务数据成为企业面临的挑战, 关系型数据库作为OLTP(联机事务处理过程)系统的首选毋庸置疑,但是关系型数据面对大规模数据的处理有其先天的不足,比如单表存储上千万数据时便会出现不同程度的处理速度缓慢问题,如何解决?分库分表技术就是为了解决由于数据量过大而导致数据库性能降低的问题,将原来独立的数据库拆分成若干数据库组成 ,将数据大表拆分成若干数据表组成,使得单一数据库、单一数据表的数据量变小,从而达到提升数据库性能的目的。本课程将系统的讲解分库分表技术。 课程价值 分库分表技术是为解决关系型数据库存储和处理大规模数据的问题,主要应用于OLTP系统,它与应用于OLAP(联机分析处理)的大数据技术有不同的应用场景,本课程本着从解决生产实际问题出发,讲授分库分表技术的解决方案,包括:垂直分库、垂直分表、水平分库、水平分表、读写分离,涵盖了分库分表的各种方案,并且深入讲解Sharding-JDBC框架的原理及使用方法,通过学习本课程可以快速应用到生产实践中。 课程优势 本课程不仅讲解多种有效的分库分表的解决方案,还深入讲解了Sharding-JDBC框架的原理和使用方法,Sharding-JDBC是一套轻量级的对代码零侵入的框架,在生产中有广泛的使用。本课程从思想原理、技术框架、案例实操三个方面去学习,可以快速的将分库分表技术应用到生产实践中,解决大数据存储与处理的问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值