互联网中有海量的文件,比如电商网站有海量的图片文件,视频网站有海量的视频文件,如果使用传统的模式上传文件,肯定是不可取的。因此需要使用第三方服务器来存储图片 。

一.FastDFS简介

​ FastDFS 是用 c 语言编写的一款开源的分布式文件系统。FastDFS 为互联网量身定制,充分考虑了冗余备份、负载均衡、线性扩容等机制,并注重高可用、高性能等指标,使用 FastDFS很容易搭建一套高性能的文件服务器集群提供文件上传、下载等服务。

​ FastDFS 架构包括 Tracker server 和 Storage server。客户端请求 Tracker server 进行文件上传、下载,通过 Tracker server 调度最终由 Storage server 完成文件上传和下载。

​ Tracker server 作用是负载均衡和调度,通过 Tracker server 在文件上传时可以根据一些策略找到 Storage server 提供文件上传服务。可以将 tracker 称为追踪服务器或调度服务器。

​ Storage server 作用是文件存储,客户端上传的文件最终存储在 Storage 服务器上,Storageserver 没有实现自己的文件系统而是利用操作系统 的文件系统来管理文件。可以将storage称为存储服务器。

服务端两个角色:

Tracker:管理集群,tracker 也可以实现集群。每个 tracker 节点地位平等。收集 Storage 集群的状态。

Storage:实际保存文件 Storage 分为多个组,每个组之间保存的文件是不同的。每个组内部可以有多个成员, 组成员内部保存的内容是一样的,组成员的地位是一致的,没有主从的概念。

文件上传流程 :

文件下载流程:

二.FastDFS的入门

使用FastDFS上传文件,我们以图片的上传为例(ssm环境)

第一步:在Spring项目中导入FastDFS的坐标

<!-- 分布式文件系统 -->
<dependency>
<groupId>org.csource.fastdfs</groupId>
<artifactId>fastdfs</artifactId>
<version>1.2</version>
</dependency>
<!-- 文件上传组件 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>

第二步:导入FastDFS的配置(resources/config/fdfs_client.conf)

# connect timeout in seconds
# default value is 30s
connect_timeout=30 # network timeout in seconds
# default value is 30s
network_timeout=60 # the base path to store log files
base_path=/home/fastdfs # tracker_server can ocur more than once, and tracker_server format is
# "host:port", host can be hostname or ip address
tracker_server=192.168.25.133:22122 #standard log level as syslog, case insensitive, value list:
### emerg for emergency
### alert
### crit for critical
### error
### warn for warning
### notice
### info
### debug
log_level=info # if use connection pool
# default value is false
# since V4.05
use_connection_pool = false # connections whose the idle time exceeds this time will be closed
# unit: second
# default value is 3600
# since V4.05
connection_pool_max_idle_time = 3600 # if load FastDFS parameters from tracker server
# since V4.05
# default value is false
load_fdfs_parameters_from_tracker=false # if use storage ID instead of IP address
# same as tracker.conf
# valid only when load_fdfs_parameters_from_tracker is false
# default value is false
# since V4.05
use_storage_id = false # specify storage ids filename, can use relative or absolute path
# same as tracker.conf
# valid only when load_fdfs_parameters_from_tracker is false
# since V4.05
storage_ids_filename = storage_ids.conf #HTTP settings
http.tracker_server_port=80 #use "#include" directive to include HTTP other settiongs
##include http.conf

第三步:编写UploadController

/**
* 图片上传的处理器
* @author Mr.song
* @date 2019/06/09 20:07
*/
@RestController
public class UploadController {
// FastDFS服务器的ip地址
private String server_url = "http://192.168.25.133/"; @RequestMapping("/upload")
public Result uploadFile(MultipartFile uploadFile) {
//把file存到fastDFS上
try {
String fileName = uploadFile.getOriginalFilename();
//获取文件扩展名称
String exeName = fileName.substring(fileName.lastIndexOf(".") + 1);
//创建上传客户端(封装的工具,见第六步)
FastDFSClient client = new FastDFSClient("classpath:config/fdfs_client.conf");
//完成上传,获得fileId
String fileId = client.uploadFile(uploadFile.getBytes(), exeName);
//拼接文件的访问路径并返回到前端页面
String filePath = server_url + fileId;
return new Result(true, filePath);
} catch (Exception e) {
e.printStackTrace();
return new Result(false, "上传失败!");
}
}
}

第四步:在SpringMVC中添加文件解析器的配置

<!-- 配置文件上传解析器 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"></property>
<!-- 设定文件上传的最大值 5MB, 5*1024*1024 -->
<property name="maxUploadSize" value="5242880"></property>
</bean>

第五步:页面文件上传的方法(这里是AngularJS)

//在前端service层中

app.service("uploadService",function ($http) {
this.uploadFile=function () {
var formData = new FormData();//上传文件的数据模型
//第一个参数:相当于表单的name,第二个参数需和文件上传框的id一致
formData.append("uploadFile",file.files[0]);
return $http({
method:'post',
url:'../upload.do',
data:formData,
//文件上传,类型必须是undefined,因为默认是text/plain(有默认值angularJS就不处理了)
headers:{'Content-type':undefined},
transformRequest:angular.identity //对整个表单进行二进制序列化
})
}
})

第六步:编写FastDFS的简单封装工具

/**
* FastDFS使用的简单封装
*/
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);
}
}

关注微信公众号,随时随地学习

FastDFS的简单使用的更多相关文章

  1. 分布式文件系统 fastDFS 安装步骤

    安装 fastDFS 很简单. 先安装 libevent, 安装成功后,安装fastDFS. ./make.sh ./make.sh install 我使用一台tracker服务器  192.168. ...

  2. (转)分布式文件存储FastDFS(二)FastDFS安装

    http://blog.csdn.net/xingjiarong/article/details/50559761 在前面的一篇中,我们分析了FastDFS的架构,知道了FastDFS是由客户端,跟踪 ...

  3. 23.app后端如何架设文件系统

    现在app展现内容的形式多种多样的,有文字,图片,声音,视频等等,其中文件占了一个很大的比重.随着app不断运营,文件会越来越多,占用的磁盘空间也不断增大,架设一套高效的文件系统,对于整个app架构有 ...

  4. Hadoop小文件存储方案

    原文地址:https://www.cnblogs.com/ballwql/p/8944025.html HDFS总体架构 在介绍文件存储方案之前,我觉得有必要先介绍下关于HDFS存储架构方面的一些知识 ...

  5. SeaweedFS上手使用指南

    SeaweedFS是基于go语言开发的高可用文件存储系统,主要特性 1.成存储上亿的文件(最终受制于你的硬盘大小)2.速度快,内存占用小 上手使用比fastDFS要简单很多,自带Rest API. S ...

  6. 分布式文件系统 - FastDFS 简单了解一下

    别问我在哪里 也许我早已不是我自己,别问我在哪里,我一直在这里. 突然不知道说些什么了... 初识 FastDFS 记得那是我刚毕业后进入的第一家公司,一个技术小白进入到当时的项目组后,在开发中上传用 ...

  7. dubbo 图片服务器(FastDFS) redis solr ActiveMQ等简单配置使用

    一.dubbo 项目基于soa的架构,表现层和服务层是不同的工程.所以要实现商品列表查询需要两个系统之间进行通信. 1.1如何实现远程通信? 1.Webservice:效率不高基于soap协议.项目中 ...

  8. Nginx|构建简单的文件服务器(mac) 续-FastDFS安装(mac)|文件存储方案

    目录 Nginx|构建简单的文件服务器(mac) 1 所需安装包 2 安装fastdfs-nginx-module-master 3 安装Nginx Nginx|构建简单的文件服务器(mac) 续上文 ...

  9. 分布式文件存储:FastDFS简单使用与原理分析

    引言 FastDFS 属于分布式存储范畴,分布式文件系统 FastDFS 非常适合中小型项目,在我接手维护公司图片服务的时候开始接触到它,本篇文章目的是总结一下 FastDFS 的知识点. 用了 2 ...

随机推荐

  1. 【php】在Windows2003下的IIS配置php5.4

    本文与<[php]在Windows2003下配置Apache2.4与php5.4>(点击打开链接)为姊妹篇,仅仅是php所用的server有点不同,这里一个是Apache2.4,一个是Wi ...

  2. java实用技能 上传文件 等等

    1.IOS  AES对称加密,加密结果不同,问题解决 IOS http post请求,使用AFNetworing 框架,默认请求content-type为application/json ,所以无法使 ...

  3. Redhat Linux 下安装Oracle 11g R2

    能够下载:http://download.csdn.net/detail/ykh554541184/8086647文档方便查阅 官方文档:http://docs.oracle.com/cd/E1188 ...

  4. 浅谈UML学习笔记之用例图

    最近一直在学习UML的基础知识,再看完视频之后,并没有很好的总结,在画图的过程中发现了很多的问题,下面是看书的过程自己总结的UML用例图的一点知识,与大家分享一下. 一.概念 用例图是由参与者.用例以 ...

  5. XMU 1050 Diffuse Secret 【最短路】

    1050: Diffuse Secret Time Limit: 500 MS  Memory Limit: 64 MBSubmit: 10  Solved: 8[Submit][Status][We ...

  6. (转)JFreeChart教程

    JFreeChart教程 一.jFreeChart产生图形的流程 创建一个数据源(dataset)来包含将要在图形中显示的数据>>创建一个 JFreeChart 对象来代表要显示的图形&g ...

  7. P4844 LJJ爱数数 数论

    思路: 化简后得到(a+b)c=ab,设g=(a,b),A=a/g,B=b/g,则g(A+B)c=ABg^2,即(A+B)c=ABg 由题目已知条件:(a,b,c)=1,即(g,c)=1,g|(A+B ...

  8. CodeForces 722C Destroying Array (并查集)

    题意:给定 n 个数,然后每次破坏一个位置的数,那么剩下的连通块的和最大是多少. 析:用并查集来做,从后往前推,一开始什么也没有,如果破坏一个,那么我们就加上一个,然后判断它左右两侧是不是存在,如果存 ...

  9. Unity优化总览

    CPU GC 序列化与反序列化,如protobuff,json解析 String的频繁构造,拼接,如ToString()会生成字符串,Object.name会返回拷贝 闭包和匿名函数,在闭包中调用外部 ...

  10. bzoj 4519: [Cqoi2016]不同的最小割【最小割树Gomory–Hu tree】

    算法详见:http://www.cnblogs.com/lokiii/p/8191573.html 求出点两两之间的最小割之后,把他们扔到map/set里跑即可 可怕的是map和set跑的时间竟然完全 ...