xsequence分佈式序列號生成組件1.0版本終於發佈了

一、前沿

折騰了一天,終於把xsequence的jar發佈到maven上去了。以後就有動力開始,維護一些文檔,並不斷的升級完善這個組件嘍。之後每次升級一次版本都會維護一份文檔,說明一些新的特性和使用方式。html

二、源碼地址

https://gitee.com/xuan698400/xsequencejava

三、Maven支持

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

四、DB方式使用說明

(1)API方式使用mysql

public class SequenceTest_Api {

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

    @Before
    public void setup() {
        //數據源
        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);
        //利用DB獲取區間管理器
        DbSeqRangeMgr dbSeqRangeMgr = new DbSeqRangeMgr();
        dbSeqRangeMgr.setDataSource(dataSource);
        dbSeqRangeMgr.setTableName("sequence");
        dbSeqRangeMgr.setRetryTimes(100);
        dbSeqRangeMgr.setStep(100);
        dbSeqRangeMgr.setStepStart(0);
        dbSeqRangeMgr.init();
        //構建序列號生成器
        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配置git

第一步:配置spring的xml文件:spring

<?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">

    <!-- 數據源 -->
    <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>

    <!-- 序列號步長管理器 -->
    <bean id="dbSeqRangeMgr" class="com.xuanner.seq.range.impl.db.DbSeqRangeMgr" init-method="init">
        <!-- 數據源[必選] -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 表名[可選] 默認:sequence-->
        <property name="tableName" value="sequence"/>
        <!-- 更新失敗重試次數[可選] 默認:100-->
        <property name="retryTimes" value="100"/>
        <!-- 每次取數步長[可選] 默認:1000-->
        <property name="step" value="1000"/>
        <!-- 起始數,注意真實開始可用數是stepStart+1,例如stepStart=0表示從1開始[可選] 默認:0-->
        <property name="stepStart" value="0"/>
    </bean>

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

第二步:直接在代碼裏面就能夠使用了sql

@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());
        }
    }

}
相關文章
相關標籤/搜索