为什么要有分布式文件系统
- 单机
- 优点:文件访问方便,项目直接使用,实现简单
- 缺点:文件和代码耦合,文件越多存放越混乱。流量大的时候,静态文件访问会占据一定的资源,影响正常业务进行
- 独立文件服务器
- 优点:独立存储,方便扩容,容灾和数据迁移,方便做图片等资源请求的负载均衡,方便应用各种缓存策略,也更方便迁移到CDN服务
- 缺点:单机存在性能瓶颈,容灾、垂直扩展性较差
- 分布式文件系统
- 优点:扩展能力好;高可用;弹性存储
- 缺点:系统复杂度高、需要更多服务器
分布式存储解决方案对比
指标 | 适合类型 | 文件分布 | 复杂度 | FUSE | POSIX | 备份机制 | 通讯协议接口 | 社区支持 | 开发语言 |
---|---|---|---|---|---|---|---|---|---|
FastDFS | 4K-500M | 中小文件存储、不分片处理 | 简单 | 不支持 | 不支持 | 组内冗余备份 | API、Http | 国内用户群 | C |
HDFS | 大文件 | 大文件分片分块存储 | 简单 | 支持 | 支持 | 多副本 | 原生API | 较多 | Java |
FastDFS特性
- 分组存储,灵活简洁,对等结构,不存在单点问题
- 文件不分块存储,上传的文件和OS文件系统的文件一一对应
- 文件ID由FastDFS生成,作为文件访问凭证,FastDFS不需要传统的命名服务
- 和流行的web server 无缝衔接,FastDFS提供了apache和nginx扩展模块
- 中小文件均可以很好的支持,支持海量小文件存储
- 支持多块磁盘,支持单磁盘数据恢复
- 支持相同内容的文件只保存一份,节约磁盘空间
- 支持在线扩容、支持主从文件
- 存储服务器上可以保存文件属性(meta-data)V2.0网络通信采用libevent,支持大并发访问,整体性能更好
- 下载文件支持多线程方式,支持断点续传
FastDFS的构成
Linux下FastDFS安装
可以参考作者的wiki
- 1.安装编译环境
yum install git gcc gcc-c++ make automake vim wget libevent -y
- 2.安装libfastcommon基础库
> mkdir /root/fastdfs
> cd /root/fastdfs
> git clone https://github.com/happyfish100/libfastcommon.git --depth 1
> cd libfastcommon/
> ./make.sh && ./make.sh install
- 3.安装FastDFS
> cd /root/fastdfs
> wget https://github.com/happyfish100/fastdfs/archive/V5.11.tar.gz
> tar -zxvf V5.11.tar.gz
> cd fastdfs-5.11
> ./make.sh && ./make.sh install
#配置文件准备
> cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf
> cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf
> cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf
> cp /root/fastdfs/fastdfs-5.11/conf/http.conf /etc/fdfs
> cp /root/fastdfs/fastdfs-5.11/conf/mime.types /etc/fdfs
# 修改tracker.conf配置文件
> vim /etc/fdfs/tracker.conf
#需要修改的内容如下
port=22122
base_path=/home/fastdfs
# 修改storage.conf配置文件
> vim /etc/fdfs/storage.conf
#需要修改的内容如下
port=23000
# 数据和日志文件存储根目录
base_path=/home/fastdfs
# 第一个存储目录
store_path0=/home/fastdfs
# 写上自己服务器的地址
tracker_server=192.168.56.119:22122
# http访问文件的端口(默认8888,看情况修改,和nginx中保持一致)
http.server_port=8888
- 4.启动
> mkdir /home/fastdfs -p
> /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
> /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
# 查看所有运行的端口
> netstat -ntlp
- 5.测试上传
# 修改client.conf 配置文件
vim /etc/fdfs/client.conf
#需要修改的内容如下
base_path=/home/fastdfs
#tracker服务器IP和端口
tracker_server=192.168.56.119:22122
# 保存后测试,返回ID表示成功
[root@localhost fastdfs]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf OOP.png
group1/M00/00/00/wKg4eF8qZ8aAMt5TAAIxAbCC66s414.png
-
6.安装fastdfs-nginx-moudle
-
下载并安装fastdfs-nginx-moudle
> cd /root/fastdfs
> wget https://github.com/happyfish100/fastdfs-nginx-module/archive/V1.20.tar.gz
# 解压
> tar -xvf V1.20.tar.gz
# 解压之后多了一个fastdfs-nginx-module-1.20文件夹
> cd fastdfs-nginx-module-1.20/src
# 修改config配置文件如下
ngx_module_incs="/usr/include/fastdfs /usr/include/fastcommon/"
CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"
# 把mod_fastdfs.conf配置文件拷贝到/etc/fdfs/文件夹
> cp mod_fastdfs.conf /etc/fdfs/
# 修该拷贝的配置文件 vim/etc/fdfs/mod_fastdfs.conf
> vim/etc/fdfs/mod_fastdfs.conf
# 需要修改下面三个地方
tracker_server=192.168.56.120:22122
url_have_group_name = true
store_path0=/home/fastdfs
# 创建目录
> mkdir -p /var/temp/nginx/client
- 7.安装Nginx
这个主要就是把FastDFS文件系统中的路径,可以通过Nginx,用Http方式访问
> cd /root/fastdfs
> wget http://nginx.org/download/nginx-1.15.6.tar.gz
> tar -zxvf nginx-1.15.6.tar.gz
> cd nginx-1.15.6/
# 因为最开始的时候yum install的时候不全,需要再安装一下环境
> yum -y install pcre-devel openssl openssl-devel
# 添加fastdfs-nginx-module模块
> ./configure --add-module=/root/fastdfs/fastdfs-nginx-module-1.20/src
# 编译安装
> make && make install
# 查看模块是否安装上
> /usr/local/nginx/sbin/nginx -V
# 修改Nginx配置文件
> vim /usr/local/nginx/conf/nginx.conf
# 在nginx.conf配置文件中添加如下配置
server {
listen 8888;
server_name localhost;
location ~/group[0-9]/ {
ngx_fastdfs_module;
}
}
# 启动nginx
> /usr/local/nginx/sbin/nginx
- 8.测试通过http访问
# 关闭防火墙
> systemctl stop firewalld
# 访问 http://192.168.56.120:8888/group1/M00/00/00/wKg4eF8qZ8aAMt5TAAIxAbCC66s414.png
java访问FastDFS
- 代码地址:fastdfs_java_demo