SequenceKeyController.java
2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.diligrp.assistant.uid.controller;
import com.diligrp.assistant.shared.domain.Message;
import com.diligrp.assistant.shared.util.AssertUtils;
import com.diligrp.assistant.uid.domain.DefaultSnowflakeKey;
import com.diligrp.assistant.uid.domain.PersistentSequenceKey;
import com.diligrp.assistant.uid.domain.SequenceKeyDTO;
import com.diligrp.assistant.uid.service.KeyGenerator;
import com.diligrp.assistant.uid.service.SequenceKeyService;
import com.diligrp.assistant.uid.service.impl.KeyGeneratorManager;
import com.diligrp.assistant.uid.service.impl.SnowflakeKeyManager;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/uid")
public class SequenceKeyController {
@Resource
private SequenceKeyService sequenceKeyService;
@Resource
private KeyGeneratorManager keyGeneratorManager;
@Resource
private SnowflakeKeyManager snowflakeKeyManager;
@RequestMapping(value = "/sequence/register.do")
public Message<?> register(@RequestBody SequenceKeyDTO request) {
AssertUtils.notEmpty(request.getKey(), "key missed");
AssertUtils.notEmpty(request.getName(), "name missed");
AssertUtils.notNull(request.getValue(), "value missed");
AssertUtils.notNull(request.getStep(), "step missed");
PersistentSequenceKey sequenceKey = PersistentSequenceKey.builder().key(request.getKey())
.name(request.getName()).value(request.getValue()).step(request.getStep()).pattern(request.getPattern())
.expiredOn(request.getExpiredOn()).version(0).build();
sequenceKeyService.registerSequenceKey(sequenceKey);
return Message.success();
}
@RequestMapping(value = "/sequence/get.do")
public Message<?> getSequenceKey(@RequestParam(value = "sequenceKey", required = false) String sequenceKey) {
AssertUtils.notEmpty(sequenceKey, "sequenceKey missed");
KeyGenerator keyGenerator = keyGeneratorManager.getKeyGenerator(sequenceKey);
return Message.success(keyGenerator.nextId());
}
@RequestMapping(value = "/snowflake/get.do")
public Message<?> getSnowflakeKey(@RequestParam(value = "sequenceKey", required = false) String sequenceKey) {
AssertUtils.notEmpty(sequenceKey, "sequenceKey missed");
DefaultSnowflakeKey snowflakeKey = new DefaultSnowflakeKey(sequenceKey);
KeyGenerator keyGenerator = snowflakeKeyManager.getKeyGenerator(snowflakeKey);
return Message.success(keyGenerator.nextId());
}
// TODO:支持类似杭蔬市场批次号生成规则,一定时间范围内随机数不重复
}