The SHARDING-JDBC of distributed database middleware

Source: Internet
Author: User
Tags generator

Small series in doing SHARDING-JDBC today, there are some problems, on the Internet Bai, found on-line SHARDING-JDBC reference is very little, alas or to continue to learn to read the document.

SHARDING-JDBC Introduction

SHARDING-JDBC is a database horizontal extension framework that is separated from the relational database module DD-RDB in the application framework Ddframe, which is the transparent database sub-table access.

In the era of high concurrency in the Internet, in order to cope with the high concurrent read and write of DB, we use read-write separation technology. Read-write separation refers to the use of database master-slave technology (copying data into multiple nodes), decentralized read multiple libraries to support high-concurrency reading, and write only on the master library. The master-slave technology in DB is only responsible for copying and synchronizing data, while the read-write separation technology needs to be implemented by the business application itself. Sharding-jdbc through simple development, can easily realize the reading and writing separation technology.

Read-Write Separation implementation

Library and table structure design diagram:

code example

I'm here. Use SSH for test (spring+struts2+hibernate) configuration please refer to: http://www.cnblogs.com/niechen/p/8619713.html

Here we use yml way to do the sub-database table, here only shows the sub-table, the principle of the library is the same as the first to create a sharding-jdbc-core.yml file

datasources:  !! com.alibaba.druid.pool.DruidDataSource    driverClassName:com.mysql.jdbc.Driver    url:jdbc:mysql: // localhost:3306/Database     username:root    password:shardingrule:  tables:    class:      actualdatanodes:ds.class_${0..3}      tablestrategy:        inline:          shardingcolumn:id          % 4}      keygeneratorcolumnname:id  defaultkeygeneratorclass: Com.bdqn.lyrk.ssh.study.generator.MyKeyGenerator

Using 4 tables as an example

Here is a brief introduction to the above is the data of the split here only to do a data source!! followed by the kind of data sources we used have a lot of dbcp and so on. Ali's Druid here, not much to say in the back.

The following is a split of the table

Class: Represents the logical table name

Actualdatanodes: Data source name. class_${0..3} is the three table corresponding to Class_0,class_1,class_2,class_3, if it is multiple data sources, it is: Data source name _${..... CLASS_${0..3}

Tablestategy The following parameter is a specification for setting a split table

Shardingcolumn: According to which column to contract to split the table, generally we are based on the primary key so here is the ID

Algorithmexpression: Agreed to the rules of the split table, here is the 4 table corresponding to 0,1,2,3, then if the balance of 4 is definitely between 0~3, then so is class_${id% 4}

Keygeneratorcolumnname: The generator for ID is specified here

Defaultkeygenerotorclass: Specify the corresponding generator of our own

 PackageCom.bdqn.lyrk.ssh.study.generator;ImportIo.shardingjdbc.core.keygen.KeyGenerator;Importorg.springframework.context.annotation.Configuration;ImportJava.util.Random;/** * @authorYang Tianle * @date 2018/4/16 21:10*/@Configuration Public classMykeygeneratorImplementsKeygenerator {@Override PublicNumber GenerateKey () {random random=NewRandom (); introm= Random.nextint (100); returnROM; }}
View Code

Note here with the random generation do not use math to generate, or there will be decimals, then must not find the corresponding table, this is a small part of the problem of the pit dad today.

Next we want to load the yml that we just configured, we create a Shardingjdbcconfig.java

 PackageCom.bdqn.lyrk.ssh.study.config;Importio.shardingjdbc.core.api.ShardingDataSourceFactory;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Configuration;ImportJavax.sql.DataSource;ImportJava.io.File;Importjava.io.IOException;Importjava.sql.SQLException;/** * @authorYang Tianle * @date 2018/4/16 17:10*/@Configuration Public classShardingjdbcconfig {@Bean PublicDataSource DataSource ()throwsIOException, SQLException {DataSource DataSource= Shardingdatasourcefactory.createdatasource (NewFile (shardingjdbcconfig.class. getClassLoader (). GetResource ("Sharding-jdbc-core.yml"). GetFile ())); returnDataSource; }}
View Code

Load our YML configuration. It must be from ClassLoader to get the configuration.

Next, let's test the add operation (business layer I won't write, just a Hibernate save method), insert 5 data to see where they were inserted? (I emptied the table's data)

This is the 5 data I inserted in order 0~3

You can also be based on these IDs to find the remainder, see the corresponding table?

Refer to Sharding official documentation and official DEMO,SSH integration examples http://www.cnblogs.com/niechen/p/8619713.html

The SHARDING-JDBC of distributed database middleware

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.