ShardingJdbc入门

视频地址
官网

SpringBoot的pom.xml

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
   <groupId>com.baomidou</groupId>
   <artifactId>mybatis-plus-boot-starter</artifactId>
   <version>3.0.5</version>
</dependency>

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
</dependency>

<dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>druid-spring-boot-starter</artifactId>
   <version>1.1.9</version>
</dependency>

<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
</dependency>

<dependency>
   <groupId>com.oracle</groupId>
   <artifactId>ojdbc12</artifactId>
   <version>12.0.1.2</version>
</dependency>

<dependency>
   <groupId>org.projectlombok</groupId>
   <artifactId>lombok</artifactId>
</dependency>

<!--注意:必须要为以下版本,不然会有问题!!-->
<dependency>
   <groupId>org.apache.shardingsphere</groupId>
   <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
   <version>4.0.0-RC1</version>
</dependency>

注意:其中shardingsphere的版本一定要为4.0.0-RC1,不然就无法使用!!!

SpringBoot中的配置文件

# 设置数据源的名称
spring.shardingsphere.datasource.names=ds0

spring.shardingsphere.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds0.url=jdbc:mysql://localhost:3306/xx?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.ds0.username=xxxx
spring.shardingsphere.datasource.ds0.password=xxxx

# 指定配置表在哪个数据库,表名都是什么
spring.shardingsphere.sharding.tables.t_gggl_log.actual-data-nodes=ds0.t_gggl_log$->{0..1}

# 指定配置表的主键是什么,以及生成的策略(SNOWFLAKE指使用雪花算法)
spring.shardingsphere.sharding.tables.t_gggl_log.key-generator.column=id
spring.shardingsphere.sharding.tables.t_gggl_log.key-generator.type=SNOWFLAKE

# 指定分片策略 奇数数在t_gggl_log0中,偶数在t_gggl_log1中
spring.shardingsphere.sharding.tables.t_gggl_log.table-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.tables.t_gggl_log.table-strategy.inline.algorithm-expression=t_gggl_log$->{id % 2}

# 设置打印sql
spring.shardingsphere.props.sql.show=true

spring.main.allow-bean-definition-overriding=true

遇到的问题:

1.遇到datasource冲突

问题原因

因为DruidDataSourceAutoConfigure创建了一个dataSource。SpringBootConfiguration默认也要创建一个dataSource,所以就冲突了。

***************************
APPLICATION FAILED TO START
***************************

Description:

The bean 'dataSource', defined in class path resource [org/apache/shardingsphere/shardingjdbc/spring/boot/SpringBootConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [com/alibaba/druid/spring/boot/autoconfigure/DruidDataSourceAutoConfigure.class] and overriding is disabled.

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
解决方案

在springboot的配置文件中加上以下配置

spring.main.allow-bean-definition-overriding=true

2.id长度不够

执行插入的时候,插入主键使用雪花算法生成,所有普通的int无法满足长度要求,需要做以下修改:
首先,数据库中的字段要修改字段长度

修改po类中id的类型为Long型

package com.shardingjdbcdemo.model;

import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

@Data
@TableName(value = "T_GGGL_LOG")
public class Log {

    private Long id;

    private String mc;
}

3.启动加载整个库的表元数据,导致启动不起来

只需要设置大于1个数据源,并且没有设置默认的数据源,就不会加载所有的表元数据

spring.shardingsphere.datasource.names=ds0,ds1

spring.shardingsphere.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds0.driver-class-name=oracle.jdbc.OracleDriver
spring.shardingsphere.datasource.ds0.url=jdbc:oracle:thin:@xxx.xxx.xx.x:1521:xxxx
spring.shardingsphere.datasource.ds0.username=xxx
spring.shardingsphere.datasource.ds0.password=xxx

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值