FastDFS是由淘宝的余庆先生所开发,是一个轻量级、高性能的开源分布式文件系统,用纯C语言开发,包括文件存储、文件同步、文件访问(上传、下载)、存取负载均衡、在线扩容、相同内容只存储一份等功能,适合有大容量存储需求的应用或系统。做分布式系统开发时,其中要解决的一个问题就是图片、音视频、文件共享的问题,分布式文件系统正好可以解决这个需求。同类的分布式文件系统有谷歌的GFS、HDFS(Hadoop)、TFS(淘宝)等。
1、client询问tracker下载文件的storage,参数为文件标识(组名和文件名);
2、tracker返回一台可用的storage;
3、client直接和storage通讯完成文件下载。
术语
FastDFS两个主要的角色:Tracker Server 和 Storage Server Tracker Server:跟踪服务器,主要负责调度storage节点与client通信,在访问上起负载均衡的作用,和记录storage节点的运行状态,是连接client和storage节点的枢纽。 Storage Server:存储服务器,保存文件和文件的meta data(元数据) Group:文件组,也可以称为卷。同组内服务器上的文件是完全相同的,做集群时往往一个组会有多台服务器,上传一个文件到同组内的一台机器上后,FastDFS会将该文件即时同步到同组内的其它所有机器上,起到备份的作用。 meta data:文件相关属性,键值对(Key Value Pair)方式,如:width=1024, height=768。和阿里云OSS的meta data相似。
本人的能力:本人接触分布式系统,集群这些比较晚,大致只能理解到多台机器上多个引用协调运行,并能够保证数据流通正常的系统。目前只能理解这么多,个人习惯先看效果,不管咋样跑起来再说,喜欢从中间开始学习,兼顾理论。欢迎各位告诉我一个比较好的学习方式。当然可以指点我更好!!
1、下载软件 :http://nginx.org/download/ https://github.com/happyfish100/fastdfs (libfastcommon)
fastdfs-nginx-module_v1.16.tar.gz
FastDFS_v5.05.tar.gz
libfastcommon-master.zip
nginx-1.8.0.tar.gz
1、安装 libfastcommon-master.zip(默认安装)
lxj@Lenovo:/opt/FastDFS$ sudo cp ~/下载/libfastcommon-master.zip .
lxj@Lenovo:/opt/FastDFS$ sudo cp ~/下载/libfastcommon-master.zip .
lxj@Lenovo:/opt/FastDFS$ sudo unzip libfastcommon-master.zip
lxj@Lenovo:/opt/FastDFS$ cd libfastcommon-master/
lxj@Lenovo:/opt/FastDFS/libfastcommon-master$ ll
lxj@Lenovo:/opt/FastDFS/libfastcommon-master$ sudo ./make.sh
libfastcommon.so 默认安装到了/usr/lib64/libfastcommon.so,而FastDFS主程序设置的lib目录是/usr/local/lib,因此需要设置软链接。
sudo ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
sudo ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so
2、安装FastDFS
解压:
lxj@Lenovo:/opt/FastDFS$ sudo tar -zxvf fastdfs-5.05.tar.gz
编译安装(默认安装)
lxj@Lenovo:/opt/FastDFS$ cd fastdfs-5.05/
lxj@Lenovo:/opt/FastDFS/fastdfs-5.05$ sudo ./make.sh
lxj@Lenovo:/opt/FastDFS/fastdfs-5.05$ sudo ./make.sh install
可以看家/etc/fdfs下面多了几个文件
3、配置tracker目录节点
创建tracker的数据文件和日志文件目录
lxj@Lenovo:/opt/FastDFS/data$ sudo mkdir fdfs_tracker
lxj@Lenovo:/opt/FastDFS/data$ cd fdfs_tracker/
lxj@Lenovo:/opt/FastDFS/data/fdfs_tracker$ pwd
/opt/FastDFS/data/fdfs_tracker
lxj@Lenovo:/opt/FastDFS/data/fdfs_tracker$ cd ..
lxj@Lenovo:/opt/FastDFS/data$ cd ..
lxj@Lenovo:/opt/FastDFS$ sudo chmod 777 data/ -R
lxj@Lenovo:/opt/FastDFS$ ll
修改tracker配置文件(简单配置不考虑优化)
# the base path to store data and log files
base_path=/opt/FastDFS/data/fdfs_tracker
# HTTP port on this tracker server
http.server_port=6666
创建软链接
lxj@Lenovo:/etc/fdfs$ sudo ln -s /usr/bin/fdfs_trackerd /usr/local/bin
lxj@Lenovo:/etc/fdfs$ sudo ln -s /usr/bin/stop.sh /usr/local/bin
lxj@Lenovo:/etc/fdfs$ sudo ln -s /usr/bin/restart.sh /usr/local/bin
启动:
方式1
lxj@Lenovo:/usr/local/bin$ fdfs_trackerd /etc/fdfs/tracker.conf
关闭:
lxj@Lenovo:/usr/local/bin$ stop.sh fdfs_trackerd /etc/fdfs/tracker.conf
stopping fdfs_trackerd ...
.
方式2
# service fdfs_trackerd start #启动tracker节点
这总方式我没有成功过,提示:
Failed to stop fdfs_trackerd.service: Unit fdfs_trackerd.service not loaded.不明觉厉,没有想到好的解决方案。哎还是太菜了。
生成日志文件:
4、配置storage节点:
创建数据文件和日志文件
lxj@Lenovo:/opt/FastDFS/data$ sudo mkdir fdfs_storage
lxj@Lenovo:/opt/FastDFS/data$ cd fdfs_storage/
lxj@Lenovo:/opt/FastDFS/data/fdfs_storage$ pwd
/opt/FastDFS/data/fdfs_storage
编辑storage.conf文件
lxj@Lenovo:/etc/fdfs$ sudo vi storage.conf
group_name=group1 #组名,可根据实际情况修改
base_path=/opt/FastDFS/data/fdfs_storage #设置storage数据文件和日志目录,需预先创建
store_path_count=1 #存储路径个数,需要和 store_path 个数匹配、
store_path0=/opt/FastDFS/data/fdfs_storage #存储路径
tracker_server=192.168.116.145:22122 # #tracker 服务器的 IP地址和端口号,如果是单机搭建,IP不要写127.0.0.1,否则启动不成功。
http.server_port=8888 #设置 http 端口号
建立软链接
sudo ln -s /usr/bin/fdfs_storaged /usr/local/bin
启动:
lxj@Lenovo:/usr/local/bin$ fdfs_storaged /etc/fdfs/storage.conf
日志文件内容:
启动成功后可运行:
/usr/bin/fdfs_monitor /etc/fdfs/storage.conf查看是否登记到tracker服务器中。
[2018-04-26 21:35:48] DEBUG - base_path=/opt/FastDFS/data/fdfs_storage, connect_timeout=30, network_timeout=60, tracker_server_count=1, anti_steal_token=0, anti_steal_secret_key length=0, use_connection_pool=0, g_connection_pool_max_idle_time=3600s, use_storage_id=0, storage server id count: 0
server_count=1, server_index=0
tracker server is 192.168.1.118:22122
group count: 1
Group 1:
group name = group1
disk total space = 92782 MB
disk free space = 63765 MB
trunk free space = 0 MB
storage server count = 1
active server count = 1
storage server port = 23000
storage HTTP port = 8888
store path count = 1
subdir count per path = 256
current write server index = 0
current trunk file id = 0
Storage 1:
id = 192.168.1.118
ip_addr = 192.168.1.118 ACTIVE
http domain =
version = 5.05
join time = 2018-04-26 21:31:35
up time = 2018-04-26 21:31:35
total storage = 92782 MB
free storage = 63765 MB
upload priority = 10
store_path_count = 1
subdir_count_per_path = 256
storage_port = 23000
storage_http_port = 8888
current_write_path = 0
source storage id =
if_trunk_server = 0
connection.alloc_count = 256
connection.current_count = 0
connection.max_count = 0
total_upload_count = 0
success_upload_count = 0
total_append_count = 0
success_append_count = 0
total_modify_count = 0
success_modify_count = 0
total_truncate_count = 0
success_truncate_count = 0
total_set_meta_count = 0
success_set_meta_count = 0
total_delete_count = 0
success_delete_count = 0
total_download_count = 0
success_download_count = 0
total_get_meta_count = 0
success_get_meta_count = 0
total_create_link_count = 0
success_create_link_count = 0
total_delete_link_count = 0
success_delete_link_count = 0
total_upload_bytes = 0
success_upload_bytes = 0
total_append_bytes = 0
success_append_bytes = 0
total_modify_bytes = 0
success_modify_bytes = 0
stotal_download_bytes = 0
success_download_bytes = 0
total_sync_in_bytes = 0
success_sync_in_bytes = 0
total_sync_out_bytes = 0
success_sync_out_bytes = 0
total_file_open_count = 0
success_file_open_count = 0
total_file_read_count = 0
success_file_read_count = 0
total_file_write_count = 0
success_file_write_count = 0
last_heart_beat_time = 2018-04-26 21:35:20
last_source_update = 1970-01-01 08:00:00
last_sync_update = 1970-01-01 08:00:00
last_synced_timestamp = 1970-01-01 08:00:00
配置防火墙
sudo ufw status
状态是不开启,所以懒得配置了,如果要在Ubuntu下配置,可以参考:
http://www.veryhuo.com/a/view/16976.html
FastDFS下防火墙设置可参考:
https://blog.csdn.net/xyang81/article/details/52928230
上传测试:
修改客户端设置:
# the base path to store log files
base_path=/opt/FastDFS/data/fdfs_tracker
# 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.1.118:22122
执行上传命令:
/usr/bin/fdfs_upload_file /etc/fdfs/client.conf ~/下载/redis教案.docx
返回id号:
group1/M00/00/00/wKgBdlrh2-SAERaOAAom8eOuasY73.docx
5、存储节点安装Nginx和fastdfs-nginx-module模块
下载:
Nginx:http://nginx.org/download/下载nginx
fastdfs-nginx-module:下载地址:http://nchc.dl.sourceforge.net/project/fastdfs/FastDFS%20Nginx%20Module%20Source%20Code/fastdfs-nginx-module_v1.16.tar.gz
解压:
sudo tar -zxvf nginx-1.8.0.tar.gz
安装:
sudo ./configure --prefix=/opt/nginx –add-module=/opt/FastDFS/fastdfs-nginx-module/src
报错:没有pcre library
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=
需要安装如下的库:
1)PCRE库
PCRE库支持正则表达式。如果我们在配置文件nginx.conf中使用了正则表达式,那么在编译Nginx时就必须把PCRE库编译进Nginx,因为Nginx的HTTP模块需要靠它来解析正则表达式。另外,pcre-devel是使用PCRE做二次开发时所需要的开发库,包括头文件等,这也是编译Nginx所必须使用的。可以这样安装:
sudo apt-get install libpcre3 libpcre3-dev
2)zlib库
zlib库用于对HTTP包的内容做gzip格式的压缩,如果我们在nginx.conf中配置了gzip on,并指定对于某些类型(content-type)的HTTP响应使用gzip来进行压缩以减少网络传输量,则在编译时就必须把zlib编译进Nginx。zlib-devel是二次开发所需要的库。可以这样安装
sudo apt-get install zlib1g-dev
3)OpenSSL库
如果服务器不只是要支持HTTP,还需要在更安全的SSL协议上传输HTTP,那么需要拥有OpenSSL。另外,如果我们想使用MD5、SHA1等散列函数,那么也需要安装它。可以这样安装:
sudo apt-get install openssl libssl-dev
make报错:
-o objs/addon/src/ngx_http_fastdfs_module.o \
/opt/FastDFS/fastdfs-nginx-module/src/ngx_http_fastdfs_module.c
In file included from /opt/FastDFS/fastdfs-nginx-module/src/ngx_http_fastdfs_module.c:6:0:
/opt/FastDFS/fastdfs-nginx-module/src/common.c:21:25: fatal error: fdfs_define.h: 没有那个文件或目录
compilation terminated.
objs/Makefile:1098: recipe for target 'objs/addon/src/ngx_http_fastdfs_module.o' failed
make[1]: *** [objs/addon/src/ngx_http_fastdfs_module.o] Error 1
make[1]: Leaving directory '/opt/FastDFS/nginx-1.8.0'
Makefile:8: recipe for target 'build' failed
make: *** [build] Error 2
执行一下命令:(不明白啥意思,哈哈)
# ln -sv /usr/include/fastcommon /usr/local/include/fastcommon
# ln -sv /usr/include/fastdfs /usr/local/include/fastdfs
拷贝相关文件到/etc/fdfs目录下:
lxj@Lenovo:/opt/FastDFS/fastdfs-nginx-module/src$ sudo cp mod_fastdfs.conf /etc/fdfs/
lxj@Lenovo:/opt/FastDFS/fastdfs-5.05/conf$ sudo cp mime.types /etc/fdfs/
lxj@Lenovo:/opt/FastDFS/fastdfs-5.05/conf$ sudo cp http.conf /etc/fdfs/
lxj@Lenovo:/opt/FastDFS/fastdfs-5.05/conf$ sudo cp anti-steal.jpg /etc/fdfs/
修改Nginx配置文件:
server {
listen 8888;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location ~/group[1-3]/M00 {
root /opt/FastDFS/data/fdfs_storage/data;
ngx_fastdfs_module;
}
修改/etc/fdfs/mod_fastdfs.conf的配置:
base_path=/opt/FastDFS/data/fdfs_storage #保存日志目录
tracker_server=192.168.1.118:22122 #tracker 服务器的 IP 地址以及端口号
url_have_group_name = true #文件 url 中是否有 group 名
store_path0=/data/fastdfs-storage # 存储路径
http.need_find_content_type=true # 从文件扩展名查找文件类型 (nginx 时 为true)
group_count = 3 #设置组的个数,事实上这次只使用了group1
在文件末添加如下内容:
[group1]
group_name=group1
storage_server_port=23000
store_path_count=1
store_path0=/opt/FastDFS/data/fdfs_storage
[group2]
group_name=group2
storage_server_port=23000
store_path_count=1
store_path0=/data/fastdfs-storage
建立 M00 至存储目录的符号连接:
sudo ln -s /opt/FastDFS/data/fdfs_storage/data /opt/FastDFS/data/fdfs_storage/data/M00
启动nginx
lxj@Lenovo:/opt/nginx/sbin$ ./nginx
error while loading shared libraries: libfdfsclient.so: cannot open shared object file: No such file or directory
打开之前安装的libfastcommon
发现里面有两个.so文件:
猜测是此处的问题
重新加上软链接:
sudo ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so
sudo ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so
再启动nginx:
lxj@Lenovo:/opt/nginx/sbin$ ./nginx
ngx_http_fastdfs_set pid=9524
上传测试:
group1/M00/00/00/wKgBbVrjAfGAMIP-AAA8-a72Mtc71.jpeg
启动:
tracker
方式1
lxj@Lenovo:/usr/local/bin$ fdfs_trackerd /etc/fdfs/tracker.conf
关闭:
lxj@Lenovo:/usr/local/bin$ stop.sh fdfs_trackerd /etc/fdfs/tracker.conf
stopping fdfs_trackerd ...
.
storage
启动:
lxj@Lenovo:/usr/local/bin$ fdfs_storaged /etc/fdfs/storage.conf
查看活跃状态:
/usr/bin/fdfs_monitor /etc/fdfs/storage.conf查看是否登记到tracker服务器中。
启动nginx
/opt/nginx/sbin/nginx
上传测试:
执行上传命令:
/usr/bin/fdfs_upload_file /etc/fdfs/client.conf ~/下载/redis教案.docx
20180427
group1/M00/00/00/wKgBbVrjAfGAMIP-AAA8-a72Mtc71.jpeg
group1/M00/00/00/wKgBdFru8zuARzIhAAA8-a72Mtc16.jpeg
访问:
http://192.168.1.124:8888/group1/M00/00/00/wKgBdlrh8-WAYlezAAA8-a72Mtc19.jpeg
好了,折腾半天总算把它给正主来了,不容易啊!!