参考
官方网站:https://github.com/happyfish100/
配置文档:https://github.com/happyfish100/fastdfs/wiki/
参考资料:https://www.oschina.net/question/tag/fastdfs
Java客户端:https://github.com/happyfish100/fastdfs-client-java
术语
fastDFS:
FastDFS是一款开源的轻量级分布式文件系统纯C实现,支持Linux、FreeBSD等UNIX系统类google FS,不是通用的文件系统,只能通过专有API访问,目前提供了C、Java和PHP API为互联网应用量身定做,解决大容量文件存储问题,追求高性能和高扩展性FastDFS可以看做是基于文件的key value pair存储系统,称作分布式文件存储服务更为合适。
------ ???????????来自官网介绍
tracker-server:
跟踪服务器, 主要做调度工作, 起负载均衡的作用。 在内存中记录集群中所有存储组和存储服务器的状态信息, 是客户端和数据服务器交互的枢纽。 相比GFS中的master更为精简, 不记录文件索引信息, 占用的内存量很少。
storage-server:
存储服务器( 又称:存储节点或数据服务器) , 文件和文件属性( metadata) 都保存到存储服务器上。 Storage server直接利用OS的文件系统调用管理文件。
group:
组, 也可称为卷。 同组内服务器上的文件是完全相同的 ,同一组内的storage server之间是对等的, 文件上传、 删除等操作可以在任意一台storage server上进行 。
meta data:
meta data:文件相关属性,键值对( Key Value Pair) 方式,如:width=1024,heigth=768 。
单机文件系统的对比
文件系统 | 高可用 | 扩展 | 部署复杂程度 | 性能 |
---|---|---|---|---|
单机文件系统 | 低,依赖于单机服务器,只要服务器崩溃,完全不可用。 | 低,要扩容只能停机增加硬盘。 | 低 | 当文件数量多到一定的程度,磁盘IO寻址操作将会成为瓶颈 |
分布式文件系统 | 高,一个group内的服务器崩溃后,group内的其他storage将接管服务。 | 高,可以不停机增加group机器。 | 高,部署较复杂 | 高,通过集群或者分布式的方式分担服务器的压力。 |
其他文件系统的对比
指标 | 适合类型 | 文件分布 | 系统性能 | 复杂度 | FUSE | POSIX | 备份机制 | 通讯协议接口 | 社区支持 | 开发语言 |
---|---|---|---|---|---|---|---|---|---|---|
FastDFS | 4KB~500MB | 小文件合并存储不分片处理 | 很高 | 简单 | 不支持 | 不支持 | 组内冗余备份 | Api HTTP | 国内用户群 | C语言 |
TFS | 所有文件 | 小文件合并,以block组织分片 | 复杂 | 不支持 | Block存储多份,主辅灾备 | API http | 少 | C++ | ||
MFS | 大于64K | 分片存储 | Master占内存多 | 支持 | 支持 | 多点备份动态冗余 | 使用fuse挂在 | 较多 | Perl | |
HDFS | 大文件 | 大文件分片分块存储 | 简单 | 支持 | 支持 | 多副本 | 原生api | 较多 | Java | |
Ceph | 对象文件块 | OSD一主多从 | 复杂 | 支持 | 支持 | 多副本 | 原生api | 较少 | C++ | |
MogileFS | 海量小图片 | 高 | 复杂 | 可以支持 | 不支持 | 动态冗余 | 原生api | 文档少 | Perl | |
ClusterFS | 大文件 | 简单 | 支持 | 支持 | 多 | C |
适用场景:
特别适合以中小文件( 建议范围: 4KB 到 500MB ) 为载体的在线服务, 如相册网站、 视频网站等等。
部署结构
适合本公司的最小化部署图
192.168.1.177安装fastdfs的tracker节点,以及nginx反向代理服务器用于下载服务。
192.168.1.188,192.168.1.189安装fastdfs的storage节点,默认分一组,一组内两台机器互为备份.
注意:为了做到高可用,一个group建议分为两台以上的机器。
tracker安装(192.168.1.177)
安装 libfastcommon、perl依赖,下载libfastcommon-master.zip,上传到服务器的/usr目录下。
https://github.com/happyfish100/libfastcommon?[root@localhost usr]# unzip libfastcommon-master.zip ?[root@localhost usr]# cd libfastcommon-master ?[root@localhost usr]# ./make.sh ?[root@localhost usr]# ./make.sh install ?[root@localhost usr]# yum install perl
编译安装fastdfs
?[root@localhost usr] # tar -zxvf fastdfs-5.08.tar.gz ?[root@localhost usr] # cd fastdfs-5.08 ?[root@localhost fastdfs-5.08] # ./make.sh ?[root@localhost fastdfs-5.08] # ./make.sh ?install ?[root@localhost fastdfs-5.08]# ./make.sh ?install ?mkdir -p /usr/bin ?mkdir -p /etc/fdfs ?cp -f fdfs_trackerd /usr/bin ?if [ ! -f /etc/fdfs/tracker.conf.sample ]; then cp -f ../conf/tracker.conf /etc/fdfs/tracker.conf.sample; fi ?mkdir -p /usr/bin ?mkdir -p /etc/fdfs ?cp -f fdfs_storaged ?/usr/bin ?if [ ! -f /etc/fdfs/storage.conf.sample ]; then cp -f ../conf/storage.conf /etc/fdfs/storage.conf.sample; fi ?mkdir -p /usr/bin ?mkdir -p /etc/fdfs ?mkdir -p /usr/lib64 ?mkdir -p /usr/lib ?cp -f fdfs_monitor fdfs_test fdfs_test1 fdfs_crc32 fdfs_upload_file fdfs_download_file fdfs_delete_file fdfs_file_info fdfs_appender_test fdfs_appender_test1 fdfs_append_file fdfs_upload_appender /usr/bin ?if [ 0 -eq 1 ]; then cp -f libfdfsclient.a /usr/lib64; cp -f libfdfsclient.a /usr/lib/;fi ?if [ 1 -eq 1 ]; then cp -f libfdfsclient.so /usr/lib64; cp -f libfdfsclient.so /usr/lib/;fi ?mkdir -p /usr/include/fastdfs ?cp -f ../common/fdfs_define.h ../common/fdfs_global.h ../common/mime_file_parser.h ../common/fdfs_http_shared.h ../tracker/tracker_types.h ../tracker/tracker_proto.h ../tracker/fdfs_shared_func.h ../storage/trunk_mgr/trunk_shared.h tracker_client.h storage_client.h storage_client1.h client_func.h client_global.h fdfs_client.h /usr/include/fastdfs ?if [ ! -f /etc/fdfs/client.conf.sample ]; then cp -f ../conf/client.conf /etc/fdfs/client.conf.sample; fi
如出现以上信息,说明安装成功。
fastDFS命令目录:/usr/bin
fastDFS配置文件目录:/etc/fdfs
创建fastdfs数据目录
?mkdir -pv /data/fastdfs/tracker
修改tracker.conf中的 base_path=/data/fastdfs/tracker,即刚才配置的数据文件目录
cp etc/fdfs/tracker.conf.sample etc/fdfs/tracker.conf vi ?etc/fdfs/tracker.conf
启动FASTDFS
?cd ?/usr/bin ?fdfs_trackerd /etc/fdfs/tracker.conf restart
验证,默认端口是22122
?[root@localhost bin]# netstat -antp|grep trackerd ?tcp ???????0 ?????0 0.0.0.0:22122 ??????????0.0.0.0:* ??????????????LISTEN ?????31046/fdfs_trackerd
storage安装(192.168.1.188,192.168.1.189)
storage的安装与tracker安装类似,首先编译安装fastdfs,然后修改配置文件。
创建配置文件
?cp etc/fdfs/storage.conf.sample etc/fdfs/storage.conf ?vi ?etc/fdfs/storage.conf
修改tracker.conf中的
?disabled=false #启用配置文件 ?group_name=group1 #组名,根据实际情况修改 ?port=23000 #设置 storage 的端口号 ?base_path=/data/fastdfs/storage #设置 storage 的日志目录(需预先创建) ?store_path_count=1 #存储路径个数,需要和 store_path 个数匹配 ?store_path0=/data/fastdfs/storage #存储路径 ?tracker_server=192.168.1.177:22122 #tracker 服务器的 IP 地址和端口号
启动
?cd /usr/bin ?./fdfs_storaged ?/etc/fdfs/storage.conf restart
java client
Javaclient的jar包不在maven的中央仓库中,需要下载源码包手工安装到私服中。
下载地址:https://github.com/happyfish100/fastdfs-client-java
我们主要关注storageclient这个类,这个类提供了文件上传下载查询文件信息等主要功能,与系统的集成也主要使用这个api。
新建fastDFS.conf配置文件
?connect_timeout = 2 ?network_timeout = 20 ?#tracker服务器地址,如果有多个tracker可以配置多个tracker_server ?tracker_server=192.168.1.177:22122
运行源码包中的org.csource.fastdfs.test.testClient.java,注意在main方法中需要传递两个参数,第一个是配置文件,第二个是待上传的文件
Paste_Image.png测试结果,已经能够通过Javaclient上传下载文件,但是通过http的方式下载还有问题。
Paste_Image.png
在storage节点上安装nginx,增加http下载支持
编译安装nginx,下载prce、zlib、fastdfs-nginx-module依赖支持,上传到storage的usr/local/src目录下。fastdfs-nginx-module主要用于解决同一组内文件同步不及时的问题。
Paste_Image.png解压缩
?cd ?/usr/local/src ?unzip ./zlib-master.zip ??tar ?-xvf ./nginx-1.11.7.tar.gz ??unzip ?./pcre-8.38.zip ?cd ./nginx-1.11.7 ?./configure --prefix=/usr/local/nginx --add-module=/usr/local/src/fastdfs-nginx-module-master/src --with-pcre=/usr/local/src/pcre-8.38 --with-zlib=/usr/local/src/zlib-master ?make && make install ??cp ../fastdfs-nginx-module-master/src/mod_fastdfs.conf ?/etc/fdfs/
修改nginx.conf
??server { ?????????listen ??????80; ?????????server_name ?localhost; ??????????location ~ /group1/M00{ ?????????????root /data/fastdfs/storage; ?????????????ngx_fastdfs_module; ?????????} ?}
配置mod_fastdfs.conf
?#保存日志目录 ?base_path=/data/fastdfs/storage ??#tracker 服务器的 IP 地址以及端口号 storage_server_port=23000 #storage 服务器的端口号 ?tracker_server=192.168.1.177:22122 ??#当前服务器的 group 名 ?group_name=group1 ?#文件 url 中是否有 group 名 ?url_have_group_name = true ?#存储路径个数,需要和 store_path 个数匹配 store_path0=/data/fastdfs/storage #存储路径 ?store_path_count=1 ??# 从文件 扩展 名查 找 文件 类型 ( nginx 时 为true) group_count = 2#设置组的个数 ?http.need_find_content_type=true ????[group1] ?group_name=group1 ?storage_server_port=23000 ?store_path_count=1 ?store_path0=/data/fastdfs/storage
安装nginx反向代理服务器(192.168.1.177)
安装nginx,安装过程参考上一步
配置nginx.conf
?upstream fdfs_group1 { ?????server 192.168.1.188 weight=1 max_fails=2 fail_timeout=30s; ?????#server 192.168.1.189 weight=1 max_fails=2 fail_timeout=30s; ?} ?server { ?????listen ??????80; ?????server_name ?localhost; ?????location /group1/M00 { ?????proxy_next_upstream http_502 http_504 error timeout invalid_header; ?????proxy_pass http://fdfs_group1; ?????expires 30d; ????} ?#其他配置 ?}
再次使用Javaclient测试
Paste_Image.pngPaste_Image.png
其他深入主题
- nginx反向代理服务器的缓存优化
- 扩容方案
- tracker节点的集群优化
作者:哥别打脸
链接:https://www.jianshu.com/p/1c71ae024e5e
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
fastdfs(https://www.jianshu.com/p/1c71ae024e5e)
原文地址:https://www.cnblogs.com/yipianchuyun/p/10261329.html