diff --git a/accompany-base/accompany-core/src/main/java/com/accompany/core/util/WorkerIdUtil.java b/accompany-base/accompany-core/src/main/java/com/accompany/core/util/WorkerIdUtil.java index d38bdf79b..769589211 100644 --- a/accompany-base/accompany-core/src/main/java/com/accompany/core/util/WorkerIdUtil.java +++ b/accompany-base/accompany-core/src/main/java/com/accompany/core/util/WorkerIdUtil.java @@ -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 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"); diff --git a/accompany-base/accompany-sharding/accompany-sharding-sdk/src/main/java/com/accompany/sharding/model/BillRecord.java b/accompany-base/accompany-sharding/accompany-sharding-sdk/src/main/java/com/accompany/sharding/model/BillRecord.java index 3c994006e..175c53174 100644 --- a/accompany-base/accompany-sharding/accompany-sharding-sdk/src/main/java/com/accompany/sharding/model/BillRecord.java +++ b/accompany-base/accompany-sharding/accompany-sharding-sdk/src/main/java/com/accompany/sharding/model/BillRecord.java @@ -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; diff --git a/accompany-business/accompany-business-sdk/src/main/java/com/accompany/business/message/BillMessage.java b/accompany-business/accompany-business-sdk/src/main/java/com/accompany/business/message/BillMessage.java index bac817e8a..95456a579 100644 --- a/accompany-business/accompany-business-sdk/src/main/java/com/accompany/business/message/BillMessage.java +++ b/accompany-business/accompany-business-sdk/src/main/java/com/accompany/business/message/BillMessage.java @@ -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; diff --git a/accompany-business/accompany-business-service/src/main/java/com/accompany/business/service/record/BillRecordService.java b/accompany-business/accompany-business-service/src/main/java/com/accompany/business/service/record/BillRecordService.java index cb8c03ed9..f7af508e1 100644 --- a/accompany-business/accompany-business-service/src/main/java/com/accompany/business/service/record/BillRecordService.java +++ b/accompany-business/accompany-business-service/src/main/java/com/accompany/business/service/record/BillRecordService.java @@ -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 private RoomService roomService; @Autowired private BillMessageService billMessageService; + @Autowired + private IdentifierGenerator identifierGenerator; /** * 生成礼物赠送的账单 @@ -143,9 +145,8 @@ public class BillRecordService extends ServiceImpl } 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 billRecord.setAfterAmount(afterA); billRecord.setCreateTime(createTime); billRecord.setRemark(eventEnum.getDesc()); - billRecord.setMessId(messId); billMessageService.sendBillMessage(billRecord);