1.定义:FastDFS分布式文件系统是用c语言编写的一款开源的分布式文件系统,考虑了冗余备份、负载均衡、线性扩容等机制,提供文件上传、下载,存储,同步,FastDFS架构包括 Tracker server(追踪、调度服务器)和Storage server(存储服务器)。
2..原理:客户端请求Tracker server进行文件上传、下载,通过Tracker server调度最终由Storage server完成文件上传和下载。
3.Tracker server作用:负载均衡和调度,通过Tracker server在文件上传时可以根据一些策略找到Storage server提供文件上传服务。可以将tracker称为追踪服务器或调度服务器。
4.Storage server作用:文件存储,客户端上传的文件最终存储在Storage服务器上,Storage server没有实现自己的文件系统而是利用操作系统 的文件系统来管理文件。可以将storage称为存储服务器。
5.FastDFS与Tomcat对比:
- FastDFS相比于Tomcat :不存在单点故障问题、支持容灾。
- FastDFS相比于Tomcat:集群 (安全) 保存图片过多时扩容 分配合理 。
- FastDFS相比于Tomcat: 具有备份功能、不怕磁盘损坏。
6.FastDFS安装及配置(Linux环境下):https://www.2cto.com/kf/201708/669886.html
7.FastDFS使用(Myeclipse)
- 配置文件client.conf
![](https://i-blog.csdnimg.cn/blog_migrate/f253d38464e537617d0d4d68b93e9ae1.png)
- 工具类FastDFSClient
package com.taotao.common.utils;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
public class FastDFSClient {
private TrackerClient trackerClient = null;
private TrackerServer trackerServer = null;
private StorageServer storageServer = null;
private StorageClient1 storageClient = null;
public FastDFSClient(String conf) throws Exception {
if (conf.contains("classpath:")) {
conf = conf.replace("classpath:", this.getClass().getResource("/").getPath());
}
ClientGlobal.init(conf);
trackerClient = new TrackerClient();
trackerServer = trackerClient.getConnection();
storageServer = null;
storageClient = new StorageClient1(trackerServer, storageServer);
}
/**
* 上传文件方法
* <p>Title: uploadFile</p>
* <p>Description: </p>
* @param fileName 文件全路径
* @param extName 文件扩展名,不包含(.)
* @param metas 文件扩展信息
* @return
* @throws Exception
*/
public String uploadFile(String fileName, String extName, NameValuePair[] metas) throws Exception {
String result = storageClient.upload_file1(fileName, extName, metas);
return result;
}
public String uploadFile(String fileName) throws Exception {
return uploadFile(fileName, null, null);
}
public String uploadFile(String fileName, String extName) throws Exception {
return uploadFile(fileName, extName, null);
}
/**
* 上传文件方法
* <p>Title: uploadFile</p>
* <p>Description: </p>
* @param fileContent 文件的内容,字节数组
* @param extName 文件扩展名
* @param metas 文件扩展信息
* @return
* @throws Exception
*/
public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) throws Exception {
String result = storageClient.upload_file1(fileContent, extName, metas);
return result;
}
public String uploadFile(byte[] fileContent) throws Exception {
return uploadFile(fileContent, null, null);
}
public String uploadFile(byte[] fileContent, String extName) throws Exception {
return uploadFile(fileContent, extName, null);
}
}
- 图片上传时调用FastDFSClient对应服务
package com.taotao.service.impl;
import java.io.IOException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.taotao.common.pojo.PictureResult;
import com.taotao.common.utils.FastDFSClient;
import com.taotao.service.PictureService;
@Service
public class PictureServiceImpl implements PictureService {
//192.168.64.128
@Value("${IMAGE_SERVER_BASE_URL}")
private String IMAGE_SERVER_BASE_URL;
@Override
public PictureResult uploadPic(MultipartFile picFile) {
System.out.println("PictureServiceImpl.uploadPic()=========================");
PictureResult result=new PictureResult();
//判断图片是否为空
if(picFile.isEmpty()){
System.out.println("picFile is empty==============================");
result.setError(1);
result.setMessage("there must be a picture at least");
return result;
}
//上传图片到服务器
try {
String originalFilename = picFile.getOriginalFilename();
String extName = originalFilename.substring(originalFilename.lastIndexOf(".")+1);
FastDFSClient client=new FastDFSClient("classpath:properties/client.conf");
String url=client.uploadFile(picFile.getBytes(),extName);
url=IMAGE_SERVER_BASE_URL+url;
System.out.println("url:"+url);
result.setError(0);
result.setUrl(url);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
result.setError(1);
result.setMessage("upload pictures failed");
}
return result;
}
}