网站首页 > 技术文章 正文
1. FastDFS概述
FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储、文件同步、文件访问(文件上传、文件下载)等,解决了大容量存储和负载均衡的问题。特别适合以文件为载体的在线服务,如相册网站、视频网站等等。
FastDFS为互联网量身定制,充分考虑了冗余备份、负载均衡、线性扩容等机制,并注重高可用、高性能等指标,使用FastDFS很容易搭建一套高性能的文件服务器集群提供文件上传、下载等服务。
FastDFS服务端有两个角色:跟踪器(tracker)和存储节点(storage)。跟踪器主要做调度工作,在访问上起负载均衡的作用。
存储节点存储文件,完成文件管理的所有功能:就是这样的存储、同步和提供存取接口,FastDFS同时对文件的metadata进行管理。所谓文件的meta data就是文件的相关属性,以键值对(key value)方式表示,如:width=1024,其中的key为width,value为1024。文件metadata是文件属性列表,可以包含多个键值对。
跟踪器和存储节点都可以由一台或多台服务器构成。跟踪器和存储节点中的服务器均可以随时增加或下线而不会影响线上服务。其中跟踪器中的所有服务器都是对等的,可以根据服务器的压力情况随时增加或减少。
为了支持大容量,存储节点(服务器)采用了分卷(或分组)的组织方式。存储系统由一个或多个卷组成,卷与卷之间的文件是相互独立的,所有卷的文件容量累加就是整个存储系统中的文件容量。一个卷可以由一台或多台存储服务器组成,一个卷下的存储服务器中的文件都是相同的,卷中的多台存储服务器起到了冗余备份和负载均衡的作用。
在卷中增加服务器时,同步已有的文件由系统自动完成,同步完成后,系统自动将新增服务器切换到线上提供服务。
当存储空间不足或即将耗尽时,可以动态添加卷。只需要增加一台或多台服务器,并将它们配置为一个新的卷,这样就扩大了存储系统的容量。
FastDFS中的文件标识分为两个部分:卷名和文件名,二者缺一不可。
2. 环境搭建
1、Linux-centos7上安装FastDFS(繁琐)
自行百度,重点介绍docker安装方式
2、docker安装FastDFS(推荐)
1.拉取镜像
docker pull delron/fastdfs
2.使用docker镜像构建tracker容器(跟踪服务器,起到调度的作用)
docker run -dti --network=host --name tracker -v /var/fdfs/tracker:/var/fdfs -v /etc/localtime:/etc/localtime delron/fastdfs tracker
3.使用docker镜像构建storage容器(存储服务器,提供容量和备份服务)
TRACKER_SERVER=本机的ip地址:22122 ,本机ip地址不要使用127.0.0.1
docker run -dti --network=host --name storage -e TRACKER_SERVER=192.168.157.133:22122 -v /var/fdfs/storage:/var/fdfs -v /etc/localtime:/etc/localtime delron/fastdfs storage
4.进入storage容器,到storage的配置文件中配置http访问的端口,配置文件在/etc/fdfs目录下的storage.conf
#进入容器
docker exec -it storage bash
#进入目录
cd /etc/fdfs/
#编辑文件
vi storage.conf
默认端口是8888,也可以不进行更改。
5.修改storage中的nginx 不需要安装
cd /usr/local/nginx/conf
vi nginx.conf
6.修改完配置重启容器,没有修改就不需要重启
docker stop storage
docker start storage
如果重启后无法启动的会,可能是报下面错误了,手动创建 vi
/var/fdfs/logs/storaged.log 文件即可 tail: cannot open '
/var/fdfs/logs/storaged.log' for reading: No such file or directory
7.测试
7.1 进入storage容器,进入/var/fdfs目录
docker exec -it storage bash
cd /var/fdfs
echo hello 这是我的第一个测试文件,大家觉得不错关注下吧>a.txt
/usr/bin/fdfs_upload_file /etc/fdfs/client.conf a.txt
浏览器访问
http://ip:8888/group1/M00/00/00/wKgcgF-_Le6AS4LvAAAATzab9Do068.txt 端口根据你在starage里面设置的要保持一致,访问之前关闭防火墙或者自己单独开放端口
8.开放端口
firewall-cmd --zone=public --permanent --add-port=8888/tcp
firewall-cmd --zone=public --permanent --add-port=22122/tcp
firewall-cmd --zone=public --permanent --add-port=23000/tcp
重启防火墙
systemctl restart firewalld
9.开机启动容器
设置容器随docker启动而启动(由于docker是开机启动的,所以容器也会随之启动)
docker update --restart=always tracker
docker update --restart=always storage
3. 服务器端口和防火墙
由于服务器防火墙的原因,导致指定的端口未暴露会出现如下问题: 1、浏览器访问fastdfs上传的资源访问不到
http://192.168.157.133:8888/group1/M00/00/00/wKidhWF1ABqAYqUwAAAATzab9Do485.txt 2、fastdfs-client客户端连接fastdfs服务出错 会报下面错误:
fastDFS报错:Unable to borrow buffer from pool
解决方案(centos7):
1、关闭防火墙
由于FastDFS安装在Linux中 22122端口被管控,windows系统访问时需要先关闭防火墙或者开放该端口 1.查看防火墙状态:
firewall-cmd --state
2.关闭并禁用防火墙,可能需要重启服务器
sudo systemctl stop firewalld.service sudo systemctl disable firewalld.service
3.查看防火墙状态:
firewall-cmd --state not running
2、开放端口
firewall-cmd --zone=public --permanent --add-port=8888/tcp
firewall-cmd --zone=public --permanent --add-port=22122/tcp
firewall-cmd --zone=public --permanent --add-port=23000/tcp
#重启防火墙
systemctl restart firewalld
然后重启fastdfs服务(tracker、storage、nginx)
4. FastDFS在Web项目中应用
Controller层
package com.xjt.fdfs.controller;
import cn.hutool.core.io.FileUtil;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Api(tags = "FastDFS测试")
@RestController
@RequestMapping("/api/fastdfs")
public class FileController {
@Autowired
private FastFileStorageClient storageClient;
public final static String server_host = "http://192.168.157.134:8888/";
@ResponseBody
@ApiOperation(value = "上传文件",httpMethod = "POST")
@PostMapping("/upload")
public String uploadFile(@ApiParam("文件") MultipartFile file) throws IOException {
StorePath storePath = storageClient.uploadFile(file.getInputStream(),
file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()),null);
String filepath = server_host + storePath.getFullPath();
return "文件上传成功 地址为:"+filepath;
}
@ResponseBody
@ApiOperation(value = "删除文件",httpMethod = "POST")
@PostMapping("/delete")
public void deleteFile(String fileUrl){
if (StringUtils.isEmpty(fileUrl)) {
return;
}
try {
StorePath storePath = StorePath.parseFromUrl(fileUrl);
storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
@ResponseBody
@ApiOperation(value = "下载文件",httpMethod = "POST")
@PostMapping("/download")
public void downloadFile(HttpServletRequest request, HttpServletResponse response,String fileUrl){
try {
StorePath storePath = StorePath.parseFromUrl(fileUrl);
System.out.println(storePath.getGroup());
System.out.println(storePath.getPath());
byte[] bytes = storageClient.downloadFile(storePath.getGroup(), storePath.getPath(), new DownloadByteArray());
response.getOutputStream().write(bytes);
FileUtil.writeBytes(bytes,FileUtil.file(storePath.getPath()));
} catch (Exception e) {
return;
}
}
}
配置文件application.properties
#fastdfs配置
fdfs.connect-timeout=1500
fdfs.so-timeout=1500
fdfs.trackerList[0]=192.168.157.134:22122
fdfs.thumbImage.height=150
fdfs.thumbImage.width=150
spring.jmx.enabled=false
fdfs.pool.max-total=200
spring.servlet.multipart.max-file-size=1000MB
spring.servlet.multipart.max-request-size=1000MB
猜你喜欢
- 2025-05-28 教你一招快速入门网络编程
- 2025-05-28 有了这些开源工具后,明天争取五点下班了!
- 2025-05-28 十年老架构师:带你读懂springmvc的世界!
- 2025-05-28 一招搞定外部请求,这款 HTTP 客户端框架真的很强大!
- 2025-05-28 动态代理 because it is a JDK dynamic proxy that implements
- 2025-05-28 基于Python的数据导出和邮件发送
- 2025-05-28 快来试试这款一行代码实现多平台文件存储的工具!
- 2025-05-28 PDF文件添加二维码水印
- 2025-05-28 HttpClient PostAsync 应用指南
- 2025-05-28 神操作!SpringBoot 如何实现 PDF 添加水印?
- 1522℃桌面软件开发新体验!用 Blazor Hybrid 打造简洁高效的视频处理工具
- 647℃Dify工具使用全场景:dify-sandbox沙盒的原理(源码篇·第2期)
- 527℃MySQL service启动脚本浅析(r12笔记第59天)
- 492℃服务器异常重启,导致mysql启动失败,问题解决过程记录
- 492℃启用MySQL查询缓存(mysql8.0查询缓存)
- 479℃「赵强老师」MySQL的闪回(赵强iso是哪个大学毕业的)
- 461℃mysql服务怎么启动和关闭?(mysql服务怎么启动和关闭)
- 459℃MySQL server PID file could not be found!失败
- 最近发表
- 标签列表
-
- cmd/c (90)
- c++中::是什么意思 (84)
- 标签用于 (71)
- 主键只能有一个吗 (77)
- c#console.writeline不显示 (95)
- pythoncase语句 (88)
- es6includes (74)
- sqlset (76)
- windowsscripthost (69)
- apt-getinstall-y (100)
- node_modules怎么生成 (87)
- chromepost (71)
- flexdirection (73)
- c++int转char (80)
- mysqlany_value (79)
- static函数和普通函数 (76)
- el-date-picker开始日期早于结束日期 (70)
- asynccallback (71)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- java (73)
- js数组插入 (83)
- mac安装java (72)
- 查看mysql是否启动 (70)
- 无效的列索引 (74)