-
Notifications
You must be signed in to change notification settings - Fork 16
sequnce module
Allen edited this page Jul 21, 2017
·
3 revisions
-
ddal-sequence is a module which can provide distributed primary key. It doesn't dependency on the other modules of ddal,so you can include this module independently.
-
ddal-sequence privid a default id rangeg getting strategy: DatabaseIdRangeGetter. you can use it to quickly implement a distributed primary key function.
from: org.hellojavaer.ddal.sequence.DatabaseIdRangeGetter
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
*
* | --------------
* | 700 ~ NULL |
* Master | |
* | |
* | --------------
* _ _ _ _ _ _ _ _ _ _ _ _ _/ _ _ _ _\ _ _ _ _ _ _ _ _ _ _ _ _
* / \
* | ------------- -------------
* | 120 ~ 200 | | 201 ~ 300 |
* Follower | 301 ~ 400 | | 501 ~ 600 |
* | 401 ~ 500 | | 601 ~ 700 |
* | ------------- -------------
* _ _ _ _ _ _ _ _/_ _ _ _ _ \_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
* / \
* | ------------ -------------
* | 1 ~ 20 | | 21 ~ 40 |
* | 41 ~ 60 | | 81 ~ 100 |
* Client | 61 ~ 80 | | 101 ~ 120 |
* ------------ -------------
* / | \ / | \
* | [id] [id] [id] [id] [id] [id]
* _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
- add dependency
<dependency>
<groupId>org.hellojavaer.ddal</groupId>
<artifactId>ddal-sequence</artifactId>
<version>x.x.x</version><!-- goto https://github.com/hellojavaer/ddal get the last release version -->
</dependency>
- init sequence table
CREATE TABLE sequence (
id bigint(20) NOT NULL AUTO_INCREMENT,
schema_name varchar(32) NOT NULL,
table_name varchar(64) NOT NULL,
begin_value bigint(20) NOT NULL,
next_value bigint(20) NOT NULL,
end_value bigint(20) DEFAULT NULL,
step int(11),
skip_n_steps int(11),
select_order int(11) NOT NULL,
version bigint(20) NOT NULL DEFAULT '0',
deleted tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (id),
KEY idx_table_name_schema_name_deleted_select_order (table_name,schema_name,deleted,select_order) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `sequence` (`id`, `schema_name`, `table_name`, `begin_value`, `next_value`, `end_value`, `select_order`, `step`, `skip_n_steps`, `version`, `deleted`)
VALUES (1, 'logical_shema_name', 'logical_table_name', 0, 0, NULL, 0, NULL, NULL, 0, 0);
- config client
DataSource dataSource = ;// TODO: specify the dataSource which can access the sequence table
String schemaName = ;// TODO: specify the schema name which the sequence table belongs to.
IdRangeGetter idRangeGetter = new DatabaseIdRangeGetter(dataSource, schemaName);
// NOTICE: 'logical_shema_name' and 'logical_table_name' are just used as a query condition.
// It doesn't strictly bind with your business table name, but generally using business table name is a good recommendation.
// eg: imagine that you can a group of split user table:user_0,user_1,.... and these tables belong to base schema.
// in this case you should use 'base' and 'user' to replace 'logical_shema_name' and 'logical_table_name'
Sequence sequence = new SingleSequence("logical_shema_name", "logical_table_name", 100, 5, 200, idRangeGetter, 100);
Long id = sequence.nextValue();