网站首页 > 技术文章 正文
1.fastDFS简单介绍(个人理解)
1.FastDFS是一个开源的轻量级分布式文件存储系统,可以供我们去完成上传下载。
2.在FastDFS中有两部分组成一个叫tracker一个叫storage。
trackerstoragetracker 是专门去管理storage的,在FastDFS中所有的访问都会先经过tracker ,并由tracker 去找有哪些 storage压力小或者比较空闲那么tracker 就会返回一个storage地址供我们去上传下载storage的职责其实就是保存上传的数据以及对数据进行下载
2.使用docker安装fastDFS
1.查看 fastdfs 都有哪些版本
docker search fastdfs
2.我这里选择的是delron/fastdfs版本(其他的版本也是可以的不要用太新的版本)
docker pull delron/fastdfs
3.安装完毕后可以在docekr镜像库中看到
4.首先启动tracker
docker run -d --network=host --name tracker -v /home/fastdfs/docker/fastdfs/tracker:/var/fdfs delron/fastdfs tracker
5.启动storage
注意:
1.TRACKER_SERVER=ip:22122。ip要自己改成当前服务器的ip
2.GROUP_NAME=group1。storage组名称可以自己随便写
docker run -d --network=host --name storage -e TRACKER_SERVER=ip:22122 -v /home/fastdfs/docker/fastdfs/storage:/var/fdfs -e GROUP_NAME=group1 delron/fastdfs storage
注意:全部启动成功后fastdfs默认的端口有三个8888,23000,22122,大家需要在服务器中放行这三个端口
8888:是默认的nginx代理端口 delron/fastdfs这个版本包含有nginx
23000:storage服务端口
22122:tracker服务端口
3.springboot整合fastDFS
1.我的boot版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/>
</parent>
2.fastDFS坐标
<!--分布式文件存储 fastDFS-->
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.2</version>
</dependency>
<!--上传下载 间接包-->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.2</version>
</dependency>
3.需要在boot启动类再加上两个注解
//加载fastDFS
@EnableMBeanExport(registration= RegistrationPolicy.IGNORE_EXISTING)
@Import(FdfsClientConfig.class)
4.在yml中添加连接fastdfs的地址
#fastDFS
注意这个trackerList:如果是集群tracker的话以逗号分隔开就行
fdfs:
connect-timeout: 600
so-timeout: 1500
trackerList: 服务器tracker的ip:22122
thumb-image:
width: 150
height: 150
pool:
max-total: 200
5.最后就是代码部分啦直接上代码
@RestController
@RequestMapping("/file")
@Api(value = "fastDFs分布式文件存储 -- 文件上传下载",description = "fastDFs分布式文件存储 -- 文件上传下载")
@CrossOrigin//跨域的意思
public class FileController {
//FastFileStorageClient 直接注入就能用 fastdsf自带的
@Resource
private FastFileStorageClient fastFileStorageClient;
//fastDFS storage存储节点路径 xxxxxx:服务器ip
private final String FASTDFSSERVERIMAGE = "http://xxxxxx:8888/";
/**
* 文件上传
* @return result
*/
@ApiOperation("上传")
@Async("asyncServiceExecutor")
@RequestMapping(value = "/upload",method = RequestMethod.POST)
public HashMap<String, String> upload(@RequestPart("file") MultipartFile file){
HashMap<String,String> result = new HashMap<>();
try {
//这里msg返回的是上传后的图片存储位置getFullPath()获取文件位置
result.put("msg",FASTDFSSERVERIMAGE+fastFileStorageClient.uploadFile(file.getInputStream(),file.getSize(),
FilenameUtils.getExtension(file.getOriginalFilename()),null).getFullPath());
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
/**
* 文件删除
* @param path
* @return
*/
@RequestMapping(value = "/delete",method = RequestMethod.DELETE)
@ApiOperation("删除")
public HashMap<String, Object> delete(@RequestParam String path) {
HashMap<String,Object> result = new HashMap<>();
fastFileStorageClient.deleteFile(path);
result.put("msg","大哥啦~~~!!,删除成功!");
return result;
}
/**
* 文件下载
* @param url 路径
* @return
*/
@RequestMapping(value = "/download",method = RequestMethod.GET)
@ApiOperation("下载")
public void downLoad(@RequestParam String url, HttpServletResponse response) throws IOException {
//文件后缀
String substring = url.substring(url.lastIndexOf(".") + 1);
byte[] bytes = fastFileStorageClient.downloadFile(url.substring(0, url.indexOf("/")), url.substring(url.indexOf("/") + 1), new DownloadByteArray());
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(UUIDUtils.getUUID() +"."+substring, "UTF-8"));
// 写出
ServletOutputStream outputStream = response.getOutputStream();
IOUtils.write(bytes, outputStream);
}
4.测试
1.上传
浏览器访问
5.总结
FastDFS是一个开源的分布式文件存储系统,有两部分组成tracker(有负载均衡的作用)负责管理storage(可集群),如有问题欢迎留言,今儿就到这里吧。下期见。
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:
https://blog.csdn.net/weixin_41914928/article/details/112308501
猜你喜欢
- 2025-05-28 教你一招快速入门网络编程
- 2025-05-28 有了这些开源工具后,明天争取五点下班了!
- 2025-05-28 十年老架构师:带你读懂springmvc的世界!
- 2025-05-28 一招搞定外部请求,这款 HTTP 客户端框架真的很强大!
- 2025-05-28 一文搞定FastDFS的搭建和使用
- 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 应用指南
- 08-06中等生如何学好初二数学函数篇
- 08-06C#构造函数
- 08-06初中数学:一次函数学习要点和方法
- 08-06仓颉编程语言基础-数据类型—结构类型
- 08-06C++实现委托机制
- 08-06初中VS高中三角函数:从"固定镜头"到"360°全景",数学视野升级
- 08-06一文讲透PLC中Static和Temp变量的区别
- 08-06类三剑客:一招修改所有对象!类方法与静态方法的核心区别!
- 1522℃桌面软件开发新体验!用 Blazor Hybrid 打造简洁高效的视频处理工具
- 650℃Dify工具使用全场景:dify-sandbox沙盒的原理(源码篇·第2期)
- 527℃MySQL service启动脚本浅析(r12笔记第59天)
- 492℃服务器异常重启,导致mysql启动失败,问题解决过程记录
- 492℃启用MySQL查询缓存(mysql8.0查询缓存)
- 479℃「赵强老师」MySQL的闪回(赵强iso是哪个大学毕业的)
- 461℃mysql服务怎么启动和关闭?(mysql服务怎么启动和关闭)
- 460℃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函数和普通函数 (84)
- el-date-picker开始日期早于结束日期 (70)
- asynccallback (71)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- java (73)
- js数组插入 (83)
- mac安装java (72)
- 查看mysql是否启动 (70)
- 无效的列索引 (74)