溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

一小時(shí)讀懂Sharding JDBC之分庫(kù)分表

發(fā)布時(shí)間:2020-04-04 11:13:07 來(lái)源:網(wǎng)絡(luò) 閱讀:246 作者:小學(xué)生學(xué)ui 欄目:編程語(yǔ)言

作為輕量級(jí)java框架,sharding JDBC在Java的jdbc層提供了額外的服務(wù),可以理解為增強(qiáng)版的jdbc驅(qū)動(dòng)。其中,分庫(kù)分表的操作是其中的重要一環(huán),接下來(lái)就跟隨我來(lái)看一看,這一操作如何進(jìn)行。
環(huán)境準(zhǔn)備
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version></parent>
<properties>
<java.version>1.8</java.version>
<sharding.version>3.1.0</sharding.version></properties>
<dependencies>
<dependency>
<groupId>io.shardingsphere</groupId>
<artifactId>sharding-jdbc-core</artifactId>
<version>${sharding.version}</version>
</dependency>

<dependency>
    <groupId>io.shardingsphere</groupId>
    <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
    <version>${sharding.version}</version>
</dependency>

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.10</version>
</dependency>

<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.4.5</version>
</dependency>

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.1</version>
</dependency>

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

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

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

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

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins></build>
domain
// 建立domain@Setter@Getter@ToString@NoArgsConstructor@AllArgsConstructorpublic class Employee {
private Long id;
private String name;}
配置類
@SpringBootApplication@MapperScan("cn.wolfcode.sharding.mapper")public class ShardingApplication { }
分庫(kù)分表
案例模型
把數(shù)據(jù)分別存放在兩臺(tái)服務(wù)器的兩個(gè)數(shù)據(jù)庫(kù)中表,通過(guò)分片算法來(lái)決定當(dāng)前的數(shù)據(jù)存放在哪個(gè)數(shù)據(jù)庫(kù)的哪個(gè)表中,由于一個(gè)連接池只能連接一個(gè)特定的數(shù)據(jù)庫(kù),所以這里需要?jiǎng)?chuàng)建多個(gè)連接池對(duì)象
建表
-- 分別在2臺(tái)服務(wù)器中建立數(shù)據(jù)庫(kù)sharding,并且建表employee_0和employee_1CREATE TABLE employee_0 (
id bigint(20) PRIMARY KEY AUTO_INCREMENT,
name varchar(255) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8;-- ###################################CREATE TABLE employee_1 (
id bigint(20) PRIMARY KEY AUTO_INCREMENT,
name varchar(255) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8;
application.properties

定義連接池

sharding.jdbc.datasource.names=db0,db1

格式sharding.jdbc.datasource.連接池名.xxx:設(shè)置4要素信息

sharding.jdbc.datasource.db0.type=com.alibaba.druid.pool.DruidDataSource
sharding.jdbc.datasource.db0.driver-class-name=com.mysql.jdbc.Driver
sharding.jdbc.datasource.db0.url=jdbc:mysql://db0Ip:port/sharing
sharding.jdbc.datasource.db0.username=xxx
sharding.jdbc.datasource.db0.password=xxx

sharding.jdbc.datasource.db1.type=com.alibaba.druid.pool.DruidDataSource
sharding.jdbc.datasource.db1.driver-class-name=com.mysql.jdbc.Driver
sharding.jdbc.datasource.db1.url=jdbc:mysql://db1Ip:port/sharing
sharding.jdbc.datasource.db1.username=xxx
sharding.jdbc.datasource.db1.password=xxx

設(shè)置分庫(kù)規(guī)則

sharding.jdbc.config.sharding.default-database-strategy.inline.sharding-column:分庫(kù)列

sharding.jdbc.config.sharding.default-database-strategy.inline.algorithm-expression:分庫(kù)算法

sharding.jdbc.config.sharding.default-database-strategy.inline.sharding-column=id
sharding.jdbc.config.sharding.default-database-strategy.inline.algorithm-expression=db$->{id % 2}

綁定邏輯表

sharding.jdbc.config.sharding.binding-tables=employee

設(shè)置分表規(guī)則

sharding.jdbc.config.sharding.tables.邏輯表.actual-data-nodes:邏輯表對(duì)應(yīng)的真實(shí)表

sharding.jdbc.config.sharding.tables.邏輯表.table-strategy.inline.sharding-column:分表列

sharding.jdbc.config.sharding.tables.邏輯表.table-strategy.inline.algorithm-expression:分表算法

sharding.jdbc.config.sharding.tables.邏輯表.key-generator-column-name:主鍵列

sharding.jdbc.config.sharding.tables.employee.actual-data-nodes=db$->{0..1}.employee$->{0..1}
sharding.jdbc.config.sharding.tables.employee.table-strategy.inline.sharding-column=id
sharding.jdbc.config.sharding.tables.employee.table-strategy.inline.algorithm-expression=employee
$->{id % 2}
sharding.jdbc.config.sharding.tables.employee.key-generator-column-name=id

打印日志

sharding.jdbc.config.props.sql.show=true
mapper
/**

  • 這里寫的employee表是上面所配置的邏輯表
  • 底層會(huì)根據(jù)分片規(guī)則,把我們寫的邏輯表改寫為數(shù)據(jù)庫(kù)中的真實(shí)表
    /@Mapperpublic interface EmployeeMapper {
    @Select("select
    from employee")
    List<Employee> selectAll();

    @Insert("insert into employee (name) values (#{name})")
    void inser(Employee entity);}
    測(cè)試
    @RunWith(SpringRunner.class)@SpringBootTest(classes=ShardingApplication.class)public class ShardingApplicationTests {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Test
    public void save() {
    for (int i = 0; i < 10; i++) {
    Employee employee = new Employee();
    employee.setName("xx"+i);
    employeeMapper.inser(employee);
    }
    }

    @Test
    public void list() {
    employeeMapper.selectAll().forEach(System.out::println);
    }}
    優(yōu)缺點(diǎn)
    ?拆分后單表數(shù)據(jù)量比較小,單表大數(shù)據(jù)被拆分,解決了單表大數(shù)據(jù)訪問(wèn)問(wèn)題
    ?分表以什么切分如果弄的不好,導(dǎo)致多次查詢,而且有時(shí)候要跨庫(kù)操作,甚至導(dǎo)致join無(wú)法使用,對(duì)排序分組等有性能影響
    ?之前的原子操作被拆分成多個(gè)操作,事務(wù)處理變得復(fù)雜
    ?多個(gè)DB維護(hù)成本增加

分庫(kù)分表也僅僅是shardingJDBC中小小的一部分,還有很多內(nèi)容需要大家繼續(xù)研究,如果你對(duì)此感興趣的話,不妨關(guān)注我,接下來(lái)我還會(huì)發(fā)布更多的相關(guān)教程供大家學(xué)習(xí),如果遇到問(wèn)題我也會(huì)盡力幫助你。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI