AIID方法变更
This commit is contained in:
@@ -1,18 +1,33 @@
|
||||
package com.volvo.ai.analytic.center.utils;
|
||||
|
||||
import cn.hutool.core.lang.Snowflake;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class AiAnalysisUtils {
|
||||
|
||||
/**
|
||||
* 生成ai分析请求id
|
||||
* @param businessType
|
||||
* @return
|
||||
*/
|
||||
private static Snowflake snowflake;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
try {
|
||||
// 使用 IP 生成唯一的 workerId
|
||||
long workerId = ipToWorkerId(getLocalHostIP());
|
||||
snowflake = IdUtil.getSnowflake(workerId, 0); // datacenterId = 0
|
||||
log.info("Initialized Snowflake with workerId: {}", workerId);
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to initialize Snowflake", e);
|
||||
throw new RuntimeException("Snowflake initialization failed");
|
||||
}
|
||||
}
|
||||
|
||||
public static String getAiAnalysisRequestId(String businessType) {
|
||||
if (businessType == null || businessType.trim().isEmpty()) {
|
||||
log.error("businessType is null or empty, using default value 'unknown'");
|
||||
@@ -20,18 +35,26 @@ public class AiAnalysisUtils {
|
||||
}
|
||||
|
||||
try {
|
||||
long snowflakeId = IdUtil.getSnowflakeNextId();
|
||||
if (snowflakeId == 0) {
|
||||
log.error("Failed to generate Snowflake ID");
|
||||
throw new RuntimeException("Failed to generate Snowflake ID");
|
||||
}
|
||||
// 使用自定义的 Snowflake 实例生成 ID
|
||||
long snowflakeId = snowflake.nextId();
|
||||
String aiAnalysisRequestId = businessType + "-" + snowflakeId;
|
||||
log.info("Generated AI analysis request ID: {}", aiAnalysisRequestId);
|
||||
return aiAnalysisRequestId;
|
||||
} catch (Exception e) {
|
||||
log.error("Error generating AI analysis request ID", e);
|
||||
//生成唯一字符串
|
||||
return businessType + "-"+IdUtil.fastSimpleUUID();
|
||||
return businessType + "-" + IdUtil.fastSimpleUUID();
|
||||
}
|
||||
}
|
||||
|
||||
// 获取本机 IP
|
||||
private static String getLocalHostIP() throws UnknownHostException {
|
||||
return InetAddress.getLocalHost().getHostAddress();
|
||||
}
|
||||
|
||||
// 将 IP 转换为合法的 workerId (0 ~ 31)
|
||||
private static long ipToWorkerId(String ip) {
|
||||
String[] parts = ip.replaceAll("[^\\d.]", "").split("\\.");
|
||||
int lastOctet = Integer.parseInt(parts[parts.length - 1]);
|
||||
return lastOctet % 32; // 限制范围 [0, 31]
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user