当前位置: 首页 > 工具软件 > xsequence > 使用案例 >

Java中sequence间隔生成_xsequence分布式序列号生成组件1.2版本发布

梁丘波
2023-12-01

1、历史版本

v1.0   支持DB方式生成序列号

文档:https://my.oschina.net/u/1271235/blog/1808103

v1.1   支持Redis方式生成序列号

文档:https://my.oschina.net/u/1271235/blog/1809437

2、新版本特性说明

v1.2   支持雪花算法生成序列号

3、使用教程

(1)API使用方式

public class SnowflakeTest_Api {

private Sequence userSeq;

@Before

public void setup() {

SnowflakeSequence snowflakeSequence = new SnowflakeSequence();

snowflakeSequence.setDatacenterId(1);

snowflakeSequence.setWorkerId(2);

userSeq = snowflakeSequence;

}

@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使用方式

xml配置

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

代码使用

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = { "classpath:snowflake-test.xml" })

public class SnowflakeTest_Spring {

@Autowired

private Sequence userSeq;

@Test

public void test() {

for (int i = 0; i < 1000; i++) {

System.out.println("++++++++++id:" + userSeq.nextValue());

}

}

}

转载至链接:https://my.oschina.net/u/1271235/blog/1812305

 类似资料: