删除多余的依赖引入,解决netty关于热更新端口占用的问题。

This commit is contained in:
dftre 2024-10-27 16:02:51 +08:00
parent 4a4c23f310
commit a9c91babab
4 changed files with 39 additions and 12 deletions

View File

@ -56,12 +56,6 @@
</exclusion> </exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
<!-- Mysql驱动包 -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
<!-- 获取系统信息 --> <!-- 获取系统信息 -->
<dependency> <dependency>

View File

@ -1,7 +1,9 @@
package com.ruoyi.framework.datasource; package com.ruoyi.framework.datasource;
import java.util.Map; import java.util.Map;
import javax.sql.DataSource; import javax.sql.DataSource;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
/** /**

View File

@ -7,6 +7,8 @@ import org.springframework.stereotype.Component;
import com.ruoyi.netty.websocket.nettyServer.NettyWebSocketServer; import com.ruoyi.netty.websocket.nettyServer.NettyWebSocketServer;
import jakarta.annotation.PreDestroy;
@Component @Component
public class NettyServerRunner implements ApplicationRunner { public class NettyServerRunner implements ApplicationRunner {
@ -18,4 +20,9 @@ public class NettyServerRunner implements ApplicationRunner {
server.start(); server.start();
} }
@PreDestroy
public void destroy() {
server.shutdown();
}
} }

View File

@ -1,14 +1,19 @@
package com.ruoyi.netty.websocket.nettyServer; package com.ruoyi.netty.websocket.nettyServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import com.ruoyi.netty.websocket.nettyServer.handler.WebSocketHandler; import com.ruoyi.netty.websocket.nettyServer.handler.WebSocketHandler;
import io.netty.bootstrap.ServerBootstrap; import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption; import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline; import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.channel.socket.nio.NioSocketChannel;
@ -19,7 +24,9 @@ import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
@Component @Component
public class NettyWebSocketServer { public class NettyWebSocketServer {
private static ServerBootstrap serverBootstrap; private static final Logger log = LoggerFactory.getLogger(NettyWebSocketServer.class);
private ServerBootstrap serverBootstrap;
private Channel serverChannel;
@Value("${netty.websocket.maxMessageSize}") @Value("${netty.websocket.maxMessageSize}")
private Long messageSize; private Long messageSize;
@ -56,10 +63,27 @@ public class NettyWebSocketServer {
pipeline.addLast(new WebSocketServerProtocolHandler("/", true)); pipeline.addLast(new WebSocketServerProtocolHandler("/", true));
} }
}); });
serverBootstrap.bind(port.intValue()).sync(); ChannelFuture future = serverBootstrap.bind(port.intValue()).sync();
System.out.println( serverChannel = future.channel();
"----------------------------------------------------------------------------------- \n Arknights!"); log.info("netty for websocket start success, running in port: {}", this.port);
NettyWebSocketServer.serverBootstrap = serverBootstrap; this.serverBootstrap = serverBootstrap;
return NettyWebSocketServer.serverBootstrap; return this.serverBootstrap;
}
public void shutdown() {
if (serverChannel != null) {
serverChannel.close().syncUninterruptibly();
}
if (serverBootstrap != null) {
EventLoopGroup bossGroup = serverBootstrap.config().group();
EventLoopGroup workerGroup = serverBootstrap.config().childGroup();
if (bossGroup != null) {
bossGroup.shutdownGracefully().syncUninterruptibly();
}
if (workerGroup != null) {
workerGroup.shutdownGracefully().syncUninterruptibly();
}
}
log.info("netty for websocket shudown success");
} }
} }