問題一:搭建雙擊熱備(keepalived+fdfs)檔案伺服器fdfs後,如果一臺檔案伺服器掛了之後浮點ip漂移到另一臺的時候正在下載的檔案進程會卡死下載不下載(上傳也一樣);
問題二:下載檔案數量超過1000會卡死;
解決方案:由於老闆版問題連線資源沒有及時釋放導致卡死狀態;注意版本號一定要1.29以上
核心設定流程和工具類封裝一下
<!-- 引入fdfs檔案伺服器jar -->
<dependency>
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.29-SNAPSHOT</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/jar/fastdfs-client-java-1.29.jar</systemPath>
</dependency>
package com.people.util;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.csource.fastdfs.*;
import org.csource.fastdfs.pool.Connection;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import com.people.exception.RRException;
import java.io.File;
import java.io.IOException;
@Component
public class FileDfsUtils {
static {
try {
ClientGlobal.initByProperties("fastdfs-client.properties");
// ClientGlobal.initByTrackers("192.168.98.20:22122,192.168.98.22:22122");
} catch (Exception e) {
Slf4jLogUtil.error("FastFds初始化失敗", e);
}
}
/**
* 上傳檔案(通用)
*
* @param multipartFile 檔案
* @return 路徑
*/
public String upload(MultipartFile multipartFile) throws Exception {
if (multipartFile.getSize() == 0){
return null;
}
String extName = FilenameUtils.getExtension(multipartFile.getOriginalFilename());
String path = getStorageClient().upload_file1(multipartFile.getBytes(), extName, null);
return path;
}
/**
* 上傳檔案
*
* @param filePath 檔案路徑
* @return 路徑
*/
public String uploadFile(String filePath) throws Exception {
return uploadFile(new File(filePath));
}
/**
* 上傳檔案
*
* @param file 檔案
* @return 路徑
* @throws Exception
*/
public String uploadFile(File file) throws Exception {
if (file == null || !file.exists()) {
return null;
}
String extName = FilenameUtils.getExtension(file.getName());
String path = getStorageClient().upload_file1(FileUtils.readFileToByteArray(file), extName, null);
return path;
}
/**
* 下載檔案
*
* @param filePath 檔案路徑
* @return 檔案位元組數據
* @throws Exception
*/
public byte[] downFile(String filePath) throws Exception {
if (StringUtils.isEmpty(filePath)) {
Slf4jLogUtil.info("fileUrl == >>檔案路徑爲空...");
return null;
}
String groupName = getGroupName(filePath);
String remoteFilename = getPath(filePath, groupName);
byte[] files = getStorageClient().download_file(groupName, remoteFilename);
return files;
}
/**
* 刪除檔案
*
* @param fileUrl 檔案路徑
*/
public void deleteFile(String fileUrl) throws Exception {
if (StringUtils.isEmpty(fileUrl)) {
Slf4jLogUtil.info("fileUrl == >>檔案路徑爲空...");
return;
}
String groupName = getGroupName(fileUrl);
String remoteFilename = getPath(fileUrl, groupName);
getStorageClient().delete_file(groupName, remoteFilename);
}
private StorageClient1 getStorageClient() throws Exception {
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getTrackerServer();
StorageClient1 storageClient = new StorageClient1(trackerServer, null);
return storageClient;
}
private String getGroupName(String filePath) {
String[] paths = filePath.split("/");
if (paths.length == 1) {
throw new RRException("解析檔案路徑錯誤,有效的路徑樣式爲(group/path) 而當前解析路徑爲".concat(filePath));
} else {
String[] var2 = paths;
int var3 = paths.length;
for (int var4 = 0; var4 < var3; ++var4) {
String item = var2[var4];
if (item.indexOf("group") != -1) {
return item;
}
}
throw new RRException("解析檔案路徑錯誤,被解析路徑url沒有group,當前解析路徑爲".concat(filePath));
}
}
private String getPath(String filePath, String group) {
int pathStartPos = filePath.indexOf(group) + group.length() + 1;
return filePath.substring(pathStartPos);
}
}