对于文件集中存储现在互联网行业主要有两种大的实现思路,基于简单hash实现,基于分布式文件系统实现(比如淘宝的TFS,Amozon的S3,google的GFS等等),下面主要讲解如何使用FastDFS来实现,架构图如下:
安装libevent
官方推荐稳定的1.4.14b版本的,2.x的可能有不稳定性
获取下载
wget https://github.com/downloads/libevent/libevent/libevent-1.4.14b-stable.tar.gz
解压并编译安装
tar zvxf libevent-1.4.14b-stable.tar.gz
./configure --prefix=/usr
make clean;
make
sudo make install
安装FastDFS
获取下载
wget http://fastdfs.googlecode.com/files/FastDFS_v4.04.tar.gz
解压并编译安装
tar zvxf FastDFS_v4.04.tar.gz
先修改相关文件:
1:修改 make.sh 查找:
#WITH_HTTPD=1
修改成:
WITH_HTTPD=1
查找:
if [ -f /usr/lib/libpthread.so ] || [ -f /usr/local/lib/libpthread.so ] || [ -f /lib64/libpthread.so ] ||
[ -f/usr/lib64/libpthread.so ] || [ -f /usr/lib/libpthread.a ] || [ -f /usr/local/lib/libpthread.a ] ||
[ -f /lib64/libpthread.a ] || [ -f /usr/lib64/libpthread.a ] ;
替换成:(以你服务器的实际路径为准)
if [ -f /usr/lib/libpthread.so ] || [ -f /usr/local/lib/libpthread.so ] || [ -f /lib64/libpthread.so ] ||
[ -f /usr/lib64/libpthread.so ] || [ -f /usr/lib/x86_64-linux-gnu/libpthread.so ] || [ -f /usr/lib/libpthread.a ] ||
[ -f /usr/local/lib/libpthread.a ] || [ -f /lib64/libpthread.a ] || [ -f /usr/lib64/libpthread.a ] || [ -f /usr/lib/x86_64-linux-gnu/libpthread.a ];
2:修改 client/fdfs_link_library.sh.in(如果是64位os,32位os无需修改) 查找:
ln -fs $TARGET_LIB/libfastcommon.so.1 /usr/lib64/libfastcommon.so ln -fs $TARGET_LIB/libfdfsclient.so.1 /usr/lib64/libfdfsclient.so 替换成: ln -fs $TARGET_LIB/libfastcommon.so.1 /usr/lib/x86_64-linux-gnu/libfastcommon.so
ln -fs $TARGET_LIB/libfdfsclient.so.1 /usr/lib/x86_64-linux-gnu/libfdfsclient.so
3:进入解压后目录,编译并安装:
vi make.sh
#WITH_LINUX_SERVICE=1 (如果需要修改为开机自启动,则去掉make.sh里的行注释WITH_LINUX_SERVICE=1)
sudo ./make.sh
sudo ./make.sh install
配置及启动Tracker Server
修改tracker.conf
FastDFS默认装完会在/etc/fdfs/下产生配置文件tracker.conf
port=22122 base_path=/www/geebook_tracker http.server_port=8090
#include http.conf
其它保持默认,注意上面那个是1个#,默认是2个#,去掉1个就行
启动tracker服务
sudo make /www/geebook_tracker
sudo fdfs_trackerd /etc/fdfs/tracker.conf
netstat –lnp –tcp 参看端口是否起来,默认如果显示22122和8090说明服务正常起来
配置及启动Storage Server
修改storage.conf
FastDFS默认装完会在/etc/fdfs/下产生配置文件storage.conf
port=23000
base_path=/www/geebook_storage(自定义路径)
#base_path,指定data和log所在目录,根目录必须存在,子目录会自动创建
store_path0=/www/geebook_storage
#文件的存储位置,在一台Storage Server上可以指定多个存储位置
group_name=group1
tracker_server=127.0.0.1:22122
http.server_port=8888
#include http.conf
启动storage服务
sudo mkdir /www/geebook_storage (创建上面提到的base_path目录)
sudo fdfs_storaged /etc/fdfs/storage.conf
netstat –lnp –tcp 参看端口是否起来,默认如果显示23000和8888说明服务正常起来
到此可以先将前面配置的FastDFS做简单的上传下载测试,没有问题之后继续进行下一步的与nginx整合配置
- 验证FastDFS上传下载
修改client.conf
FastDFS默认装完会在/etc/fdfs/下产生配置文件client.conf
base_path=/www/geebook_client
tracker_server=127.0.0.1:22122
http.tracker_server_port=8090
- 测试上传
fdfs_test /etc/fdfs/client.conf upload /etc/fstab(将etc下的fstab文件作为上传的测试文件)
得到信息如下:
Copyright (C) 2008, Happy Fish / YuQing FastDFS may be copied only under the terms of the GNU General Public License V3, which may be found in the FastDFS source kit. Please visit the FastDFS Home Page http://www.csource.org/ for more detail. [2012-12-27 14:27:01] DEBUG - base_path=/www/geebook_client, connect_timeout=30,
network_timeout=60, tracker_server_count=1, anti_steal_token=0, anti_steal_secret_key length=0 tracker_query_storage_store_list_without_group:server 1. group_name=group1, ip_addr=127.0.0.1, port=23000 group_name=group1, ip_addr=127.0.0.1, port=23000 storage_upload_by_filename group_name=group1, remote_filename=M00/00/00/OlPQG1Db6jWAFmbTAAADqdgNsks5096460 source ip address: 58.83.208.27 file timestamp=2012-12-27 14:27:01 file size=937 file crc32=3624776267 file url: http://127.0.0.1:8090/group1/M00/00/00/OlPQG1Db6jWAFmbTAAADqdgNsks5096460 storage_upload_slave_by_filename group_name=group1, remote_filename=M00/00/00/OlPQG1Db6jWAFmbTAAADqdgNsks5096460_big source ip address: 58.83.208.27 file timestamp=2012-12-27 14:27:01 file size=937 file crc32=3624776267 fileurl:http://127.0.0.1:8090/group1/M00/00/00/OlPQG1Db6jWAFmbTAAADqdgNsks5096460_big
直接访问http://127.0.0.1:8090/group1/M00/00/00/OlPQG1Db6jWAFmbTAAADqdgNsks5096460_big
正常说明上传成功,并且fastdfs正常运行 - 删除测试
/etc/fdfs/client.conf delete group1 M00/00/00/OlPQG1Db6jWAFmbTAAADqdgNsks5096460_big
This is FastDFS client test program v4.04 Copyright (C) 2008, Happy Fish / YuQing FastDFS may be copied only under the terms of the GNU General Public License V3, which may be found in the FastDFS source kit. Please visit the FastDFS Home Page http://www.csource.org/ for more detail. [2012-12-27 14:31:42] DEBUG - base_path=/www/geebook_client, connect_timeout=30,
network_timeout=60, tracker_server_count=1, anti_steal_token=0, anti_steal_secret_key length=0 storage=58.83.208.27:23000
delete file success
显示删除成功
到此测试结束
FastDFS和apache/nginx整合
因为FastDFS默认自带的http服务器性能不好, 所以一般建议用外置的apache或者nginx 来解决http下载,以应付大并发的情况 注意nginx扩展模块只支持GET和HEAD模式获取文件,需要开发那边配合修改程序
获取nginx和FastDFS的nginx扩展插件
wget http://fastdfs-nginx-module.googlecode.com/files/fastdfs-nginx-module_v1.13.tar.gz
wget http://nginx.org/download/nginx-1.2.6.tar.gz
解压并编译安装
备注:如果之前机器上安装了nginx,可以单独安装fastdfs-nginx-module模块,具体方法请参考:如何单独添加NGINX自定义模块
apt-get install libssl-dev zlib1g-dev libpcre3-dev
tar zvxf nginx-1.2.6.tar.gz
tar zvxf fastdfs-nginx-module_v1.13.tar.gz
cd nginx-1.2.6
./configure --prefix=/usr/local/nginx --with-http_gzip_static_module --add-module=/www/soft/fastdfs-nginx-module/src/
make
make install
修改相关配置文件
1.修改FastDFS的storage.conf
http.disabled=true
#关闭内置web server 其它保持不变,保存退出
2.修改nginx扩展模块的配置文件
cd ../fastdfs-nginx-module/src
cp mod_fastdfs.conf /etc/fdfs/
vi /etc/fdfs/mod_fastdfs.conf
base_path=/www/logs
#存放log的路径
tracker_server=127.0.0.1:22122
#指定tracker服务器及端口
url_have_group_name = true
#这个很重要,在URL中包含group名称
store_path0=/www/geebook_storage
#存储文件的路径
storage_server_port=23000
#与storage的配置端口保持一致
保存后退出
3.做M00的链接
ln -s /www/geebook_storage/data /www/geebook_storage/data/M00
4.修改nginx的配置文件
备注:如果nginx有其他服务器在用,在nginx.conf原有的基础上追加一个下面server的配置即可
vi /usr/local/nginx/conf/nginx.conf
server {
listen 8888;
location / {
root /www/geebook_storage/data;
index index.html index.htm;
}
location /group1/M00 {
root /www/geebook_storage/data;
ngx_fastdfs_module;
}
}
5.重启相关服务,验证整合是否成功
先重启storage服务:
/etc/init.d/fdfs_storaged start
然后再启动nginx,
注意顺序,否则会报端口占用的错误
/usr/local/nginx/sbin/nginx
查看端口使用情况:
netstat -lnp --tcp
tcp 0 0 0.0.0.0:23000 0.0.0.0:* LISTEN 1761/fdfs_storaged
tcp 0 0 0.0.0.0:8888 0.0.0.0:* LISTEN 1718/nginx
tcp 0 0 0.0.0.0:8090 0.0.0.0:* LISTEN 1809/fdfs_trackerd
tcp 0 0 0.0.0.0:22122 0.0.0.0:* LISTEN 1809/fdfs_trackerd
默认的8888端口本来是storage的,现在被nginx替代 完成了FastDFS和nginx的整合 用上面的fdfs_test程序测试上传和下载,能正常访问下载,说明nginx已经顺利替代 FastDFS的内置Web server。
未完待续。。。。配置Varnish进行加速Varnish配制方法