分布式文件管理系统:(相册管理)
FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储、文件同步、文件访问(文件上传、文件下载)等,解决了大容量存储和负载均衡的问题。特别适合以文件为载体的在线服务,如相册网站、视频网站等等。
工作i流程图:
三个group是集群,多组集群;
group里面的节点是同步;冗余备份;容错机制;同步机制,没有主从;
线性扩容机制;
Tracker server 作用是负载均衡和调度。
通过Tracker server 调度最终由 Storage server 完成文件上传和下载。
上传流程图:
必须配置nginx:
安装tracker和stroage时候就已经装好;
搭建fastDFS服务:
2,在resources文件夹下创建fasfDFS的配置文件fdfs_client.conf
配置连接信息:connect_timeout=60network_timeout=60charset=UTF-8
#track的http请求端口
http.tracker_http_port=8080
track的tcp通信端口tracker_server=192.168.211.132:22122
核心yml配置文件:
spring: servlet:
multipart:
max-file-size: 10MB 上传文件大小
max-request-size: 10MB 请求数据大小
--------------
封装文件上传信息:
public class FastDFSFile implements Serializable {
//文件名字
private String name;
//文件内容
private byte[] content;
//文件扩展名
private String ext;
//文件MD5摘要值
private String md5;
//文件创建作者
private String author;
public FastDFSFile(String name, byte[] content, String ext, String md5, String author) {
this.name = name;
this.content = content;
this.ext = ext;
this.md5 = md5;
this.author = author;
}
public FastDFSFile(String name, byte[] content, String ext) {
this.name = name;
this.content = content;
this.ext = ext;
}
public FastDFSFile() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public byte[] getContent() {
return content;
}
public void setContent(byte[] content) {
this.content = content;
}
public String getExt() {
return ext;
}
public void setExt(String ext) {
this.ext = ext;
}
public String getMd5() {
return md5;
}
public void setMd5(String md5) {
this.md5 = md5;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
实现FastDFS文件管理:
文件上传,文件删除,文件下载等
先去找track IP+端口
实现文件的上传:
FastDFSClientUtil
//加载全局配置文件
static{
try {
//获取classpath下面的文件路径
String filename=new ClassPathResource("fdfs_client.conf").getPath();
//加载track信息
ClientGlobal.init(filename);
} catch (Exception e) {
e.printStackTrace();
}
}
//文件上传
//@param 上传的文件信息封装
public static void upload(FastDFSFile fastDFSFile) throws IOException, MyException {
//附加参数
NameValuePair [] meta_list=new NameValuePair[1];
meta_list[0]=new NameValuePair("author",fastDFSFile.getAuthor());
//创建一个tracker访问 的客户端对象 叫TrackClient
TrackerClient trackerClient=new TrackerClient();
//通过TrackerClient访问TrackServer访问TrackServer服务,获取连接信息
TrackerServer trackerServer = trackerClient.getConnection();
//通过连接信息 获取storage的连接信息,创建storage存储stoage的连接信息
StorageClient storageClient=new StorageClient(trackerServer,null);
//通过storageClient访问Storage,实现文件上传,并且获取文件上传后的存储信息
//参数1:上传文件的字节数组
//2,文件的扩展名
//3,附加参数
storageClient.upload_file(fastDFSFile.getContent(),fastDFSFile.getExt(),meta_list);
fileController:
@RestController
@RequestMapping("/upload")
@CrossOrigin//跨域
public class FileUploadController {
@PostMapping
public Result upload(@RequestParam("file")MultipartFile file) throws IOException, MyException {
//封装文件信息
FastDFSFile fastDFSFile=new FastDFSFile(
file.getOriginalFilename(),//文件名称
file.getBytes(),//文件字节数据
StringUtils.getFilenameExtension(file.getOriginalFilename())//获取文件扩展名
);
//调用工具类将文件传入到FastDFS
FastDFSClient.upload(fastDFSFile);
return new Result(true, StatusCode.OK,"成功");
}
文件的信息获取:
* 获取文件信息
*参数:(1) groupName 文件的组名
* (2)remoteFileName:文件的存储路径的名字
public static FileInfo getFile(String groupName,String remoteFileName )throws Exception{
//创建TrackerClient对象,通过TrackerClient对象访问TrackerServer
TrackerClient trackerClient=new TrackerClient();
//通过TrackerClient获取TrackerServer的连接信息
TrackerServer trackerServer = trackerClient.getConnection();
//通过TrackerServer获取storage信息,创建StorageClient对象存储Storage信息
StorageClient storageClient=new StorageClient(trackerServer,null);
//获取文件信息
return storageClient.get_file_info(groupName,remoteFileName);
}
文件下载的实现:
public InputStream downFile(String groupName,String remoteFileName ) throws Exception{
//创建TrackerClient对象,通过TrackerClient对象访问TrackerServer
TrackerClient trackerClient=new TrackerClient();
//通过TrackerClient获取TrackerServer的连接信息
TrackerServer trackerServer = trackerClient.getConnection();
//通过TrackerServer获取storage信息,创建StorageClient对象存储Storage信息
StorageClient storageClient=new StorageClient(trackerServer,null);
byte[] buffer= storageClient.download_file(groupName,remoteFileName);
ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(buffer);
return byteArrayInputStream;
}
删除文件实现:
public static void deleteFile(String groupName,String remoteFileName)throws Exception{
//创建TrackerClient对象,通过TrackerClient对象访问TrackerServer
TrackerClient trackerClient=new TrackerClient();
//通过TrackerClient获取TrackerServer的连接信息
TrackerServer trackerServer = trackerClient.getConnection();
//通过TrackerServer获取storage信息,创建StorageClient对象存储Storage信息
StorageClient storageClient=new StorageClient(trackerServer,null);
storageClient.delete_file(groupName,remoteFileName);
}
根据文件组名和文件存储路径获取Storage服务的IP、端口信息
/**
* 根据文件组名和文件存储路径获取Storage服务的IP、端口信息
* 文件所存的storage组的信息
* @return
*/
public ServerInfo[] getServerInfo(String groupName, String remoteFileName)throws Exception{
TrackerClient trackerClient=new TrackerClient();
//通过TrackerClient获取TrackerServer的连接信息
TrackerServer trackerServer = trackerClient.getConnection();
//获取storage的ip和端口号
return trackerClient.getFetchStorages(trackerServer,groupName,remoteFileName);
}
获取Tracker的信息
/**
* 获取Tracker的信息
*/
public static String getTrackerInfo() throws Exception{
TrackerClient trackerClient=new TrackerClient();
//通过TrackerClient获取TrackerServer的连接信息
TrackerServer trackerServer = trackerClient.getConnection();
//Tracker的IP 端口
int g_tracker_http_port = ClientGlobal.getG_tracker_http_port();//端口
String ip = trackerServer.getInetSocketAddress().getHostString();//ip
String url="http://"+ip+":"+g_tracker_http_port;
return url;
}