From 6d2aeb4621d215529d903d1b88b88c74edbf3746 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=A6=E8=8B=B1=E6=9D=B0?= <327782001@qq.com> Date: Sun, 27 Aug 2023 18:21:00 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E6=B7=BB=E5=8A=A0=E9=9B=AA=E8=8A=B1?= =?UTF-8?q?=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- READ.md | 6 +- pom.xml | 5 ++ .../config/WebSocketConfig.java | 3 +- .../springbootkwan/utils/SnowflakeUtil.java | 54 ++++++++++++++ src/main/resources/templates/socket1.html | 74 ------------------- .../kwan/springbootkwan/SensitiveTest.java | 2 +- .../springbootkwan/UserServiceImplTest.java | 2 +- .../controller/ChatbotControllerTest.java | 2 +- .../springbootkwan/utils/PinyinUtilTest.java | 2 +- .../utils/SnowflakeUtilTest.java | 21 ++++++ .../utils/StringEncryptorUtil.java | 2 +- 11 files changed, 89 insertions(+), 84 deletions(-) create mode 100644 src/main/java/com/kwan/springbootkwan/utils/SnowflakeUtil.java delete mode 100644 src/main/resources/templates/socket1.html create mode 100644 src/test/java/com/kwan/springbootkwan/utils/SnowflakeUtilTest.java diff --git a/READ.md b/READ.md index 3dee398..76aa7a4 100644 --- a/READ.md +++ b/READ.md @@ -18,7 +18,7 @@ - devtools热部署 -- mapstruct dto-vo转换 +- mapstruct dto-vo转换 - test测试模块 @@ -35,7 +35,6 @@ - 批处理 - redis 全局唯一id - ## 三.链接 @@ -47,5 +46,4 @@ http://localhost:8761/user/all http://localhost:8761/swagger-ui.html - - +index.html 这个页面是进行websocket连接的 diff --git a/pom.xml b/pom.xml index 17c3b12..8313aac 100644 --- a/pom.xml +++ b/pom.xml @@ -216,6 +216,11 @@ jasypt-spring-boot-starter 2.1.0 + + javax.websocket + javax.websocket-api + 1.1 + org.springframework.boot spring-boot-starter-websocket diff --git a/src/main/java/com/kwan/springbootkwan/config/WebSocketConfig.java b/src/main/java/com/kwan/springbootkwan/config/WebSocketConfig.java index 7980fcf..ea942b2 100644 --- a/src/main/java/com/kwan/springbootkwan/config/WebSocketConfig.java +++ b/src/main/java/com/kwan/springbootkwan/config/WebSocketConfig.java @@ -2,6 +2,7 @@ package com.kwan.springbootkwan.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.web.socket.config.annotation.EnableWebSocket; import org.springframework.web.socket.server.standard.ServerEndpointExporter; /** @@ -11,7 +12,7 @@ import org.springframework.web.socket.server.standard.ServerEndpointExporter; * @version : 2.2.0 * @date : 2023/8/26 16:45 */ - +@EnableWebSocket @Configuration public class WebSocketConfig { @Bean diff --git a/src/main/java/com/kwan/springbootkwan/utils/SnowflakeUtil.java b/src/main/java/com/kwan/springbootkwan/utils/SnowflakeUtil.java new file mode 100644 index 0000000..c75992f --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/utils/SnowflakeUtil.java @@ -0,0 +1,54 @@ +package com.kwan.springbootkwan.utils; + +public class SnowflakeUtil { + private static final long EPOCH = 1630444800000L; // 设置一个起始时间戳,例如:2021-09-01 00:00:00 + private static final long MACHINE_ID_BITS = 5; + private static final long DATACENTER_ID_BITS = 5; + private static final long SEQUENCE_BITS = 12; + private static final long MAX_MACHINE_ID = (1L << MACHINE_ID_BITS) - 1; + private static final long MAX_DATACENTER_ID = (1L << DATACENTER_ID_BITS) - 1; + private static final long MAX_SEQUENCE = (1L << SEQUENCE_BITS) - 1; + private final long machineId; + private final long datacenterId; + private long lastTimestamp = -1L; + private long sequence = 0L; + + public SnowflakeUtil(long machineId, long datacenterId) { + if (machineId > MAX_MACHINE_ID || machineId < 0) { + throw new IllegalArgumentException("Invalid machine ID"); + } + if (datacenterId > MAX_DATACENTER_ID || datacenterId < 0) { + throw new IllegalArgumentException("Invalid datacenter ID"); + } + this.machineId = machineId; + this.datacenterId = datacenterId; + } + + public synchronized long nextId() { + long timestamp = System.currentTimeMillis(); + if (timestamp < lastTimestamp) { + throw new RuntimeException("Clock moved backwards. Refusing to generate ID."); + } + if (timestamp == lastTimestamp) { + sequence = (sequence + 1) & MAX_SEQUENCE; + if (sequence == 0) { + timestamp = tilNextMillis(lastTimestamp); + } + } else { + sequence = 0; + } + lastTimestamp = timestamp; + return ((timestamp - EPOCH) << (MACHINE_ID_BITS + DATACENTER_ID_BITS + SEQUENCE_BITS)) + | (datacenterId << (MACHINE_ID_BITS + SEQUENCE_BITS)) + | (machineId << SEQUENCE_BITS) + | sequence; + } + + private long tilNextMillis(long lastTimestamp) { + long timestamp = System.currentTimeMillis(); + while (timestamp <= lastTimestamp) { + timestamp = System.currentTimeMillis(); + } + return timestamp; + } +} \ No newline at end of file diff --git a/src/main/resources/templates/socket1.html b/src/main/resources/templates/socket1.html deleted file mode 100644 index 20f4396..0000000 --- a/src/main/resources/templates/socket1.html +++ /dev/null @@ -1,74 +0,0 @@ - - - - - - Java后端WebSocket的Tomcat实现 - - - - -
-Welcome
- -
- -
-
- - - - \ No newline at end of file diff --git a/src/test/java/com/kwan/springbootkwan/SensitiveTest.java b/src/test/java/com/kwan/springbootkwan/SensitiveTest.java index 3b7b723..4de09fd 100644 --- a/src/test/java/com/kwan/springbootkwan/SensitiveTest.java +++ b/src/test/java/com/kwan/springbootkwan/SensitiveTest.java @@ -9,7 +9,7 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) -@SpringBootTest +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @ContextConfiguration(classes = SpringBootKwanApplication.class) public class SensitiveTest { diff --git a/src/test/java/com/kwan/springbootkwan/UserServiceImplTest.java b/src/test/java/com/kwan/springbootkwan/UserServiceImplTest.java index c202ae8..a8e920f 100644 --- a/src/test/java/com/kwan/springbootkwan/UserServiceImplTest.java +++ b/src/test/java/com/kwan/springbootkwan/UserServiceImplTest.java @@ -17,7 +17,7 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -@SpringBootTest +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class UserServiceImplTest { @Autowired diff --git a/src/test/java/com/kwan/springbootkwan/controller/ChatbotControllerTest.java b/src/test/java/com/kwan/springbootkwan/controller/ChatbotControllerTest.java index f30d0f1..3481f72 100644 --- a/src/test/java/com/kwan/springbootkwan/controller/ChatbotControllerTest.java +++ b/src/test/java/com/kwan/springbootkwan/controller/ChatbotControllerTest.java @@ -9,7 +9,7 @@ import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) -@SpringBootTest +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @ContextConfiguration(classes = SpringBootKwanApplication.class) class ChatbotControllerTest { diff --git a/src/test/java/com/kwan/springbootkwan/utils/PinyinUtilTest.java b/src/test/java/com/kwan/springbootkwan/utils/PinyinUtilTest.java index 4526860..df709a0 100644 --- a/src/test/java/com/kwan/springbootkwan/utils/PinyinUtilTest.java +++ b/src/test/java/com/kwan/springbootkwan/utils/PinyinUtilTest.java @@ -8,7 +8,7 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) -@SpringBootTest +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @ContextConfiguration(classes = SpringBootKwanApplication.class) public class PinyinUtilTest { diff --git a/src/test/java/com/kwan/springbootkwan/utils/SnowflakeUtilTest.java b/src/test/java/com/kwan/springbootkwan/utils/SnowflakeUtilTest.java new file mode 100644 index 0000000..42b87c1 --- /dev/null +++ b/src/test/java/com/kwan/springbootkwan/utils/SnowflakeUtilTest.java @@ -0,0 +1,21 @@ +package com.kwan.springbootkwan.utils; + +import com.kwan.springbootkwan.SpringBootKwanApplication; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@ContextConfiguration(classes = SpringBootKwanApplication.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +public class SnowflakeUtilTest { + @Test + public void testName() { + // 传入机器ID和数据中心ID + SnowflakeUtil snowflake = new SnowflakeUtil(1, 1); + long id = snowflake.nextId(); + System.out.println("Generated ID: " + id); + } +} \ No newline at end of file diff --git a/src/test/java/com/kwan/springbootkwan/utils/StringEncryptorUtil.java b/src/test/java/com/kwan/springbootkwan/utils/StringEncryptorUtil.java index 7535666..37fd285 100644 --- a/src/test/java/com/kwan/springbootkwan/utils/StringEncryptorUtil.java +++ b/src/test/java/com/kwan/springbootkwan/utils/StringEncryptorUtil.java @@ -19,7 +19,7 @@ import org.springframework.test.context.junit4.SpringRunner; * @date : 2023/8/26 15:49 */ @RunWith(SpringRunner.class) -@SpringBootTest +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @ContextConfiguration(classes = SpringBootKwanApplication.class) public class StringEncryptorUtil { -- GitLab