-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
425 additions
and
393 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
|
||
引入maven依赖 | ||
``` | ||
<dependency> | ||
<groupId>com.netease.nim</groupId> | ||
<artifactId>camellia-id-gen-core</artifactId> | ||
<version>1.3.0</version> | ||
</dependency> | ||
``` | ||
示例如下: | ||
```java | ||
public class CamelliaSegmentIdGenTest { | ||
|
||
private static final AtomicLong id = new AtomicLong(); | ||
|
||
public static void main(String[] args) throws Exception { | ||
CamelliaSegmentIdGenConfig config = new CamelliaSegmentIdGenConfig(); | ||
config.setStep(1000);//每次从数据库获取一批id时的批次大小 | ||
config.setTagCount(1000);//服务包括的tag数量,会缓存在本地内存,如果实际tag数超过本配置,会导致本地内存被驱逐,进而丢失部分id段,丢失后会穿透到数据库) | ||
config.setMaxRetry(10);//当并发请求过来时,只会让一次请求穿透到db,其他请求会等待并重试,本配置表示重试的次数 | ||
config.setRetryIntervalMillis(10);//当并发请求过来时,只会让一次请求穿透到db,其他请求会等待并重试,表示重试间隔 | ||
config.setRegionBits(0);//region比特位,0表示不区分单元 | ||
config.setRegionId(0);//regionId,如果regionBits为0,则regionId必须为0 | ||
|
||
//设置IdLoader,可以使用数据库实现 | ||
config.setIdLoader((tag, step) -> { | ||
IDRange idRange = new IDRange(id.get() + 1, id.addAndGet(step)); | ||
System.out.println("load [" + idRange.getStart() + "-" + idRange.getEnd() + "] in " + Thread.currentThread().getName()); | ||
return idRange; | ||
}); | ||
CamelliaSegmentIdGen idGen = new CamelliaSegmentIdGen(config); | ||
int i=2000; | ||
while (i -- > 0) { | ||
//可以获取一批 | ||
System.out.println(idGen.genIds("tag", 3)); | ||
// Thread.sleep(1000); | ||
//也可以获取一个 | ||
System.out.println(idGen.genId("tag")); | ||
// Thread.sleep(1000); | ||
} | ||
} | ||
} | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
|
||
引入maven依赖 | ||
``` | ||
<dependency> | ||
<groupId>com.netease.nim</groupId> | ||
<artifactId>camellia-id-gen-core</artifactId> | ||
<version>1.3.0</version> | ||
</dependency> | ||
``` | ||
示例如下: | ||
```java | ||
public class CamelliaSnowflakeIdGenTest { | ||
|
||
public static void main(String[] args) { | ||
CamelliaSnowflakeConfig config = new CamelliaSnowflakeConfig(); | ||
config.setRegionBits(0);//单元id所占的比特位数,0表示不区分单元 | ||
config.setRegionId(0);//regionId,如果regionBits为0,则regionId必须为0 | ||
config.setWorkerIdBits(10);//workerId所占的比特位数 | ||
config.setSequenceBits(12);//序列号所占比特位数 | ||
//使用redis生成workerId | ||
config.setWorkerIdGen(new RedisWorkerIdGen(new CamelliaRedisTemplate("redis://@127.0.0.1:6379"))); | ||
|
||
CamelliaSnowflakeIdGen idGen = new CamelliaSnowflakeIdGen(config); | ||
|
||
int i=2000; | ||
while (i -- > 0) { | ||
long id = idGen.genId();//生成id | ||
System.out.println(id); | ||
System.out.println(Long.toBinaryString(id)); | ||
System.out.println(Long.toBinaryString(id).length()); | ||
long ts = idGen.decodeTs(id);//从id中解析出时间戳 | ||
System.out.println(ts); | ||
System.out.println(new Date(ts)); | ||
} | ||
|
||
long target = 1000*10000; | ||
int j = 0; | ||
long start = System.currentTimeMillis(); | ||
while (true) { | ||
idGen.genId(); | ||
j++; | ||
if (j % 100000 == 0) { | ||
System.out.println("i=" + j); | ||
} | ||
if (j >= target) break; | ||
} | ||
long end = System.currentTimeMillis(); | ||
System.out.println("QPS=" + (target / ((end - start)/1000.0))); | ||
//###idea里直接运行的简单测试结果: | ||
//QPS=4061738.424045491 | ||
} | ||
} | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
|
||
引入maven依赖 | ||
``` | ||
<dependency> | ||
<groupId>com.netease.nim</groupId> | ||
<artifactId>camellia-id-gen-core</artifactId> | ||
<version>1.3.0</version> | ||
</dependency> | ||
``` | ||
示例如下: | ||
```java | ||
public class CamelliaStrictIdGenTest { | ||
|
||
private static final AtomicLong id = new AtomicLong(); | ||
|
||
public static void main(String[] args) { | ||
CamelliaStrictIdGenConfig config = new CamelliaStrictIdGenConfig(); | ||
|
||
config.setCacheKeyPrefix("strict");//redis key的前缀 | ||
config.setDefaultStep(10);//默认每次从db获取的id个数,也是最小的个数 | ||
config.setMaxStep(100);//根据id的消耗速率动态调整每次从db获取id的个数,这个是上限值 | ||
config.setLockExpireMillis(3000);//redis缓存里id耗尽时需要穿透到db重新获取,为了控制并发需要一个分布式锁,这是分布式锁的超时时间 | ||
config.setCacheExpireSeconds(3600*24);//id缓存在redis里,redis key的过期时间,默认1天 | ||
config.setCacheHoldSeconds(10);//缓存里的id如果在短时间内被消耗完,则下次获取id时需要多获取一些,本配置是触发step调整的阈值 | ||
config.setRegionBits(0);//单元id所占的比特位数,0表示不区分单元 | ||
config.setRegionId(0);//regionId,如果regionBits为0,则regionId必须为0 | ||
config.setMaxRetry(1000);//缓存中id耗尽时穿透到db,其他线程等待重试的最大次数 | ||
config.setRetryIntervalMillis(5);//缓存中id耗尽时穿透到db,其他线程等待重试的间隔 | ||
|
||
//设置redis template | ||
config.setTemplate(new CamelliaRedisTemplate("redis://@127.0.0.1:6379")); | ||
//设置IdLoader,可以使用数据库实现 | ||
config.setIdLoader((tag, step) -> { | ||
IDRange idRange = new IDRange(id.get() + 1, id.addAndGet(step)); | ||
System.out.println("load [" + idRange.getStart() + "," + idRange.getEnd() + "] in " + Thread.currentThread().getName()); | ||
return idRange; | ||
}); | ||
|
||
CamelliaStrictIdGen idGen = new CamelliaStrictIdGen(config); | ||
int i=2000; | ||
while (i -- > 0) { | ||
//可以获取最新的id,但是不使用 | ||
System.out.println("peek, id = " + idGen.peekId("tag")); | ||
//获取最新的id | ||
System.out.println("get, id = " + idGen.genId("tag")); | ||
} | ||
} | ||
} | ||
``` |
59 changes: 59 additions & 0 deletions
59
docs/camellia-id-gen/quick-start-spring-boot-starter-segment.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
|
||
|
||
使用spring-boot-starter方式下,默认使用数据库来生成id | ||
引入maven依赖 | ||
``` | ||
<dependency> | ||
<groupId>com.netease.nim</groupId> | ||
<artifactId>camellia-id-gen-segment-spring-boot-starter</artifactId> | ||
<version>1.3.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.netease.nim</groupId> | ||
<artifactId>camellia-id-gen-id-loader</artifactId> | ||
<version>1.3.0</version> | ||
</dependency> | ||
``` | ||
编写启动类: | ||
```java | ||
@SpringBootApplication | ||
@ComponentScan(basePackages = {"com.netease.nim.camellia.id.gen.springboot.segment", "com.netease.nim.camellia.id.gen.springboot.idloader"}) | ||
@MapperScan("com.netease.nim.camellia.id.gen.springboot.idloader") | ||
public class Application { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(Application.class); | ||
} | ||
} | ||
|
||
``` | ||
配置application.yml | ||
```yaml | ||
server: | ||
port: 8083 | ||
spring: | ||
application: | ||
name: camellia-id-gen-segment | ||
datasource: | ||
type: com.zaxxer.hikari.HikariDataSource | ||
driver-class-name: com.mysql.jdbc.Driver | ||
url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&rewriteBatchedStatements=false | ||
username: root | ||
password: root | ||
hikari: | ||
minimum-idle: 5 | ||
idle-timeout: 180000 | ||
maximum-pool-size: 10 | ||
connection-timeout: 30000 | ||
connection-test-query: SELECT 1 | ||
|
||
|
||
camellia-id-gen-segment: | ||
region-bits: 0 #region比特位,0表示不区分单元 | ||
region-id: 0 #regionId,如果regionBits为0,则regionId必须为0 | ||
region-id-shifting-bits: 0 #regionId左移多少位 | ||
tag-count: 1000 #服务包括的tag数量,会缓存在本地内存,如果实际tag数超过本配置,会导致本地内存被驱逐,进而丢失部分id段,丢失后会穿透到数据库) | ||
step: 1000 #每次从数据库获取一批id时的批次大小 | ||
max-retry: 500 #当并发请求过来时,只会让一次请求穿透到db,其他请求会等待并重试,本配置表示重试的次数 | ||
retry-interval-millis: 10 #当并发请求过来时,只会让一次请求穿透到db,其他请求会等待并重试,表示重试间隔 | ||
``` |
65 changes: 65 additions & 0 deletions
65
docs/camellia-id-gen/quick-start-spring-boot-starter-snowflake.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
|
||
引入maven依赖 | ||
``` | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.netease.nim</groupId> | ||
<artifactId>camellia-id-gen-snowflake-spring-boot-starter</artifactId> | ||
<version>1.3.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.netease.nim</groupId> | ||
<artifactId>camellia-redis-spring-boot3-starter</artifactId> | ||
<version>1.3.0</version> | ||
</dependency> | ||
</dependencies> | ||
``` | ||
编写启动类: | ||
```java | ||
@SpringBootApplication | ||
@ComponentScan(basePackages = {"com.netease.nim.camellia.id.gen.springboot.snowflake"}) | ||
public class Application { | ||
public static void main(String[] args) { | ||
SpringApplication.run(Application.class); | ||
} | ||
} | ||
``` | ||
配置application.yml | ||
```yaml | ||
server: | ||
port: 8081 | ||
spring: | ||
application: | ||
name: camellia-id-gen-snowflake | ||
camellia-id-gen-snowflake: | ||
sequence-bits: 12 #序列号所占比特位数 | ||
worker-id-bits: 10 #workerId所占的比特位数 | ||
region-bits: 0 #单元id所占的比特位数,0表示不区分单元 | ||
region-id: 0 #regionId,如果regionBits为0,则regionId必须为0 | ||
worker-id: -1 #-1表示使用redis生成workerId | ||
redis-worker-id-gen-conf: | ||
namespace: camellia #使用redis生成workerId时不同的命名空间下,workerId生成互不干扰 | ||
lock-expire-millis: 3000 #使用redis生成workerId时获取分布式锁时的超时时间 | ||
renew-interval-millis: 1000 #使用redis生成workerId时续约workerId的间隔 | ||
exit-if-renew-fail: false #如果续约失败(可能redis超时了,或者gc导致workerId被其他进程抢走了,概率较低),是否进程退出,默认false | ||
|
||
|
||
camellia-redis: | ||
type: local | ||
local: | ||
resource: redis://@127.0.0.1:6379 | ||
redis-conf: | ||
jedis: | ||
timeout: 2000 | ||
min-idle: 0 | ||
max-idle: 32 | ||
max-active: 32 | ||
max-wait-millis: 2000 | ||
jedis-cluster: | ||
max-wait-millis: 2000 | ||
min-idle: 0 | ||
max-idle: 8 | ||
max-active: 16 | ||
max-attempts: 5 | ||
timeout: 2000 | ||
``` |
87 changes: 87 additions & 0 deletions
87
docs/camellia-id-gen/quick-start-spring-boot-starter-strict.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
|
||
引入maven依赖 | ||
``` | ||
<dependency> | ||
<groupId>com.netease.nim</groupId> | ||
<artifactId>camellia-id-gen-strict-spring-boot-starter</artifactId> | ||
<version>1.3.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.netease.nim</groupId> | ||
<artifactId>camellia-id-gen-id-loader</artifactId> | ||
<version>1.3.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.netease.nim</groupId> | ||
<artifactId>camellia-redis-spring-boot3-starter</artifactId> | ||
<version>1.3.0</version> | ||
</dependency> | ||
``` | ||
编写启动类: | ||
```java | ||
@SpringBootApplication | ||
@ComponentScan(basePackages = {"com.netease.nim.camellia.id.gen.springboot.segment", "com.netease.nim.camellia.id.gen.springboot.idloader"}) | ||
@MapperScan("com.netease.nim.camellia.id.gen.springboot.idloader") | ||
public class Application { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(Application.class); | ||
} | ||
} | ||
|
||
``` | ||
配置application.yml | ||
```yaml | ||
server: | ||
port: 8082 | ||
spring: | ||
application: | ||
name: camellia-id-gen-segment | ||
datasource: | ||
type: com.zaxxer.hikari.HikariDataSource | ||
driver-class-name: com.mysql.jdbc.Driver | ||
url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&rewriteBatchedStatements=false | ||
username: root | ||
password: root | ||
hikari: | ||
minimum-idle: 5 | ||
idle-timeout: 180000 | ||
maximum-pool-size: 10 | ||
connection-timeout: 30000 | ||
connection-test-query: SELECT 1 | ||
|
||
|
||
camellia-id-gen-strict: | ||
region-bits: 0 #单元id所占的比特位数,0表示不区分单元 | ||
region-id: 0 #regionId,如果regionBits为0,则regionId必须为0 | ||
region-id-shifting-bits: 0 #regionId左移多少位 | ||
cache-key-prefix: strict #redis key的前缀 | ||
lock-expire-millis: 3000 #redis缓存里id耗尽时需要穿透到db重新获取,为了控制并发需要一个分布式锁,这是分布式锁的超时时间 | ||
cache-expire-seconds: 86400 #id缓存在redis里,redis key的过期时间,默认1天 | ||
cache-hold-seconds: 10 #缓存里的id如果在短时间内被消耗完,则下次获取id时需要多获取一些,本配置是触发step调整的阈值 | ||
max-retry: 1000 #缓存中id耗尽时穿透到db,其他线程等待重试的最大次数 | ||
retry-interval-millis: 5 #缓存中id耗尽时穿透到db,其他线程等待重试的间隔 | ||
default-step: 10 #默认每次从db获取的id个数,也是最小的个数 | ||
max-step: 100 #根据id的消耗速率动态调整每次从db获取id的个数,这个是上限值 | ||
|
||
|
||
|
||
camellia-redis: | ||
type: local | ||
local: | ||
resource: redis://@127.0.0.1:6379 | ||
redis-conf: | ||
jedis: | ||
timeout: 2000 | ||
min-idle: 0 | ||
max-idle: 32 | ||
max-active: 32 | ||
max-wait-millis: 2000 | ||
jedis-cluster: | ||
max-wait-millis: 2000 | ||
min-idle: 0 | ||
max-idle: 8 | ||
max-active: 16 | ||
max-attempts: 5 | ||
timeout: 2000 | ||
``` |
Oops, something went wrong.