From c00328cf5e3509d0f0fd3837166c6671c505ff1b Mon Sep 17 00:00:00 2001 From: huang-juanjuan <482999194@qq.com> Date: Tue, 30 Dec 2025 03:24:23 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=9B=B4=E6=96=B0UI=E7=95=8C=E9=9D=A2?= =?UTF-8?q?=EF=BC=8C=E6=96=B0=E5=A2=9E=E7=82=B9=E5=87=BB=E8=8A=82=E7=82=B9?= =?UTF-8?q?=E5=90=8E=E6=98=BE=E7=A4=BA=E5=88=86=E5=8C=BA=EF=BC=88=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E8=87=AA=E5=AE=9A=E4=B9=89=E6=B5=8B=E8=AF=95=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/static/index.html | 20 ++- .../main/resources/static/node-detail.html | 145 ++++++++++++++++++ 2 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 dkv-master/src/main/resources/static/node-detail.html diff --git a/dkv-master/src/main/resources/static/index.html b/dkv-master/src/main/resources/static/index.html index 50160ce..a4ee327 100644 --- a/dkv-master/src/main/resources/static/index.html +++ b/dkv-master/src/main/resources/static/index.html @@ -162,9 +162,27 @@
🔍 路由模拟追踪
}); } + // 添加跳转事件 function updateNodeList(nodes) { const container = document.getElementById('nodes-container'); - container.innerHTML = nodes.map(ip => `🖥️ ${ip}`).join('') || '集群无可用节点'; + + if (!nodes || nodes.length === 0) { + container.innerHTML = '集群无可用节点'; + return; + } + + container.innerHTML = nodes.map(ip => ` + + 🖥️ ${ip} + + `).join(''); + } + + // 跳转函数 + function goToNodeDetail(node) { + window.location.href = `/node-detail.html?node=${encodeURIComponent(node)}`; } function updateChart(nodes) { diff --git a/dkv-master/src/main/resources/static/node-detail.html b/dkv-master/src/main/resources/static/node-detail.html new file mode 100644 index 0000000..d6d67de --- /dev/null +++ b/dkv-master/src/main/resources/static/node-detail.html @@ -0,0 +1,145 @@ + + + + + 节点详情 + + + + + + + +

节点详情

+
+ + +
当前分区节点
+
+ + + + + +
+ + + + + From d81f9d81292d7a112e781c27f5d4c4ef17dab8cb Mon Sep 17 00:00:00 2001 From: huang-juanjuan <482999194@qq.com> Date: Tue, 30 Dec 2025 14:31:25 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9save=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=EF=BC=8C=E6=94=B9=E4=B8=BA=E4=BB=8Emaster=E8=8E=B7=E5=8F=96tar?= =?UTF-8?q?getIP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/dkv/dkvclient/client/DkvClient.java | 58 +++++++------------ .../controller/ClientController.java | 4 +- 2 files changed, 23 insertions(+), 39 deletions(-) diff --git a/dkv-client/src/main/java/com/dkv/dkvclient/client/DkvClient.java b/dkv-client/src/main/java/com/dkv/dkvclient/client/DkvClient.java index 94ec6e1..927f128 100644 --- a/dkv-client/src/main/java/com/dkv/dkvclient/client/DkvClient.java +++ b/dkv-client/src/main/java/com/dkv/dkvclient/client/DkvClient.java @@ -13,7 +13,8 @@ import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.retry.ExponentialBackoffRetry; import org.apache.zookeeper.Watcher; - +import org.springframework.web.client.RestTemplate; +import java.util.Map; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CompletableFuture; @@ -64,22 +65,28 @@ private void updateNodes() throws Exception { System.out.println("[DkvClient] Current nodes: " + nodes); } - /** 简单一致性哈希计算目标节点 */ + /** 计算目标节点 */ private String getTargetIp(String key) { - synchronized (nodes) { - if (nodes.isEmpty()) { - throw new RuntimeException("No available nodes in ZooKeeper!"); - } - int hash = Math.abs(key.hashCode()); - int idx = hash % nodes.size(); - return nodes.get(idx); - } + return "127.0.0.1:9001"; +// String url = "http://localhost:8081/api/route?key=" + key; +// +// RestTemplate restTemplate = new RestTemplate(); +// Map response = restTemplate.getForObject(url, Map.class); +// +// if (response == null || !response.containsKey("primary")) { +// return null; +// } +// +// return (String) response.get("primary"); } + /** PUT 操作 */ - public void put(String key, byte[] value) throws InterruptedException { + public String put(String key, byte[] value) throws InterruptedException { KvMessage message = new KvMessage(KvMessage.Type.PUT, key, value); - sendRequest(getTargetIp(key), message); + String primaryNode = getTargetIp(key); + sendRequest(primaryNode, message); + return primaryNode; } /** GET 操作 */ @@ -117,31 +124,8 @@ protected void initChannel(Channel ch) { ); } }); - ChannelFuture future = b.connect(host, port).sync(); - CompletableFuture responseFuture = new CompletableFuture<>(); - -// 添加 handler 时,把 responseFuture 传入 handler - future.channel().pipeline().addLast(new KvClientHandler(responseFuture)); - -// 发送请求 - future.channel().writeAndFlush(request).sync(); - System.out.println("Request sent, waiting for response..."); - - try { - // 等待响应,最多 5 秒 - KvMessage response = responseFuture.get(1, TimeUnit.SECONDS); - System.out.println("Got response: " + response); - } catch (TimeoutException e) { - System.out.println("Response timed out, closing channel..."); - } catch (Exception e) { - e.printStackTrace(); - } finally { - // 超时或正常都要安全关闭 channel - future.channel().close().sync(); - future.channel().closeFuture().sync(); - } - - + Channel ch = b.connect(host, port).sync().channel(); + ch.writeAndFlush(request).sync(); return handler.getResponse(); } catch (Exception e) { throw new RuntimeException(e); diff --git a/dkv-client/src/main/java/com/dkv/dkvclient/controller/ClientController.java b/dkv-client/src/main/java/com/dkv/dkvclient/controller/ClientController.java index b269e16..4348cbc 100644 --- a/dkv-client/src/main/java/com/dkv/dkvclient/controller/ClientController.java +++ b/dkv-client/src/main/java/com/dkv/dkvclient/controller/ClientController.java @@ -29,8 +29,8 @@ public void init() { @PostMapping("/save") public String save(@RequestParam String key, @RequestParam String value) { try { - client.put(key, value.getBytes()); - return "Saved key: " + key + ", value: " + value; + String savedNode = client.put(key, value.getBytes()); + return "Saved key: " + key + " to Node: " + savedNode + ", value: " + value; } catch (Exception e) { e.printStackTrace(); return "Failed to save key: " + key + ", error: " + e.getMessage();