优化
This commit is contained in:
@@ -119,5 +119,10 @@
|
|||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>spring-test</artifactId>
|
<artifactId>spring-test</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.netty</groupId>
|
||||||
|
<artifactId>netty-all</artifactId>
|
||||||
|
<version>4.1.100.Final</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
@@ -21,5 +21,11 @@
|
|||||||
<groupId>top.wms</groupId>
|
<groupId>top.wms</groupId>
|
||||||
<artifactId>wms-common</artifactId>
|
<artifactId>wms-common</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.annotation</groupId>
|
||||||
|
<artifactId>javax.annotation-api</artifactId>
|
||||||
|
<version>1.3.2</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
@@ -33,7 +33,6 @@ public class NettyTcpServer {
|
|||||||
private EventLoopGroup workerGroup;
|
private EventLoopGroup workerGroup;
|
||||||
private Channel serverChannel;
|
private Channel serverChannel;
|
||||||
|
|
||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void start() throws InterruptedException {
|
public void start() throws InterruptedException {
|
||||||
log.info("正在启动TCP服务端,端口: {}", port);
|
log.info("正在启动TCP服务端,端口: {}", port);
|
||||||
@@ -43,27 +42,27 @@ public class NettyTcpServer {
|
|||||||
|
|
||||||
ServerBootstrap bootstrap = new ServerBootstrap();
|
ServerBootstrap bootstrap = new ServerBootstrap();
|
||||||
bootstrap.group(bossGroup, workerGroup)
|
bootstrap.group(bossGroup, workerGroup)
|
||||||
.channel(NioServerSocketChannel.class)
|
.channel(NioServerSocketChannel.class)
|
||||||
.option(ChannelOption.SO_BACKLOG, 128)
|
.option(ChannelOption.SO_BACKLOG, 128)
|
||||||
.childOption(ChannelOption.SO_KEEPALIVE, true)
|
.childOption(ChannelOption.SO_KEEPALIVE, true)
|
||||||
.childHandler(new ChannelInitializer<SocketChannel>() {
|
.childHandler(new ChannelInitializer<SocketChannel>() {
|
||||||
@Override
|
@Override
|
||||||
protected void initChannel(SocketChannel ch) {
|
protected void initChannel(SocketChannel ch) {
|
||||||
ChannelPipeline pipeline = ch.pipeline();
|
ChannelPipeline pipeline = ch.pipeline();
|
||||||
|
|
||||||
// 解决TCP粘包问题
|
// 解决TCP粘包问题
|
||||||
pipeline.addLast(new LineBasedFrameDecoder(1024));
|
pipeline.addLast(new LineBasedFrameDecoder(1024));
|
||||||
|
|
||||||
// 字符串编解码
|
// 字符串编解码
|
||||||
pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
|
pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
|
||||||
pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));
|
pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));
|
||||||
|
|
||||||
// 关键修复:直接new,不要从Spring容器获取
|
// 关键修复:直接new,不要从Spring容器获取
|
||||||
pipeline.addLast(new TcpServerHandler());
|
pipeline.addLast(new TcpServerHandler());
|
||||||
|
|
||||||
log.debug("新连接接入,处理器已添加");
|
log.debug("新连接接入,处理器已添加");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
ChannelFuture future = bootstrap.bind(port).sync();
|
ChannelFuture future = bootstrap.bind(port).sync();
|
||||||
if (future.isSuccess()) {
|
if (future.isSuccess()) {
|
||||||
|
|||||||
@@ -2,9 +2,6 @@ package top.wms.admin.controller.tcp.service;
|
|||||||
|
|
||||||
import io.netty.channel.Channel;
|
import io.netty.channel.Channel;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.scheduling.annotation.Scheduled;
|
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
|
||||||
import top.continew.starter.core.validation.CheckUtils;
|
|
||||||
import top.wms.admin.controller.tcp.manager.ChannelManager;
|
import top.wms.admin.controller.tcp.manager.ChannelManager;
|
||||||
import top.wms.admin.controller.tcp.model.VMResult;
|
import top.wms.admin.controller.tcp.model.VMResult;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@@ -51,10 +48,8 @@ public class CommandService {
|
|||||||
|
|
||||||
// 获取统计信息
|
// 获取统计信息
|
||||||
public String getStatistics() {
|
public String getStatistics() {
|
||||||
return String.format("成功: %d, 失败: %d, 总数: %d",
|
return String.format("成功: %d, 失败: %d, 总数: %d", successCount.get(), failCount.get(), successCount
|
||||||
successCount.get(),
|
.get() + failCount.get());
|
||||||
failCount.get(),
|
|
||||||
successCount.get() + failCount.get());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取最新结果
|
// 获取最新结果
|
||||||
@@ -62,11 +57,10 @@ public class CommandService {
|
|||||||
return resultQueue.peek();
|
return resultQueue.peek();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ChannelManager channelManager;
|
private ChannelManager channelManager;
|
||||||
|
|
||||||
// @Scheduled(cron = "*/3 * * * * ?")
|
// @Scheduled(cron = "*/3 * * * * ?")
|
||||||
public void sendAndWait() {
|
public void sendAndWait() {
|
||||||
// 1. 检查连接
|
// 1. 检查连接
|
||||||
log.info("查询时间========");
|
log.info("查询时间========");
|
||||||
|
|||||||
@@ -32,13 +32,13 @@ public class VmCommandController {
|
|||||||
log.info("发送指令: {}", sendMsg);
|
log.info("发送指令: {}", sendMsg);
|
||||||
// 3. 等待响应
|
// 3. 等待响应
|
||||||
String response = requestMatcher.waitForResponse(20);
|
String response = requestMatcher.waitForResponse(20);
|
||||||
CheckUtils.throwIf("TIMEOUT".equals(response),"响应超时,请重试");
|
CheckUtils.throwIf("TIMEOUT".equals(response), "响应超时,请重试");
|
||||||
if (StrUtil.equals(response, "success") || StrUtil.equals(response, "failed")) {
|
if (StrUtil.equals(response, "success") || StrUtil.equals(response, "failed")) {
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
if (StrUtil.equals(response, msg)) {
|
if (StrUtil.equals(response, msg)) {
|
||||||
response = "success";
|
response = "success";
|
||||||
}else{
|
} else {
|
||||||
response = "failed";
|
response = "failed";
|
||||||
}
|
}
|
||||||
// 4. 返回结果
|
// 4. 返回结果
|
||||||
|
|||||||
@@ -52,10 +52,9 @@ public class AHDZCConnect {
|
|||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void init() {
|
public void init() {
|
||||||
// 项目启动时初始化并启动服务
|
// 项目启动时初始化并启动服务
|
||||||
if (false) {
|
|
||||||
ScaleService();
|
ScaleService();
|
||||||
start();
|
start();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@PreDestroy
|
@PreDestroy
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ spring.datasource:
|
|||||||
# 请务必提前创建好名为 wms_admin 的数据库,如果使用其他数据库名请注意同步修改 DB_NAME 配置
|
# 请务必提前创建好名为 wms_admin 的数据库,如果使用其他数据库名请注意同步修改 DB_NAME 配置
|
||||||
url: jdbc:p6spy:mysql://127.0.0.1:3306/wms?serverTimezone=Asia/Shanghai&useSSL=true&useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&autoReconnect=true&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true
|
url: jdbc:p6spy:mysql://127.0.0.1:3306/wms?serverTimezone=Asia/Shanghai&useSSL=true&useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&autoReconnect=true&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true
|
||||||
username: root
|
username: root
|
||||||
password: test123$
|
password: root
|
||||||
# PostgreSQL 配置
|
# PostgreSQL 配置
|
||||||
# url: jdbc:p6spy:mysql://192.168.2.30:${DB_PORT:3306}/continew?serverTimezone=Asia/Shanghai&useSSL=true&useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&autoReconnect=true&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true
|
# url: jdbc:p6spy:mysql://192.168.2.30:${DB_PORT:3306}/continew?serverTimezone=Asia/Shanghai&useSSL=true&useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&autoReconnect=true&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true
|
||||||
# username: ${DB_USER:root}
|
# username: ${DB_USER:root}
|
||||||
|
|||||||
Reference in New Issue
Block a user