diff --git a/READ.md b/READ.md index 3dee3982d3cc49b0d4478e76336ca5536d7602fa..76aa7a4f49b318f983005ac6d4c8b9832176906d 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 17c3b126f3659fd3b2b6d771b12eacd31509915c..8313aacc829f1bc40c5be228774269b7f00a56ad 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 7980fcfef0cdada7832fd40291ffcbae0abd8be2..ea942b2d096e8020fb6ee25e2304636e0138a35b 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 0000000000000000000000000000000000000000..c75992f71a16a21738144f45f58e66df309944a5 --- /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 20f4396a4604f577c28698680d39f331cbc93af9..0000000000000000000000000000000000000000 --- 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 3b7b72374326737ff871521c55e4295ab891d4ca..4de09fdb63b29993731db64a7025abe93f2adaff 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 c202ae88f3d2ab789e269ced742eaaaebc1b1c8a..a8e920f6d219145008e00ed78333405ac40cb442 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 f30d0f18203b0514b8804a639ae0cdfb2230b777..3481f72bfd833b57aaa3ae6c7892ca99f83de263 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 45268601323e73d6feda8abb63f1dcea30029dae..df709a0dc94e720c2923c0e3d4f2e00c52ac6128 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 0000000000000000000000000000000000000000..42b87c1380542de5e7dd893dd24a0b47dc334393 --- /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 75356669c3a6bfb569a18c1da0eafc8c95e054f5..37fd285b67ba26c454bd8bb6ede3daf1f581fcf5 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 {