Sub-library sub-table practice -Sharding-JDBC

In the most recent study of some of the problems of sub-library sub-table, next weekend just to summarize a little time to do simple, and easy to himself later.
Why do regarding sub-library sub-table, what is the level of sub-table, vertical sub-table concepts, I believe we all know, not here in the repeat.
This article only describes the use of Sharding-JDBC sub-library sub-table to do some practical experience, if there is an error welcome to point out.

What is Sharding-JDBC


Sharding-jdbc is a Dangdang CAPE source 客户端代理middleware. Sharding-jdbc fragmentation and sub-libraries comprising separate read and write functions. No application code invasive, almost no changes, orm framework compatible with the mainstream, the mainstream database connection pool. Currently part of the Apache incubator project ShardingSphere.
Sharding-jdbc positioned as a lightweight Java framework, additional services provided by Java's JDBC layer. It uses direct client database, provide services in the form of jar package, without additional deployment and dependence, understood as enhanced version of the JDBC driver, JDBC and is fully compatible with all kinds of ORM frameworks.
ORM framework for any JDBC-based, such as: JPA, Hibernate, Mybatis, Spring JDBC Template directly or JDBC.
Support any third-party database connection pool, such as: DBCP, C3P0, BoneCP, Druid , HikariCP and so on.
Support any implementation database JDBC specification. Currently supports MySQL, Oracle, SQLServer, PostgreSQL database as well as any follow SQL92 standard.

Official documents address

ShardingSphere: https://shardingsphere.apache.org/
GitHub address: https://github.com/apache/incubator-shardingsphere

Some proposals, and

However, I suggest that you simply had here at the official document, because the document is not comprehensive or feeling is not up to date.
Suggest that you focus can be placed on the official examples git
latest official version is 4.0, if you use springboot created, you can use the following dependence can be.

<dependency>
    <groupId>org.apache.shardingsphere</groupId>
    <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
    <version>4.0.0</version>
</dependency>

Sharding-jdbc powerful, but this article focuses on the following points, places not covered can read the document viewing.
1, the single database sub-table
2, the sub-library sub-table (table containing a single sub-library)
3, and the sub-table queries
4, sub-table transaction
In either also, or sub-libraries sub-table type, the core is nothing more than a few below two configurations:
1, the configuration data source, specifically the number of data sources you have
2, the definition of the table name, sub-table is a logical table name (t_order), and all physical table names (t_order_0, t_order_1)
3, the definition of sub-libraries columns and sub-libraries algorithm
4, the definition of sub-sub-table and the table columns algorithm

Code

Single-library sub-table

sharding-jdbc advantage is that the code is not invasive, we basically do not move the original code, but will be configured to replace the relational database sharding configuration can be connected.
In my personal practice project as an example:
the original configuration, regardless of when tables:

#项目配置
spring:
  #数据连接配置
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://xxx.xx.xx.xx:3306/yyms?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
    username: xxx
    password: xxx

After using the sharding configuration

# 分表配置
spring:
  shardingsphere:
    datasource:
      names: yyms
      yyms:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        jdbc-url: jdbc:mysql://xxx.xx.xx.xx:3306/yyms?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
        username: xx
        password: xxx
    sharding:
      tables:
        # 表名
        sys_log:
          actual-data-nodes: yyms.sys_log_$->{0..1}
          # 分表配置,根据id分表
          table-strategy:
            inline:
              sharding-column: id
              algorithm-expression: sys_log_$->{id % 2}
          # 配置字段的生成策略,column为字段名,type为生成策略,sharding默认提供SNOWFLAKE和UUID两种,可以自己实现其他策略
          key-generator:
            column: id
            type: SNOWFLAKE
    props:
      sql:
        show: true

The above configuration is basically a single reservoir to achieve resolution of sys_log table, modulo algorithm according id, and sys_log_1 sys_log_0 split into two tables. No changes to the code level to achieve the resolution, the resolution effect as FIG.

Note Well, after sys_log table split is actually non-existent.
Of course, using the official default configuration often does not meet our needs.
If you get the id and then to calculate a data repository data in which, no doubt in our daily operation and maintenance maintenance caused great inconvenience. Here we can achieve through some simple custom development configuration.
For example, I want the last id to show the data table where the number is located.

Multi-library sub-table

The first show cases more than a single database table

spring:
  shardingsphere:
    datasource:
      names: ds0,ds1
      ds_1:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        jdbc-url: jdbc:mysql://xxxxxx:3306/ds1?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
        username: xxxx
        password: xxx
      ds0:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        jdbc-url: jdbc:mysql://xxxxx:3306/ds0?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
        username: xxxx
        password: xxxx
    sharding:
      default-data-source-name: ds0
      default-database-strategy:
        inline:
          sharding-column: id
          algorithm-expression: ds$->{id % 2}
      tables:
        sys_log:
          actual-data-nodes: ds$->{0..1}.sys_log
          key-generator:
            column: id
            type: SNOWFLAKE
            props:
              worker:
                id: 123

The single table split into a plurality of libraries, using the same sys_log. The effect is as follows:

in fact, nothing more than a collection of sub-library sub-table two modes above, the specific configuration options, you can refer to the official case Configuration "" " I am a link

After the sub-library sub-table query

select * from sys_log where id='444271380247588864'
Following the above case, as an example in the above statement, id column for the sub-libraries, Sharding is parsed to locate the corresponding data source, the following query directly.
select * from sys_log where id='444271380247588864'
If we adjust the query does not contain a sub-library columns to the following statement as an example:
select * from sys_log where user_name='zhangsan
two sql statement after the execution. We are five data in the two libraries, post a query result set is 10 data, in line with our expectations.
Database:

result set:

Conclusion: When the search condition contains sub-libraries column (sub-table column), this time sharing will first call the sub-library sub-table strategy class, directly to the corresponding database and corresponding sub-table. When the search condition does not contain partakers library column, this time the engine will not call the policy class, but will direct that all library or target library table, above the target library is the case, [ds0, ds1] two data sources, since there are two target library, generated behind DataNode, TableUnits, PreparedStatementUnit will double the previous number, so this time, the engine will eventually initiate concurrent execution of multiple sql statements, and the combined final result back .

After the sub-library sub-table affairs

Sharding-Sphere supports XA transactions and flexible, which allows each access to the database, you can choose the type of transaction. Distributed transaction completely transparent to the business operations, which greatly reduces the cost of the introduction of distributed transactions.
I feel distributed transactions written in official documents and cases are already fairly complete, and here we can refer to: I am a link official case realization, not here in the repeat.
Finally, the project reference code: https://github.com/allanzhuo/yyms

Guess you like

Origin www.cnblogs.com/laoyeye/p/12439252.html