|
@@ -2,6 +2,7 @@ package com.kym.common.config;
|
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
|
|
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
|
|
|
import com.kym.common.utils.IDGenerator;
|
|
import com.kym.common.utils.IDGenerator;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -12,15 +13,19 @@ import org.springframework.stereotype.Component;
|
|
|
@Component
|
|
@Component
|
|
|
public class CustomIdGenerator implements IdentifierGenerator {
|
|
public class CustomIdGenerator implements IdentifierGenerator {
|
|
|
|
|
|
|
|
- private IDGenerator idGenerator;
|
|
|
|
|
|
|
+ private final IDGenerator idGenerator;
|
|
|
|
|
|
|
|
- public CustomIdGenerator() {
|
|
|
|
|
- this.idGenerator = new IDGenerator(0, 0);
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * workerId/datacenterId 支持配置(默认 0),多实例部署时通过
|
|
|
|
|
+ * kym.id.worker-id / kym.id.datacenter-id 区分,避免跨进程同毫秒撞号
|
|
|
|
|
+ */
|
|
|
|
|
+ public CustomIdGenerator(@Value("${kym.id.worker-id:0}") long workerId,
|
|
|
|
|
+ @Value("${kym.id.datacenter-id:0}") long datacenterId) {
|
|
|
|
|
+ this.idGenerator = new IDGenerator(workerId, datacenterId);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public Long nextId(Object entity) {
|
|
public Long nextId(Object entity) {
|
|
|
- final long id = idGenerator.nextId();
|
|
|
|
|
- return id;
|
|
|
|
|
|
|
+ return idGenerator.nextId();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|