将日志写入文件;升级依赖;将项目JDK设置为21;修改部分日志记录方式
This commit is contained in:
parent
dd0b7e608c
commit
95329a5603
@ -7,13 +7,12 @@
|
|||||||
<option value="$PROJECT_DIR$/pom.xml" />
|
<option value="$PROJECT_DIR$/pom.xml" />
|
||||||
</list>
|
</list>
|
||||||
</option>
|
</option>
|
||||||
<option name="workspaceImportForciblyTurnedOn" value="true" />
|
|
||||||
</component>
|
</component>
|
||||||
<component name="PWA">
|
<component name="PWA">
|
||||||
<option name="enabled" value="true" />
|
<option name="enabled" value="true" />
|
||||||
<option name="wasEnabledAtLeastOnce" value="true" />
|
<option name="wasEnabledAtLeastOnce" value="true" />
|
||||||
</component>
|
</component>
|
||||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
|
||||||
<output url="file://$PROJECT_DIR$/out" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
||||||
6
pom.xml
6
pom.xml
@ -50,13 +50,13 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>cn.hutool</groupId>
|
<groupId>cn.hutool</groupId>
|
||||||
<artifactId>hutool-all</artifactId>
|
<artifactId>hutool-all</artifactId>
|
||||||
<version>5.8.20</version>
|
<version>5.8.26</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.commons</groupId>
|
<groupId>org.apache.commons</groupId>
|
||||||
<artifactId>commons-compress</artifactId>
|
<artifactId>commons-compress</artifactId>
|
||||||
<version>1.21</version>
|
<version>1.25.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
@ -96,6 +96,8 @@
|
|||||||
<arg>-H:+ReportExceptionStackTraces</arg>
|
<arg>-H:+ReportExceptionStackTraces</arg>
|
||||||
<arg>--gc=G1</arg>
|
<arg>--gc=G1</arg>
|
||||||
<arg>--enable-url-protocols=https</arg>
|
<arg>--enable-url-protocols=https</arg>
|
||||||
|
<arg>-H:IncludeResources="simplelogger.properties"</arg>
|
||||||
|
<arg>--initialize-at-build-time=org.slf4j.simple.SimpleLogger,org.slf4j.simple.SimpleLoggerFactory</arg>
|
||||||
<arg>-H:ReflectionConfigurationFiles=src/main/resources/reflect-config.json</arg>
|
<arg>-H:ReflectionConfigurationFiles=src/main/resources/reflect-config.json</arg>
|
||||||
</buildArgs>
|
</buildArgs>
|
||||||
<metadataRepository>
|
<metadataRepository>
|
||||||
|
|||||||
@ -1,5 +1,7 @@
|
|||||||
package lion;
|
package lion;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
@ -8,7 +10,7 @@ import java.util.HashMap;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
@Slf4j
|
||||||
public class MultiThreadedHTTPServer {
|
public class MultiThreadedHTTPServer {
|
||||||
private static final int PORT = 8888;
|
private static final int PORT = 8888;
|
||||||
private static final int BUFFER_SIZE = 1024;
|
private static final int BUFFER_SIZE = 1024;
|
||||||
@ -22,26 +24,27 @@ public class MultiThreadedHTTPServer {
|
|||||||
real_ip = "207.60.50.74";
|
real_ip = "207.60.50.74";
|
||||||
}
|
}
|
||||||
try(ServerSocket serverSocket = new ServerSocket(PORT)) {
|
try(ServerSocket serverSocket = new ServerSocket(PORT)) {
|
||||||
System.out.println("Server listening on port " + PORT);
|
log.info("Server listening on port {}", PORT);
|
||||||
while (true) {
|
while (true) {
|
||||||
Socket clientSocket = serverSocket.accept();
|
Socket clientSocket = serverSocket.accept();
|
||||||
String ip = clientSocket.getInetAddress().getHostAddress();
|
String ip = clientSocket.getInetAddress().getHostAddress();
|
||||||
if(ip.equals(real_ip)){
|
if(ip.equals(real_ip)){
|
||||||
System.out.println("Client connected");
|
log.info("Client connected");
|
||||||
// 线程池处理下载请求
|
// 线程池处理下载请求
|
||||||
threadPool.submit(() -> handleClientRequest(clientSocket));
|
threadPool.submit(() -> handleClientRequest(clientSocket));
|
||||||
}else{
|
}else{
|
||||||
System.out.println("unknown ip: " + ip);
|
log.info("unknown ip: " + ip);
|
||||||
clientSocket.close();
|
clientSocket.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
log.error("处理http请求时出错,IP:{},ERROR:{}", real_ip, e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void handleClientRequest(Socket clientSocket) {
|
private static void handleClientRequest(Socket clientSocket) {
|
||||||
|
String fileName = "";
|
||||||
try {
|
try {
|
||||||
BufferedReader requestReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
|
BufferedReader requestReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
|
||||||
String requestLine = requestReader.readLine();
|
String requestLine = requestReader.readLine();
|
||||||
@ -50,7 +53,7 @@ public class MultiThreadedHTTPServer {
|
|||||||
String[] requestParts = requestLine.split(" ");
|
String[] requestParts = requestLine.split(" ");
|
||||||
String method = requestParts[0];
|
String method = requestParts[0];
|
||||||
Map<String, String> paramMap = parseRequestLine(requestParts[1]);//path
|
Map<String, String> paramMap = parseRequestLine(requestParts[1]);//path
|
||||||
System.out.println(Arrays.toString(requestParts));
|
log.info(Arrays.toString(requestParts));
|
||||||
|
|
||||||
// Only handle GET requests
|
// Only handle GET requests
|
||||||
if (method.equals("GET")) {
|
if (method.equals("GET")) {
|
||||||
@ -97,7 +100,8 @@ public class MultiThreadedHTTPServer {
|
|||||||
sendErrorResponse(clientSocket, "403 Forbidden");
|
sendErrorResponse(clientSocket, "403 Forbidden");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
System.out.println(file.getAbsolutePath());
|
fileName = file.getName();
|
||||||
|
log.info(file.getAbsolutePath());
|
||||||
// Check if the file exists and is readable
|
// Check if the file exists and is readable
|
||||||
if (file.exists() && file.isFile() && file.canRead()) {
|
if (file.exists() && file.isFile() && file.canRead()) {
|
||||||
// Get the file length
|
// Get the file length
|
||||||
@ -154,7 +158,7 @@ public class MultiThreadedHTTPServer {
|
|||||||
requestReader.close();
|
requestReader.close();
|
||||||
clientSocket.close();
|
clientSocket.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
log.error("处理文件下载时出错,IP:{}, 文件:{}, ERROR:{}", clientSocket.getInetAddress().getHostAddress(), fileName, e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -117,21 +117,26 @@ public class DownloadCheckService {
|
|||||||
compress_queue.clear();
|
compress_queue.clear();
|
||||||
reentrantLock.unlock();
|
reentrantLock.unlock();
|
||||||
for (GalleryTask galleryTask : galleryTasks) {
|
for (GalleryTask galleryTask : galleryTasks) {
|
||||||
|
try {
|
||||||
|
log.info("开始压缩:{}", galleryTask.getName());
|
||||||
//创建文件夹
|
//创建文件夹
|
||||||
File file = new File(storagePath + galleryTask.getName());
|
File file = new File(storagePath + galleryTask.getName());
|
||||||
if (file.isDirectory() || file.mkdirs()) {
|
if (file.isDirectory() || file.mkdirs()) {
|
||||||
log.info(galleryTask.getName() + "文件夹创建成功");
|
log.info("{}文件夹创建成功", galleryTask.getName());
|
||||||
} else {
|
} else {
|
||||||
log.error(galleryTask.getName() + "文件夹创建失败");
|
log.error("{}文件夹创建失败", galleryTask.getName());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
//生成压缩包
|
//生成压缩包
|
||||||
ZipUtil.zip(galleryTask.getPath(), storagePath + galleryTask.getName() + "/" + galleryTask.getName() + ".zip");
|
ZipUtil.zip(galleryTask.getPath(), storagePath + galleryTask.getName() + "/" + galleryTask.getName() + ".zip");
|
||||||
log.info(galleryTask.getName() + "压缩完成" );
|
log.info("{}压缩完成", galleryTask.getName());
|
||||||
|
|
||||||
FileUtil.del(galleryTask.getPath());
|
FileUtil.del(galleryTask.getPath());
|
||||||
galleryTask.setStatus(GalleryTask.COMPRESS_COMPLETE);
|
galleryTask.setStatus(GalleryTask.COMPRESS_COMPLETE);
|
||||||
|
} catch (Exception e){
|
||||||
|
log.error("{}压缩失败:{}", galleryTask, e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,8 +15,6 @@ import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -115,14 +113,7 @@ public class storageNode {
|
|||||||
queue.removeIf(GalleryTask::is_compress_complete);
|
queue.removeIf(GalleryTask::is_compress_complete);
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
}catch (Exception e){
|
}catch (Exception e){
|
||||||
e.printStackTrace();
|
log.error("发送任务状态时发生异常:{}", e.getMessage());
|
||||||
try (OutputStream outputStream = new FileOutputStream("/root/gallery/storageNode/err.txt")){
|
|
||||||
outputStream.write(e.getMessage().getBytes());
|
|
||||||
channelFuture.channel().close().sync();
|
|
||||||
System.exit(-1);
|
|
||||||
}catch (Exception ex){
|
|
||||||
ex.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -135,7 +126,7 @@ public class storageNode {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void channelRead(ChannelHandlerContext ctx, Object msg) {
|
public void channelRead(ChannelHandlerContext ctx, Object msg) {
|
||||||
System.out.println(msg);
|
log.info(String.valueOf(msg));
|
||||||
AbstractMessage abstractMessage = (AbstractMessage) msg;
|
AbstractMessage abstractMessage = (AbstractMessage) msg;
|
||||||
|
|
||||||
switch (abstractMessage.messageType){
|
switch (abstractMessage.messageType){
|
||||||
@ -154,7 +145,7 @@ public class storageNode {
|
|||||||
DownloadPostMessage dpm = (DownloadPostMessage) abstractMessage;
|
DownloadPostMessage dpm = (DownloadPostMessage) abstractMessage;
|
||||||
lock.lock();
|
lock.lock();
|
||||||
queue.add(dpm.getGalleryTask());
|
queue.add(dpm.getGalleryTask());
|
||||||
System.out.println(queue);
|
log.info(String.valueOf(queue));
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
ctx.writeAndFlush(new ResponseMessage(dpm.messageId, (byte) 0));
|
ctx.writeAndFlush(new ResponseMessage(dpm.messageId, (byte) 0));
|
||||||
}
|
}
|
||||||
|
|||||||
9
src/main/resources/simplelogger.properties
Normal file
9
src/main/resources/simplelogger.properties
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
|
||||||
|
org.slf4j.simpleLogger.defaultLogLevel=info
|
||||||
|
|
||||||
|
org.slf4j.simpleLogger.showDateTime=true
|
||||||
|
|
||||||
|
org.slf4j.simpleLogger.dateTimeFormat=yyyy-MM-dd HH:mm:ss
|
||||||
|
|
||||||
|
org.slf4j.simpleLogger.logFile=run.out
|
||||||
|
|
||||||
Loading…
Reference in New Issue
Block a user