File tree Expand file tree Collapse file tree 3 files changed +139
-0
lines changed
api-boot-project/api-boot-autoconfigure/src/main/java/org/minbox/framework/api/boot/autoconfigure/sequence Expand file tree Collapse file tree 3 files changed +139
-0
lines changed Original file line number Diff line number Diff line change 1+ package org .minbox .framework .api .boot .autoconfigure .sequence ;
2+
3+ import org .minbox .framework .api .boot .plugin .sequence .Sequence ;
4+ import org .springframework .boot .autoconfigure .condition .ConditionalOnClass ;
5+ import org .springframework .boot .autoconfigure .condition .ConditionalOnMissingBean ;
6+ import org .springframework .boot .context .properties .EnableConfigurationProperties ;
7+ import org .springframework .context .annotation .Bean ;
8+ import org .springframework .context .annotation .Configuration ;
9+
10+ /**
11+ * 分布式高效ID "Sequence" 自动化配置类
12+ *
13+ * @author 恒宇少年
14+ */
15+ @ Configuration
16+ @ ConditionalOnClass (Sequence .class )
17+ @ EnableConfigurationProperties (ApiBootSequenceProperties .class )
18+ public class ApiBootSequenceAutoConfiguration {
19+ /**
20+ * 注入 "Sequence" 所需要的属性配置类
21+ */
22+ private ApiBootSequenceProperties apiBootSequenceProperties ;
23+
24+ public ApiBootSequenceAutoConfiguration (ApiBootSequenceProperties apiBootSequenceProperties ) {
25+ this .apiBootSequenceProperties = apiBootSequenceProperties ;
26+ }
27+
28+ /**
29+ * 实例化 {@link ApiBootSequenceContext}
30+ * <p>
31+ * 在{@link ApiBootSequenceContext}内进行实例化{@link Sequence},
32+ * 部分配置的默认值请参考{@link ApiBootSequenceProperties}
33+ *
34+ * @return Sequence instance object
35+ */
36+ @ Bean
37+ @ ConditionalOnMissingBean
38+ public ApiBootSequenceContext apiBootSequenceContext () {
39+ return new ApiBootSequenceContext (apiBootSequenceProperties );
40+ }
41+ }
Original file line number Diff line number Diff line change 1+ package org .minbox .framework .api .boot .autoconfigure .sequence ;
2+
3+ import org .minbox .framework .api .boot .plugin .sequence .Sequence ;
4+
5+ /**
6+ * 封装{@link Sequence}的上下文
7+ * <p>
8+ * 通过该类可以直接获取分布式唯一的ID{@link Sequence#nextId()}
9+ *
10+ * @author 恒宇少年
11+ */
12+ public class ApiBootSequenceContext {
13+ /**
14+ * 分布式唯一ID实例
15+ */
16+ private Sequence sequence ;
17+
18+ /**
19+ * 构造函数实例化{@link Sequence}
20+ * <p>
21+ * 根据传递的{@link ApiBootSequenceProperties}属性配置类各个字段的值来实例化{@link Sequence}
22+ *
23+ * @param sequenceProperties Sequence属性配置类
24+ */
25+ public ApiBootSequenceContext (ApiBootSequenceProperties sequenceProperties ) {
26+ this .sequence = new Sequence (
27+ sequenceProperties .getDataCenterId (),
28+ sequenceProperties .getWorkerId (),
29+ sequenceProperties .isClock (),
30+ sequenceProperties .getTimeOffsetMilliseconds (),
31+ sequenceProperties .isRandomSequence ()
32+ );
33+ }
34+
35+ /**
36+ * 获取下一个分布式ID的值
37+ * <p>
38+ * 返回{@link String}类型的值
39+ *
40+ * @return 下一个ID的值
41+ */
42+ public String nextStringId () {
43+ return String .valueOf (this .nextId ());
44+ }
45+
46+ /**
47+ * 获取下一个分布式ID的值
48+ * <p>
49+ * 返回{@link Long} 类型的值
50+ *
51+ * @return 下一个ID的值
52+ */
53+ public Long nextId () {
54+ return this .sequence .nextId ();
55+ }
56+ }
Original file line number Diff line number Diff line change 1+ package org .minbox .framework .api .boot .autoconfigure .sequence ;
2+
3+ import lombok .Data ;
4+ import org .springframework .boot .context .properties .ConfigurationProperties ;
5+ import org .springframework .context .annotation .Configuration ;
6+
7+ import static org .minbox .framework .api .boot .autoconfigure .sequence .ApiBootSequenceProperties .API_BOOT_SEQUENCE_PREFIX ;
8+
9+ /**
10+ * 整合Sequence分布式高效ID的属性相关配置
11+ *
12+ * @author 恒宇少年
13+ */
14+ @ Data
15+ @ Configuration
16+ @ ConfigurationProperties (prefix = API_BOOT_SEQUENCE_PREFIX )
17+ public class ApiBootSequenceProperties {
18+ /**
19+ * 分布式ID配置前缀
20+ */
21+ public static final String API_BOOT_SEQUENCE_PREFIX = "api.boot.sequence" ;
22+ /**
23+ * 数据中心编号,取值范围为0 ~ 3
24+ **/
25+ private int dataCenterId = 1 ;
26+ /**
27+ * 工作机器编号,取值范围为0 ~ 255
28+ */
29+ private int workerId = 1 ;
30+ /**
31+ * 是否解决高并发下获取时间戳的性能问题
32+ */
33+ private boolean clock ;
34+ /**
35+ * 允许时间回拨的毫秒量
36+ */
37+ private long timeOffsetMilliseconds = 5L ;
38+ /**
39+ * 毫秒内的随机序列
40+ */
41+ private boolean randomSequence ;
42+ }
You can’t perform that action at this time.
0 commit comments