xsequence distributed serial number generation component version 1.0 is finally released

1, Frontiers

After a day's tossing, I finally released the jar of xsequence to maven. After that, I have the motivation to start, maintain some documents, and constantly upgrade and improve this component. After each upgrade, a document will be maintained to explain some new features and usage.

2. Source address

https://gitee.com/xuan698400/xsequence

3. Maven support

<dependency>
    <groupId>com.xuanner</groupId>
    <artifactId>xsequence-core</artifactId>
    <version>1.0</version>
</dependency>

4. DB mode instructions

(1) API usage

public class SequenceTest_Api {

    private com.xuanner.seq.sequence.Sequence userSeq;

    @Before
    public void setup() {
        //data source
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl("jdbc:mysql://121.196.218.206:3306/admin?characterEncoding=UTF-8");
        dataSource.setUsername("admin");
        dataSource.setPassword("xxx");
        dataSource.setMaxActive(300);
        dataSource.setMinIdle(50);
        dataSource.setInitialSize(2);
        dataSource.setMaxWait(500);
        //Using DB to get interval Manager
        DbSeqRangeMgr dbSeqRangeMgr = new DbSeqRangeMgr();
        dbSeqRangeMgr.setDataSource(dataSource);
        dbSeqRangeMgr.setTableName("sequence");
        dbSeqRangeMgr.setRetryTimes(100);
        dbSeqRangeMgr.setStep(100);
        dbSeqRangeMgr.setStepStart(0);
        dbSeqRangeMgr.init();
        //Build serial number generator
        DefaultRangeSequence defaultRangeSequence = new DefaultRangeSequence();
        defaultRangeSequence.setName("user");
        defaultRangeSequence.setSeqRangeMgr(dbSeqRangeMgr);
        userSeq = defaultRangeSequence;
    }

    @Test
    public void test() {
        long start = System.currentTimeMillis();
        for (int i = 0; i < 100; i++) {
            System.out.println("++++++++++id:" + userSeq.nextValue());
        }
        System.out.println("interval time:" + (System.currentTimeMillis() - start));
    }
}

(2) Spring configuration

Step 1: configure spring's xml file:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd" default-autowire="byName"
       default-lazy-init="false">

    <!-- data source -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="url" value="jdbc:mysql://121.196.218.206:3306/admin?characterEncoding=UTF-8"/>
        <property name="username" value="admin"/>
        <property name="password" value="xxx"/>
        <property name="maxActive" value="300"/>
        <property name="minIdle" value="50"/>
        <property name="initialSize" value="2"/>
        <property name="maxWait" value="500"/>
    </bean>

    <!-- Serial number step Manager -->
    <bean id="dbSeqRangeMgr" class="com.xuanner.seq.range.impl.db.DbSeqRangeMgr" init-method="init">
        <!-- data source[Mandatory] -->
        <property name="dataSource" ref="dataSource"/>
        <!-- Table name[Optional] Acquiescence: sequence-->
        <property name="tableName" value="sequence"/>
        <!-- Retry times of update failure[Optional] Default: 100-->
        <property name="retryTimes" value="100"/>
        <!-- Each access step[Optional] Default: 1000-->
        <property name="step" value="1000"/>
        <!-- Start number, note that the actual start available number is stepStart+1,for example stepStart=0 Indicates starting from 1[Optional] Default: 0-->
        <property name="stepStart" value="0"/>
    </bean>

    <!-- Specific use demo -->
    <bean id="userSeq" class="com.xuanner.seq.sequence.impl.DefaultRangeSequence">
        <property name="seqRangeMgr" ref="dbSeqRangeMgr"/>
        <property name="name" value="user"/>
    </bean>
</beans>

Step 2: you can use it directly in the code

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:seq-test.xml" })
public class SequenceTest_Spring {

    @Autowired
    private Sequence userSeq;

    @Test
    public void test() {
        for (int i = 0; i < 1000; i++) {
            System.out.println("++++++++++id:" + userSeq.nextValue());
        }
    }

}

Tags: Spring xml Maven JDBC

Posted on Wed, 18 Mar 2020 11:38:52 -0400 by kaeRock