Skip to content

Commit

Permalink
Merge pull request #3241 from KouShenhai/dev
Browse files Browse the repository at this point in the history
feat: 优化代码【去掉随机id生成】
  • Loading branch information
KouShenhai authored Jan 1, 2025
2 parents 9339dc3 + d5df1fc commit 103adaa
Show file tree
Hide file tree
Showing 16 changed files with 96 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,22 @@

package org.laokou.common.core.utils;

import org.laokou.common.i18n.common.exception.SystemException;

import java.security.SecureRandom;

public final class RandomStringUtil {

private static final SecureRandom RANDOM = new SecureRandom();

public static String randomNumeric() {
return randomNumeric(6);
}

public static String randomNumeric(int length) {
if (length < 1) {
throw new SystemException("S_String_InvalidLength", "随机字符串长度不能小于1");
}
StringBuilder stringBuilder = new StringBuilder(length);
for (int i = 0; i < length; i++) {
stringBuilder.append(RANDOM.nextInt(10));
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ class RandomStringUtilTest {
@Test
void testRandomNumeric() {
Assertions.assertNotNull(RandomStringUtil.randomNumeric(6));
Assertions.assertNotNull(RandomStringUtil.randomNumeric());
// 验证长度正确
Assertions.assertEquals(6, RandomStringUtil.randomNumeric().length());
Assertions.assertEquals(7, RandomStringUtil.randomNumeric(7).length());
// 验证字符串仅包含数字
Assertions.assertTrue(RandomStringUtil.randomNumeric().matches("\\d+"), "生成的字符串应仅包含数字字符");
}

}

This file was deleted.

14 changes: 12 additions & 2 deletions laokou-common/laokou-common-idempotent/pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>laokou-common</artifactId>
Expand All @@ -14,6 +14,16 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</exclusion>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.laokou</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public MailServiceImpl(MailProperties mailProperties) {
public MailResult send(String mail) {
String subject = "验证码";
String name = "邮箱" + subject;
String captcha = RandomStringUtil.randomNumeric(6);
String captcha = RandomStringUtil.randomNumeric();
// 默认5分钟有效
Map<String, Object> param = Map.of("captcha", captcha, "minute", 5);
String content = TemplateUtil.getContent(CAPTCHA_TEMPLATE, param);
Expand Down
10 changes: 10 additions & 0 deletions laokou-common/laokou-common-nacos/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</exclusion>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
Expand Down
12 changes: 11 additions & 1 deletion laokou-common/laokou-common-redis/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*
*/
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.laokou</groupId>
Expand All @@ -30,6 +30,16 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</exclusion>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
12 changes: 11 additions & 1 deletion laokou-common/laokou-common-secret/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*
*/
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>laokou-common</artifactId>
Expand All @@ -39,6 +39,16 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</exclusion>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
Expand Down
14 changes: 12 additions & 2 deletions laokou-common/laokou-common-sensitive/pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>laokou-common</artifactId>
Expand All @@ -18,6 +18,16 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</exclusion>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public GYYSmsServiceImpl(SmsProperties smsProperties) {
@Override
public SmsResult send(String mobile) {
String name = "手机号验证码【国阳云】";
String captcha = RandomStringUtil.randomNumeric(6);
String captcha = RandomStringUtil.randomNumeric();
String templateId = smsProperties.getGyy().getTemplateId();
if (!TEMPLATES.containsKey(templateId)) {
return new SmsResult(name, SendStatus.FAIL.getCode(), "模板不存在", JacksonUtil.EMPTY_JSON, captcha);
Expand Down
10 changes: 10 additions & 0 deletions laokou-common/laokou-common-trace/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</exclusion>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.laokou.common.core.utils.UniqueIdGenerator;
import org.laokou.common.i18n.utils.JacksonUtil;
import org.laokou.common.core.utils.IdGenerator;
import org.laokou.common.core.utils.MapUtil;
import org.laokou.common.i18n.utils.JacksonUtil;
import org.laokou.common.i18n.utils.StringUtil;
import org.laokou.logstash.common.support.TraceLogStorage;
import org.laokou.logstash.gateway.database.dataobject.TraceLogIndex;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -70,7 +71,7 @@ private TraceLogIndex getTraceLogIndex(String str) {
String traceId = traceLogIndex.getTraceId();
String spanId = traceLogIndex.getSpanId();
if (isTraceLog(traceId, spanId)) {
traceLogIndex.setId(UniqueIdGenerator.generateId());
traceLogIndex.setId(String.valueOf(IdGenerator.defaultSnowflakeId()));
return traceLogIndex;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@
<groupId>org.laokou</groupId>
<artifactId>laokou-common-trace</artifactId>
</dependency>
<dependency>
<groupId>org.laokou</groupId>
<artifactId>laokou-common-rocketmq</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,23 @@

import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.laokou.common.core.annotation.EnableTaskExecutor;
import org.laokou.common.i18n.utils.SslUtil;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.util.StopWatch;

import java.net.InetAddress;

/**
* @author laokou
*/
@Slf4j
@EnableTaskExecutor
@EnableDiscoveryClient
@EnableConfigurationProperties
@EnableEncryptableProperties
Expand All @@ -49,6 +54,8 @@ public class LogtashApp {
/// ```
@SneakyThrows
public static void main(String[] args) {
StopWatch stopWatch = new StopWatch("Logstash应用程序");
stopWatch.start();
System.setProperty("address", String.format("%s:%s", InetAddress.getLocalHost().getHostAddress(), System.getProperty("server.port", "10003")));
// 配置关闭nacos日志,因为nacos的log4j2导致本项目的日志不输出的问题
System.setProperty("nacos.logging.default.config.enabled", "false");
Expand All @@ -57,6 +64,8 @@ public static void main(String[] args) {
// 忽略SSL认证
SslUtil.ignoreSSLTrust();
new SpringApplicationBuilder(LogtashApp.class).web(WebApplicationType.SERVLET).run(args);
stopWatch.stop();
log.info("{}", stopWatch.prettyPrint());
}
// @formatter:on

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ spring:
import:
- optional:nacos:application-common-kafka.yaml?refreshEnabled=true&group=${NACOS-GROUP}
- optional:nacos:application-common-elasticsearch.yaml?refreshEnabled=true&group=${NACOS-GROUP}
- optional:nacos:application-common-rocketmq?refreshEnabled=true&group=${NACOS-GROUP}
- optional:nacos:application-common.yaml?refreshEnabled=true&group=${NACOS-GROUP}
cloud:
# nacos
Expand Down

0 comments on commit 103adaa

Please sign in to comment.