文件服务器fdfs集群切换和上传或者下载条数限制问题的解决方案

本文详细介绍了使用FDFS和Keepalived搭建热备文件服务器的常见问题及解决方案,针对下载进程卡死和高并发下载瓶颈进行深入分析,并提供了核心配置流程与工具类封装示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题一:搭建双击热备(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);
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BACKWASH2038

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值