diff --git a/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/info/ClusterInfoManager.java b/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/info/ClusterInfoManager.java index dd28ab79..26472153 100644 --- a/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/info/ClusterInfoManager.java +++ b/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/info/ClusterInfoManager.java @@ -3,14 +3,12 @@ import com.fasterxml.jackson.databind.ObjectMapper; import kr.hakdang.cadio.common.Jsons; import kr.hakdang.cadio.core.domain.bootstrap.BootstrapProvider; -import kr.hakdang.cadio.core.domain.cluster.ClusterConnection; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import java.io.File; import java.util.Collections; import java.util.List; -import java.util.UUID; /** * ClusterManager @@ -51,7 +49,7 @@ public void register(ClusterInfoRegisterArgs args) { } List result = om.readValue(clusterJsonFile, TYPED); - result.add(args.makeClusterInfo(UUID.randomUUID().toString())); + result.add(args.makeClusterInfo(ClusterUtils.generateClusterId())); om.writeValue(clusterJsonFile, result); diff --git a/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/info/ClusterUtils.java b/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/info/ClusterUtils.java new file mode 100644 index 00000000..3b9547a8 --- /dev/null +++ b/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/info/ClusterUtils.java @@ -0,0 +1,34 @@ +package kr.hakdang.cadio.core.domain.cluster.info; + +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.UUID; + +/** + * ClusterUtils + * + * @author akageun + * @since 2024-07-05 + */ +public abstract class ClusterUtils { + + public static String generateClusterId() { + String uuid = UUID.randomUUID().toString(); + byte[] temp = uuid.getBytes(StandardCharsets.UTF_8); + byte[] test; + try { + MessageDigest digest = MessageDigest.getInstance("SHA-256"); + test = digest.digest(temp); + } catch (NoSuchAlgorithmException e) { + throw new RuntimeException(e); + } + + StringBuilder sb = new StringBuilder(); + for (int j = 0; j < 4; j++) { + sb.append(String.format("%02x", test[j])); + } + + return sb.toString(); + } +} diff --git a/cadio-core/src/test/java/kr/hakdang/cadio/core/domain/cluster/info/ClusterUtilsTest.java b/cadio-core/src/test/java/kr/hakdang/cadio/core/domain/cluster/info/ClusterUtilsTest.java new file mode 100644 index 00000000..b17c909c --- /dev/null +++ b/cadio-core/src/test/java/kr/hakdang/cadio/core/domain/cluster/info/ClusterUtilsTest.java @@ -0,0 +1,16 @@ +package kr.hakdang.cadio.core.domain.cluster.info; + +import kr.hakdang.cadio.BaseTest; +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.Test; + +@Slf4j +class ClusterUtilsTest extends BaseTest { + + @Test + void generateClusterIdTest() { + for (int i = 0; i < 10; i++) { + log.info("id : {}", ClusterUtils.generateClusterId()); + } + } +}