FastDFS的两个核心概念分别是:Tracker(跟踪器)、Storage(存储节点) 跟踪器Tracker主要做调度工作,相当于mvc中的controller的角色,在访问上起负载均衡的作用。跟踪器和存储节点都可以由一台或多台服务器构成,跟踪器和存储节点中的服务器均可以随时增加或下线而不会影响线上服务,其中跟踪器中的所有服务器都是对等的,可以根据服务器的压力情况随时增加或减少。跟踪器Tracker负责管理所有的Storage和group,每个Storage在启动后会连接Tracker,告知自己所属的group等信息,并保持周期性的心跳,tracker根据storage的心跳信息,建立group==>[storage server list]的映射表,Tracker需要管理的元信息很少,会全部存储在内存中;另外tracker上的元信息都是由storage汇报的信息生成的,本身不需要持久化任何数据,这样使得tracker非常容易扩展,直接增加tracker机器即可扩展为tracker cluster来服务,cluster里每个tracker之间是完全对等的,所有的tracker都接受stroage的心跳信息,生成元数据信息来提供读写服务。 存储节点Storage采用了分卷[Volume](或分组[group])的组织方式,存储系统由一个或多个组组成,组与组之间的文件是相互独立的,所有组的文件容量累加就是整个存储系统中的文件容量。 一个卷[Volume](组[group])可以由一台或多台存储服务器组成,一个组中的存储服务器中的文件都是相同的,组中的多台存储服务器起到了冗余备份和负载均衡的作用,数据互为备份,存储空间以group内容量最小的storage为准,所以建议group内的多个storage尽量配置相同,以免造成存储空间的浪费。
一、com.github.tobato.fastdfs-client
引入相关的依赖
<dependency><groupId>com.github.tobato</groupId><artifactId>fastdfs-client</artifactId><version>1.26.6</version></dependency>
在application.yml当中配置Fdfs相关参数
# ===================================================================# 分布式文件系统FDFS配置# ===================================================================fdfs:so-timeout: 1501connect-timeout: 601thumb-image: #缩略图生成参数width: 150height: 150webServerUrl: http://……/tracker-list: #TrackerList参数,支持多个- 192.168.1.105:22122- 192.168.1.106:22122
连接池配置,如果有必要可以参考 apache.pool2 参数配置连接池属性,默认配置为
fdfs:..其他配置信息..pool:#从池中借出的对象的最大数目(配置为-1表示不限制)max-total: -1#获取连接时的最大等待毫秒数(默认配置为5秒)max-wait-millis: 5*1000#每个key最大连接数max-total-per-key: 50#每个key对应的连接池最大空闲连接数max-idle-per-key: 10#每个key对应的连接池最小空闲连接数max_idle_per_key: 5
注意: key配置的是连接服务端的地址(IP+端口)连接情况,如果有连接不够用的情况可以调整以上参数
主要接口包括
- TrackerClient - TrackerServer接口
- GenerateStorageClient - 一般文件存储接口 (StorageServer接口)
- FastFileStorageClient - 为方便项目开发集成的简单接口(StorageServer接口)
- AppendFileStorageClient - 支持文件续传操作的接口 (StorageServer接口)
问题:
高并发下测试出现上传的文件和得到的返回路径的文件不是同一个
通过加大超时时间后解决
soTimeout: 1500connectTimeout: 600
FastDFS客户端
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;import com.github.tobato.fastdfs.domain.fdfs.StorePath;import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;import com.github.tobato.fastdfs.service.FastFileStorageClient;import org.apache.commons.io.FilenameUtils;import org.apache.commons.lang3.StringUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import org.springframework.web.multipart.MultipartFile;import java.io.ByteArrayInputStream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.nio.charset.Charset;@Componentpublic class FastDFSClient {private final Logger logger = LoggerFactory.getLogger(FastDFSClient.class);@Autowiredprivate FastFileStorageClient storageClient;@Autowiredprivate FdfsWebServer fdfsWebServer;/*** 上传文件* @param file 文件对象* @return 文件访问地址* @throws IOException*/public String uploadFile(MultipartFile file) throws IOException {StorePath storePath = storageClient.uploadFile(file.getInputStream(),file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()),null);return getResAccessUrl(storePath);}/*** 上传文件* @param file 文件对象* @return 文件访问地址* @throws IOException*/public String uploadFile(File file) throws IOException {FileInputStream inputStream = new FileInputStream (file);StorePath storePath = storageClient.uploadFile(inputStream,file.length(), FilenameUtils.getExtension(file.getName()),null);return getResAccessUrl(storePath);}/*** 将一段字符串生成一个文件上传* @param content 文件内容* @param fileExtension* @return*/public String uploadFile(String content, String fileExtension) {byte[] buff = content.getBytes(Charset.forName("UTF-8"));ByteArrayInputStream stream = new ByteArrayInputStream(buff);StorePath storePath = storageClient.uploadFile(stream,buff.length, fileExtension,null);return getResAccessUrl(storePath);}// 封装图片完整URL地址private String getResAccessUrl(StorePath storePath) {String fileUrl = fdfsWebServer.getWebServerUrl() + storePath.getFullPath();return fileUrl;}/*** 删除文件* @param fileUrl 文件访问地址* @return*/public void deleteFile(String fileUrl) {if (StringUtils.isEmpty(fileUrl)) {return;}try {StorePath storePath = StorePath.parseFromUrl(fileUrl);storageClient.deleteFile(storePath.getGroup(), storePath.getPath());} catch (FdfsUnsupportStorePathException e) {logger.warn(e.getMessage());}}}
二、org.csource.fastdfs-client-java
添加pom 依赖
<dependency><groupId>org.csource</groupId><artifactId>fastdfs-client-java</artifactId><version>1.27-SNAPSHOT</version></dependency>
resources目录下添加fdfs_client.conf文件
connect_timeout = 60network_timeout = 60charset = UTF-8http.tracker_http_port = 8080http.anti_steal_token = nohttp.secret_key = 123456tracker_server = 182.254.161.54:22122
上传工具类
public class FastDFSClient {private static org.slf4j.Logger logger = LoggerFactory.getLogger(FastDFSClient.class);static {try {String filePath = new ClassPathResource("fdfs_client.conf").getFile().getAbsolutePath();;ClientGlobal.init(filePath);} catch (Exception e) {logger.error("FastDFS Client Init Fail!",e);}}public static String[] upload(FastDFSFile file) {logger.info("File Name: " + file.getName() + "File Length:" + file.getContent().length);NameValuePair[] meta_list = new NameValuePair[1];meta_list[0] = new NameValuePair("author", file.getAuthor());long startTime = System.currentTimeMillis();String[] uploadResults = null;StorageClient storageClient=null;try {storageClient = getTrackerClient();uploadResults = storageClient.upload_file(file.getContent(), file.getExt(), meta_list);} catch (IOException e) {logger.error("IO Exception when uploadind the file:" + file.getName(), e);} catch (Exception e) {logger.error("Non IO Exception when uploadind the file:" + file.getName(), e);}logger.info("upload_file time used:" + (System.currentTimeMillis() - startTime) + " ms");if (uploadResults == null && storageClient!=null) {logger.error("upload file fail, error code:" + storageClient.getErrorCode());}String groupName = uploadResults[0];String remoteFileName = uploadResults[1];logger.info("upload file successfully!!!" + "group_name:" + groupName + ", remoteFileName:" + " " + remoteFileName);return uploadResults;}public static FileInfo getFile(String groupName, String remoteFileName) {try {StorageClient storageClient = getTrackerClient();return storageClient.get_file_info(groupName, remoteFileName);} catch (IOException e) {logger.error("IO Exception: Get File from Fast DFS failed", e);} catch (Exception e) {logger.error("Non IO Exception: Get File from Fast DFS failed", e);}return null;}public static InputStream downFile(String groupName, String remoteFileName) {try {StorageClient storageClient = getTrackerClient();byte[] fileByte = storageClient.download_file(groupName, remoteFileName);InputStream ins = new ByteArrayInputStream(fileByte);return ins;} catch (IOException e) {logger.error("IO Exception: Get File from Fast DFS failed", e);} catch (Exception e) {logger.error("Non IO Exception: Get File from Fast DFS failed", e);}return null;}public static void deleteFile(String groupName, String remoteFileName)throws Exception {StorageClient storageClient = getTrackerClient();int i = storageClient.delete_file(groupName, remoteFileName);logger.info("delete file successfully!!!" + i);}public static StorageServer[] getStoreStorages(String groupName)throws IOException {TrackerClient trackerClient = new TrackerClient();TrackerServer trackerServer = trackerClient.getConnection();return trackerClient.getStoreStorages(trackerServer, groupName);}public static ServerInfo[] getFetchStorages(String groupName,String remoteFileName) throws IOException {TrackerClient trackerClient = new TrackerClient();TrackerServer trackerServer = trackerClient.getConnection();return trackerClient.getFetchStorages(trackerServer, groupName, remoteFileName);}public static String getTrackerUrl() throws IOException {return "http://"+getTrackerServer().getInetSocketAddress().getHostString()+":"+ ClientGlobal.getG_tracker_http_port()+"/";}private static StorageClient getTrackerClient() throws IOException {TrackerServer trackerServer = getTrackerServer();StorageClient storageClient = new StorageClient(trackerServer, null);return storageClient;}private static TrackerServer getTrackerServer() throws IOException {TrackerClient trackerClient = new TrackerClient();TrackerServer trackerServer = trackerClient.getConnection();return trackerServer;}}
测试
@PostMapping("/upload")public String singleFileUpload(@RequestParam("file") MultipartFile file,RedirectAttributes redirectAttributes) {if (file.isEmpty()) {redirectAttributes.addFlashAttribute("message", "Please select a file to upload");return "redirect:uploadStatus";}try {// Get the file and save it somewhereString path=saveFile(file);redirectAttributes.addFlashAttribute("message","You successfully uploaded '" + file.getOriginalFilename() + "'");redirectAttributes.addFlashAttribute("path","file path url '" + path + "'");} catch (Exception e) {logger.error("upload file failed",e);}return "redirect:/uploadStatus";}@GetMapping("/uploadStatus")public String uploadStatus() {return "uploadStatus";}/*** @param multipartFile* @return* @throws IOException*/public String saveFile(MultipartFile multipartFile) throws IOException {String[] fileAbsolutePath={};String fileName=multipartFile.getOriginalFilename();String ext = fileName.substring(fileName.lastIndexOf(".") + 1);byte[] file_buff = null;InputStream inputStream=multipartFile.getInputStream();if(inputStream!=null){int len1 = inputStream.available();file_buff = new byte[len1];inputStream.read(file_buff);}inputStream.close();FastDFSFile file = new FastDFSFile(fileName, file_buff, ext);try {fileAbsolutePath = FastDFSClient.upload(file); //upload to fastdfs} catch (Exception e) {logger.error("upload file Exception!",e);}if (fileAbsolutePath==null) {logger.error("upload file failed,please upload again!");}String path=FastDFSClient.getTrackerUrl()+fileAbsolutePath[0]+ "/"+fileAbsolutePath[1];return path;}}
