From f985cdac8f1b25f1f426f5e50f4c62a37cd1d275 Mon Sep 17 00:00:00 2001 From: wujiawei <12345678> Date: Thu, 9 Jan 2025 17:26:12 +0800 Subject: [PATCH] =?UTF-8?q?[fix]=20=20=E4=BF=AE=E5=A4=8D=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=AD=98=E4=B9=8E=E5=AF=BC=E8=87=B4=E4=B8=BB=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NettyClientSocketApplicationListener.java | 65 ++++++++++--------- .../src/test/java/NettyMulticastExample.java | 55 ++++++++++++++++ 2 files changed, 90 insertions(+), 30 deletions(-) create mode 100644 wu-lazy-cloud-heartbeat-start/wu-lazy-cloud-heartbeat-server-start/src/test/java/NettyMulticastExample.java diff --git a/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/context/NettyClientSocketApplicationListener.java b/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/context/NettyClientSocketApplicationListener.java index b289193..e2d506f 100644 --- a/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/context/NettyClientSocketApplicationListener.java +++ b/wu-lazy-cloud-heartbeat-client/src/main/java/org/framework/lazy/cloud/network/heartbeat/client/context/NettyClientSocketApplicationListener.java @@ -37,37 +37,42 @@ public class NettyClientSocketApplicationListener implements ApplicationListener */ public void initDb2Config() { - String clientId = nettyClientProperties.getClientId(); - String inetHost = nettyClientProperties.getInetHost(); - int inetPort = nettyClientProperties.getInetPort(); - String appKey = nettyClientProperties.getAppKey(); - String appSecret = nettyClientProperties.getAppSecret(); - ProtocolType protocolType = nettyClientProperties.getProtocolType(); - if (Objects.isNull(clientId) || - Objects.isNull(inetHost)) { - log.warn("配置信息为空,请通过页面添加配置信息:{}", nettyClientProperties); - return; - } - LazyNettyServerPropertiesDO lazyNettyServerPropertiesDO = new LazyNettyServerPropertiesDO(); - lazyNettyServerPropertiesDO.setClientId(clientId); - lazyNettyServerPropertiesDO.setInetHost(inetHost); - lazyNettyServerPropertiesDO.setInetPort(inetPort); - lazyNettyServerPropertiesDO.setType(PropertiesType.CONFIG); - lazyNettyServerPropertiesDO.setIsDeleted(false); - lazyNettyServerPropertiesDO.setAppKey(appKey); - lazyNettyServerPropertiesDO.setAppSecret(appSecret); - lazyNettyServerPropertiesDO.setProtocolType(protocolType); + try { + String clientId = nettyClientProperties.getClientId(); + String inetHost = nettyClientProperties.getInetHost(); + int inetPort = nettyClientProperties.getInetPort(); + String appKey = nettyClientProperties.getAppKey(); + String appSecret = nettyClientProperties.getAppSecret(); + ProtocolType protocolType = nettyClientProperties.getProtocolType(); + if (Objects.isNull(clientId) || + Objects.isNull(inetHost)) { + log.warn("配置信息为空,请通过页面添加配置信息:{}", nettyClientProperties); + return; + } + LazyNettyServerPropertiesDO lazyNettyServerPropertiesDO = new LazyNettyServerPropertiesDO(); + lazyNettyServerPropertiesDO.setClientId(clientId); + lazyNettyServerPropertiesDO.setInetHost(inetHost); + lazyNettyServerPropertiesDO.setInetPort(inetPort); + lazyNettyServerPropertiesDO.setType(PropertiesType.CONFIG); + lazyNettyServerPropertiesDO.setIsDeleted(false); + lazyNettyServerPropertiesDO.setAppKey(appKey); + lazyNettyServerPropertiesDO.setAppSecret(appSecret); + lazyNettyServerPropertiesDO.setProtocolType(protocolType); - // 根据服务端端口、port 唯一性验证 - boolean exists = lazyLambdaStream.exists(LazyWrappers.lambdaWrapper() - .eq(LazyNettyServerPropertiesDO::getInetHost, inetHost) - .eq(LazyNettyServerPropertiesDO::getInetPort, inetPort) - .eq(LazyNettyServerPropertiesDO::getClientId, clientId) - .eq(LazyNettyServerPropertiesDO::getProtocolType, protocolType) - ); - if (!exists) { - lazyLambdaStream.insert(lazyNettyServerPropertiesDO); - } + + // 根据服务端端口、port 唯一性验证 + boolean exists = lazyLambdaStream.exists(LazyWrappers.lambdaWrapper() + .eq(LazyNettyServerPropertiesDO::getInetHost, inetHost) + .eq(LazyNettyServerPropertiesDO::getInetPort, inetPort) + .eq(LazyNettyServerPropertiesDO::getClientId, clientId) + .eq(LazyNettyServerPropertiesDO::getProtocolType, protocolType) + ); + if (!exists) { + lazyLambdaStream.insert(lazyNettyServerPropertiesDO); + } + }catch (Exception e){ + e.printStackTrace(); + } } diff --git a/wu-lazy-cloud-heartbeat-start/wu-lazy-cloud-heartbeat-server-start/src/test/java/NettyMulticastExample.java b/wu-lazy-cloud-heartbeat-start/wu-lazy-cloud-heartbeat-server-start/src/test/java/NettyMulticastExample.java new file mode 100644 index 0000000..074e831 --- /dev/null +++ b/wu-lazy-cloud-heartbeat-start/wu-lazy-cloud-heartbeat-server-start/src/test/java/NettyMulticastExample.java @@ -0,0 +1,55 @@ +//import io.netty.bootstrap.Bootstrap; +//import io.netty.channel.Channel; +//import io.netty.channel.ChannelOption; +//import io.netty.channel.EventLoopGroup; +//import io.netty.channel.nio.NioEventLoopGroup; +//import io.netty.channel.socket.DatagramPacket; +//import io.netty.channel.socket.nio.NioDatagramChannel; +//import io.netty.channel.socket.nio.NioMulticastChannel; +// +//import java.net.InetSocketAddress; +// +//public class NettyMulticastExample { +// +// public static void main(String[] args) throws Exception { +// EventLoopGroup group = new NioEventLoopGroup(); +// try { +// Bootstrap b = new Bootstrap(); +// b.group(group) +// .channel(NioMulticastChannel.class) +// .option(ChannelOption.SO_BROADCAST, true) +// .handler(new MulticastChannelHandler()); +// +// // 替换为你的虚拟IP和端口 +// String multicastAddress = "230.0.0.1"; +// int port = 30000; +// Channel ch = b.bind(new InetSocketAddress(multicastAddress, port)).sync().channel(); +// +// // 发送数据 +// ch.writeAndFlush(new DatagramPacket( +// Unpooled.copiedBuffer("Hello, world!", CharsetUtil.UTF_8), +// new InetSocketAddress(multicastAddress, port))); +// +// // 等待输入以关闭服务 +// System.in.read(); +// } finally { +// group.shutdownGracefully().sync(); +// } +// } +// +// private static class MulticastChannelHandler extends ChannelInboundHandlerAdapter { +// @Override +// public void channelRead(ChannelHandlerContext ctx, Object msg) { +// DatagramPacket packet = (DatagramPacket) msg; +// ByteBuf data = packet.content(); +// System.out.println("Received message: " + data.toString(CharsetUtil.UTF_8)); +// data.release(); +// } +// +// @Override +// public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { +// cause.printStackTrace(); +// ctx.close(); +// } +// } +//} \ No newline at end of file