diff --git a/mica-core/src/main/java/net/dreamlu/mica/core/utils/StringUtil.java b/mica-core/src/main/java/net/dreamlu/mica/core/utils/StringUtil.java index f76b36208..9cba8aee3 100644 --- a/mica-core/src/main/java/net/dreamlu/mica/core/utils/StringUtil.java +++ b/mica-core/src/main/java/net/dreamlu/mica/core/utils/StringUtil.java @@ -24,6 +24,7 @@ import java.util.Collection; import java.util.Map; import java.util.Random; +import java.util.concurrent.ThreadLocalRandom; import java.util.regex.Pattern; import java.util.stream.Stream; @@ -417,21 +418,21 @@ public static boolean simpleMatch(@Nullable String[] patterns, String str) { } /** - * 生成uuid(冲突概率小),采用 jdk 9 的形式,优化性能 + * 生成uuid(更快,存在冲突的可能),采用 jdk 9 的形式,优化性能 * * @return UUID */ public static String getUUID() { - return getUUID(Holder.SECURE_RANDOM); + return getUUID(ThreadLocalRandom.current()); } /** - * 生成安全的 uuid(更快,存在冲突的可能),采用 jdk 9 的形式,优化性能 + * 生成安全的 uuid(冲突概率小),采用 jdk 9 的形式,优化性能 * * @return UUID */ - public static String getFastUUID() { - return getUUID(Holder.RANDOM); + public static String getSafeUUID() { + return getUUID(Holder.SECURE_RANDOM); } private static String getUUID(Random random) { @@ -453,7 +454,7 @@ private static String getUUID(Random random) { * @return NanoId */ public static String getNanoId() { - return getNanoId(Holder.SECURE_RANDOM, true); + return getNanoId(ThreadLocalRandom.current(), true); } /** @@ -462,7 +463,7 @@ public static String getNanoId() { * @return NanoId */ public static String getNanoId62() { - return getNanoId(Holder.SECURE_RANDOM, false); + return getNanoId(ThreadLocalRandom.current(), false); } /** @@ -470,8 +471,8 @@ public static String getNanoId62() { * * @return NanoId */ - public static String getFastNanoId() { - return getNanoId(Holder.RANDOM, true); + public static String getSafeNanoId() { + return getNanoId(Holder.SECURE_RANDOM, true); } /** @@ -479,8 +480,8 @@ public static String getFastNanoId() { * * @return NanoId */ - public static String getFastNanoId62() { - return getNanoId(Holder.RANDOM, false); + public static String getSafeNanoId62() { + return getNanoId(Holder.SECURE_RANDOM, false); } private static String getNanoId(Random random, boolean radix64) { diff --git a/mica-core/src/test/java/net/dreamlu/mica/test/utils/StringTest.java b/mica-core/src/test/java/net/dreamlu/mica/test/utils/StringTest.java index 108fbd951..6d11399d4 100644 --- a/mica-core/src/test/java/net/dreamlu/mica/test/utils/StringTest.java +++ b/mica-core/src/test/java/net/dreamlu/mica/test/utils/StringTest.java @@ -9,40 +9,46 @@ public class StringTest { public static void main(String[] args) { long startNs1 = System.nanoTime(); - for (int i = 0; i < 10000; i++) { + for (int i = 0; i < 100000; i++) { System.out.println(StringUtil.getUUID()); } long tookMs1 = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs1); long startNs2 = System.nanoTime(); - for (int i = 0; i < 10000; i++) { + for (int i = 0; i < 100000; i++) { System.out.println(UUID.randomUUID()); } long tookMs2 = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs2); long startNs3 = System.nanoTime(); - for (int i = 0; i < 10000; i++) { + for (int i = 0; i < 100000; i++) { System.out.println(StringUtil.getUUID()); } long tookMs3 = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs3); long startNs4 = System.nanoTime(); - for (int i = 0; i < 10000; i++) { - System.out.println(UUID.randomUUID()); + for (int i = 0; i < 100000; i++) { + System.out.println(StringUtil.getNanoId()); } long tookMs4 = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs4); long startNs5 = System.nanoTime(); - for (int i = 0; i < 10000; i++) { - System.out.println(StringUtil.getUUID()); + for (int i = 0; i < 100000; i++) { + System.out.println(StringUtil.getSafeUUID()); } long tookMs5 = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs5); long startNs6 = System.nanoTime(); - for (int i = 0; i < 10000; i++) { - System.out.println(StringUtil.getFastUUID()); + for (int i = 0; i < 100000; i++) { + System.out.println(StringUtil.getSafeNanoId()); } long tookMs6 = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs6); + long startNs7 = System.nanoTime(); + for (int i = 0; i < 100000; i++) { + System.out.println(UUID.randomUUID()); + } + long tookMs7 = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs7); System.out.println("time1:" + tookMs1); System.out.println("time2:" + tookMs2); System.out.println("time3:" + tookMs3); System.out.println("time4:" + tookMs4); System.out.println("time5:" + tookMs5); System.out.println("time6:" + tookMs6); + System.out.println("time7:" + tookMs7); } } diff --git a/mica-core/src/test/java/net/dreamlu/mica/test/utils/UUIDTest.java b/mica-core/src/test/java/net/dreamlu/mica/test/utils/UUIDTest.java index 9676905a4..c1042f284 100644 --- a/mica-core/src/test/java/net/dreamlu/mica/test/utils/UUIDTest.java +++ b/mica-core/src/test/java/net/dreamlu/mica/test/utils/UUIDTest.java @@ -25,7 +25,7 @@ void test() { int size = 100_0000; for (int i = 0; i < size; i++) { service.submit(() -> { - String nanoId = StringUtil.getFastNanoId(); + String nanoId = StringUtil.getNanoId(); if (uuidSet.contains(nanoId)) { System.out.println("-----------存在冲突-------"); } else { diff --git a/mica-nats/src/main/java/net/dreamlu/mica/nats/config/NatsStreamConfiguration.java b/mica-nats/src/main/java/net/dreamlu/mica/nats/config/NatsStreamConfiguration.java index eeedc6396..ec132d670 100644 --- a/mica-nats/src/main/java/net/dreamlu/mica/nats/config/NatsStreamConfiguration.java +++ b/mica-nats/src/main/java/net/dreamlu/mica/nats/config/NatsStreamConfiguration.java @@ -21,7 +21,10 @@ import io.nats.client.api.StreamInfo; import io.nats.client.support.JsonUtils; import lombok.extern.slf4j.Slf4j; +import net.dreamlu.mica.nats.core.DefaultNatsStreamTemplate; import net.dreamlu.mica.nats.core.NatsStreamListenerDetector; +import net.dreamlu.mica.nats.core.NatsStreamTemplate; +import net.dreamlu.mica.nats.core.NatsTemplate; import net.dreamlu.mica.nats.utils.StreamConfigurationUtil; import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.AutoConfiguration; @@ -69,4 +72,9 @@ public NatsStreamListenerDetector natsStreamListenerDetector(NatsStreamPropertie return new NatsStreamListenerDetector(properties, natsStreamCustomizerObjectProvider, natsConnection, natsJetStream); } + @Bean + public NatsStreamTemplate natsStreamTemplate(JetStream natsJetStream) { + return new DefaultNatsStreamTemplate(natsJetStream); + } + }