账单-雪花主键-发mq前预生产分布式序列id

This commit is contained in:
2025-09-08 18:32:06 +08:00
parent 9f4e73a611
commit 45e38eed8e
4 changed files with 15 additions and 36 deletions

View File

@@ -7,7 +7,6 @@ import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Enumeration;
@Slf4j
public class WorkerIdUtil {
@@ -15,7 +14,8 @@ public class WorkerIdUtil {
public static long generateDataCenterId() {
InetAddress host = null;
String ip = null;
byte[] mac = null;
// 优先使用Docker环境变量HOST_IP
String hostIpEnv = System.getenv("HOST_IP");
if (hostIpEnv != null && !hostIpEnv.isEmpty()) {
@@ -26,9 +26,16 @@ public class WorkerIdUtil {
if (null == host){
try {
host = InetAddress.getLocalHost();
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(host);
if (networkInterface != null) {
mac = networkInterface.getHardwareAddress();
}
} catch (UnknownHostException e) {
log.error("[workerId] 获取不到localhost的ip");
throw new RuntimeException(e);
} catch (SocketException e) {
log.error("[workerId] 获取不到ip {} 的 mac地址", ip);
throw new RuntimeException(e);
}
}
ip = host.getHostAddress();
@@ -39,35 +46,6 @@ public class WorkerIdUtil {
throw new RuntimeException("[workerId] 获取不到ip");
}
byte[] mac = null;
// 只有在没有使用环境变量且host不为null的情况下才尝试获取MAC地址
try {
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(host);
if (networkInterface != null) {
mac = networkInterface.getHardwareAddress();
}
} catch (SocketException e) {
log.error("[workerId] 获取不到ip {} 的 mac地址", ip);
}
// 如果通过InetAddress找不到MAC地址则尝试遍历网络接口
if (mac == null) {
try {
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = networkInterfaces.nextElement();
if (!networkInterface.isLoopback() && networkInterface.getHardwareAddress() != null) {
mac = networkInterface.getHardwareAddress();
log.info("[workerId] 通过遍历网络接口获取到MAC地址");
break;
}
}
} catch (SocketException e) {
log.error("[workerId] 遍历网络接口获取MAC地址失败", e);
}
}
// 如果仍然找不到MAC地址则使用IP地址的哈希值
if (mac == null) {
log.warn("[workerId] 无法获取MAC地址使用IP地址哈希生成workerId");

View File

@@ -16,7 +16,7 @@ import java.util.Date;
@TableName("bill_record")
public class BillRecord{
@TableId(value = "bill_id", type = IdType.ASSIGN_ID)
@TableId(value = "bill_id", type = IdType.INPUT)
private Long billId;
private Long uid;

View File

@@ -7,6 +7,7 @@ import java.util.Date;
@Data
public class BillMessage extends BaseMqMessage {
private Long billId;
private Long uid;
private Long targetUid;
private Long roomUid;

View File

@@ -22,7 +22,7 @@ import com.accompany.core.util.DoubleUtil;
import com.accompany.sharding.mapper.BillRecordMapper;
import com.accompany.sharding.model.BillRecord;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
@@ -60,6 +60,8 @@ public class BillRecordService extends ServiceImpl<BillRecordMapper, BillRecord>
private RoomService roomService;
@Autowired
private BillMessageService billMessageService;
@Autowired
private IdentifierGenerator identifierGenerator;
/**
* 生成礼物赠送的账单
@@ -143,9 +145,8 @@ public class BillRecordService extends ServiceImpl<BillRecordMapper, BillRecord>
}
Double beforeA = null != afterA? DoubleUtil.sub(afterA, a) : null;
String messId = DefaultIdentifierGenerator.getInstance().nextUUID(null);
BillRecord billRecord = new BillRecord();
billRecord.setBillId(identifierGenerator.nextId(null).longValue());
billRecord.setBillType(billType.getValue());
billRecord.setUid(uid);
billRecord.setTargetUid(targetUid);
@@ -162,7 +163,6 @@ public class BillRecordService extends ServiceImpl<BillRecordMapper, BillRecord>
billRecord.setAfterAmount(afterA);
billRecord.setCreateTime(createTime);
billRecord.setRemark(eventEnum.getDesc());
billRecord.setMessId(messId);
billMessageService.sendBillMessage(billRecord);