fix:添加雪花算法

上级 39954353
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
- devtools热部署 - devtools热部署
- mapstruct dto-vo转换 - mapstruct dto-vo转换
- test测试模块 - test测试模块
...@@ -35,7 +35,6 @@ ...@@ -35,7 +35,6 @@
- 批处理 - 批处理
- redis 全局唯一id - redis 全局唯一id
## 三.链接 ## 三.链接
...@@ -47,5 +46,4 @@ http://localhost:8761/user/all ...@@ -47,5 +46,4 @@ http://localhost:8761/user/all
http://localhost:8761/swagger-ui.html http://localhost:8761/swagger-ui.html
index.html 这个页面是进行websocket连接的
...@@ -216,6 +216,11 @@ ...@@ -216,6 +216,11 @@
<artifactId>jasypt-spring-boot-starter</artifactId> <artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.0</version> <version>2.1.0</version>
</dependency> </dependency>
<dependency>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-api</artifactId>
<version>1.1</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId> <artifactId>spring-boot-starter-websocket</artifactId>
......
...@@ -2,6 +2,7 @@ package com.kwan.springbootkwan.config; ...@@ -2,6 +2,7 @@ package com.kwan.springbootkwan.config;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.server.standard.ServerEndpointExporter; import org.springframework.web.socket.server.standard.ServerEndpointExporter;
/** /**
...@@ -11,7 +12,7 @@ import org.springframework.web.socket.server.standard.ServerEndpointExporter; ...@@ -11,7 +12,7 @@ import org.springframework.web.socket.server.standard.ServerEndpointExporter;
* @version : 2.2.0 * @version : 2.2.0
* @date : 2023/8/26 16:45 * @date : 2023/8/26 16:45
*/ */
@EnableWebSocket
@Configuration @Configuration
public class WebSocketConfig { public class WebSocketConfig {
@Bean @Bean
......
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Java后端WebSocket的Tomcat实现</title>
<script type="text/javascript" src="js/jquery.min.js"></script>
</head>
<body>
<div id="main" style="width: 1200px;height:800px;"></div>
Welcome<br/><input id="text" type="text"/>
<button onclick="send()">发送消息</button>
<hr/>
<button onclick="closeWebSocket()">关闭WebSocket连接</button>
<hr/>
<div id="message"></div>
</body>
<script type="text/javascript">
var websocket = null;
//判断当前浏览器是否支持WebSocket
if ('WebSocket' in window) {
//改成你的地址
websocket = new WebSocket("ws://localhost:8888/api/websocket/100");
} else {
alert('当前浏览器 Not support websocket')
}
//连接发生错误的回调方法
websocket.onerror = function () {
setMessageInnerHTML("WebSocket连接发生错误");
};
//连接成功建立的回调方法
websocket.onopen = function () {
setMessageInnerHTML("WebSocket连接成功");
}
var U01data, Uidata, Usdata
//接收到消息的回调方法
websocket.onmessage = function (event) {
console.log(event);
setMessageInnerHTML(event);
setechart()
}
//连接关闭的回调方法
websocket.onclose = function () {
setMessageInnerHTML("WebSocket连接关闭");
}
//监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。
window.onbeforeunload = function () {
closeWebSocket();
}
//将消息显示在网页上
function setMessageInnerHTML(innerHTML) {
document.getElementById('message').innerHTML += innerHTML + '<br/>';
}
//关闭WebSocket连接
function closeWebSocket() {
websocket.close();
}
//发送消息
function send() {
var message = document.getElementById('text').value;
websocket.send('{"msg":"' + message + '"}');
setMessageInnerHTML(message + "&#13;");
}
</script>
</html>
\ No newline at end of file
...@@ -9,7 +9,7 @@ import org.springframework.test.context.ContextConfiguration; ...@@ -9,7 +9,7 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration(classes = SpringBootKwanApplication.class) @ContextConfiguration(classes = SpringBootKwanApplication.class)
public class SensitiveTest { public class SensitiveTest {
......
...@@ -17,7 +17,7 @@ import java.util.concurrent.ExecutorService; ...@@ -17,7 +17,7 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@SpringBootTest @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class UserServiceImplTest { public class UserServiceImplTest {
@Autowired @Autowired
......
...@@ -9,7 +9,7 @@ import org.springframework.test.context.junit4.SpringRunner; ...@@ -9,7 +9,7 @@ import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration(classes = SpringBootKwanApplication.class) @ContextConfiguration(classes = SpringBootKwanApplication.class)
class ChatbotControllerTest { class ChatbotControllerTest {
......
...@@ -8,7 +8,7 @@ import org.springframework.test.context.ContextConfiguration; ...@@ -8,7 +8,7 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration(classes = SpringBootKwanApplication.class) @ContextConfiguration(classes = SpringBootKwanApplication.class)
public class PinyinUtilTest { public class PinyinUtilTest {
......
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
...@@ -19,7 +19,7 @@ import org.springframework.test.context.junit4.SpringRunner; ...@@ -19,7 +19,7 @@ import org.springframework.test.context.junit4.SpringRunner;
* @date : 2023/8/26 15:49 * @date : 2023/8/26 15:49
*/ */
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration(classes = SpringBootKwanApplication.class) @ContextConfiguration(classes = SpringBootKwanApplication.class)
public class StringEncryptorUtil { public class StringEncryptorUtil {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册