fastdfs 介绍
FastDFS是一个开源的分布式文件系统,它对文件进行管理,功能包括:文件存储、文件同步、文件访问(文件上传、文件下载)等,解决了大容量存储和负载均衡的问题。特别适合以文件为载体的在线服务,如相册网站、视频网站等等。
FastDFS服务端有两个角色:跟踪器(tracker)和存储节点(storage)。跟踪器主要做调度工作,在访问上起负载均衡的作用。
存储节点存储文件,完成文件管理的所有功能:存储、同步和提供存取接口,FastDFS同时对文件的meta data进行管理。所谓文件的meta data就是文件的相关属性,以键值对(key value pair)方式表示,如:width=1024,其中的key为width,value为1024。文件meta data是文件属性列表,可以包含多个键值对。
FastDFS系统结构如下图所示:
跟踪器和存储节点都可以由一台多台服务器构成。跟踪器和存储节点中的服务器均可以随时增加或下线而不会影响线上服务。其中跟踪器中的所有服务器都是对等的,可以根据服务器的压力情况随时增加或减少。
为了支持大容量,存储节点(服务器)采用了分卷(或分组)的组织方式。存储系统由一个或多个卷组成,卷与卷之间的文件是相互独立的,所有卷 的文件容量累加就是整个存储系统中的文件容量。一个卷可以由一台或多台存储服务器组成,一个卷下的存储服务器中的文件都是相同的,卷中的多台存储服务器起 到了冗余备份和负载均衡的作用。
在卷中增加服务器时,同步已有的文件由系统自动完成,同步完成后,系统自动将新增服务器切换到线上提供服务。
当存储空间不足或即将耗尽时,可以动态添加卷。只需要增加一台或多台服务器,并将它们配置为一个新的卷,这样就扩大了存储系统的容量。
FastDFS中的文件标识分为两个部分:卷名和文件名,二者缺一不可。
上传文件交互过程:
1. client询问tracker上传到的storage,不需要附加参数;
2. tracker返回一台可用的storage;
3. client直接和storage通讯完成文件上传。
FastDFS file download
下载文件交互过程:
1. client询问tracker下载文件的storage,参数为文件标识(卷名和文件名);
2. tracker返回一台可用的storage;
3. client直接和storage通讯完成文件下载。
需要说明的是,client为使用FastDFS服务的调用方,client也应该是一台服务器,它对tracker和storage的调用均为服务器间的调用。
一:安装FastDFS
1、首先安装libevent。
|
1
2
3
4
5
6
7
|
wget http://monkey.org/~provos/libevent-2.0.11-stable.tar.gztar -zxvf libevent-2.0.11-stable.tar.gzcd libevent-2.0.11-stable./configure --prefix=/usrmake cleanmakemake install |
2、安装FastDFS的步骤
|
1
2
3
4
5
6
|
wget http://fastdfs.googlecode.com/files/FastDFS_v3.03.tar.gztar -zxvf FastDFS_v3.03.tar.gzcd FastDFSvim make.sh 将#WITH_HTTPD=1 修改成WITH_HTTPD=1 以支持 http./make.sh./make.sh install |
二:FastDFS的配置
1、配置及启动Tracker Server
|
1
2
3
4
5
6
|
mkdir /var/www/fastdfsvi /etc/fdfs/tracker.confbase_path=/home/yuqing/fastdfs -> base_path=/var/www/fastdfshttp.server_port=8080 -> http.server_port=9090##include http.conf -> #include http.confreserved_storage_space = 4GB -> reserved_storage_space = 1GB |
|
1
2
|
cd /usr/local/binfdfs_trackerd /etc/fdfs/tracker.conf |
检查tracker是否启动成功,可以查看如下文件/var/www/fastdfs/logs/trackerd.log
[2011-10-21 14:29:44] INFO – FastDFS v3.03, base_path=/home/fastdfs,run_by_group=, run_by_user=, connect_timeout=30s, network_timeout=60s,port=22122, bind_addr=, max_connections=256, work_threads=4, store_lookup=2,store_group=, store_server=0, store_path=0, reserved_storage_space=1024MB,download_server=0, allow_ip_count=-1, sync_log_buff_interval=10s,check_active_interval=120s, thread_stack_size=64 KB,storage_ip_changed_auto_adjust=1, storage_sync_file_max_delay=86400s,storage_sync_file_max_time=300s, use_trunk_file=0, slot_min_size=256,slot_max_size=16 MB, trunk_file_size=64 MB[2011-10-21 14:29:44] INFO – HTTP supported: server_port=9090,default_content_type=application/octet-stream, anti_steal_token=0,token_ttl=0s, anti_steal_secret_key length=0, token_check_failcontent_type=, token_check_fail buff length=0, check_active_interval=30,check_active_type=tcp, check_active_uri=/status.html |
2、配置及启动Storage Server
|
1
2
3
4
5
6
7
8
|
mkdir /var/www/fastdfs/fdfs_storagecd /etc/fdfsvi storage.confbase_path=/home/yuqing/fastdfs -> /var/www/fastdfs/fdfs_storagestore_path0=/home/yuqing/fastdfs -> store_path0=/var/www/fastdfs/fdfs_storagegroup_name=group2tracker_server=192.168.209.121:22122 -> tracker_server=10.0.2.15:22122##include http.conf ->#include http.conf |
|
1
2
|
cd /usr/local/binfdfs_storaged /etc/fdfs/storage.conf |
接下来会出现很多mkdir data path,这是系统在创建数据目录。
三:测试及使用FastDFS
1、FastDFS之配置client
|
1
2
3
4
5
|
vi /etc/fdfs/client.confbase_path=/home/yuqing/fastdfs-> base_path=/var/www/fastdfstracker_server=192.168.209.121:22122 -> tracker_server=10.0.2.15:22122http.tracker_server_port=8080 ->http.tracker_server_port=9090##include http.conf ->#include http.conf |
2、测试上传文件
|
1
2
3
|
cd /usr/local/bintouch aa.phpfdfs_test /etc/fdfs/client.conf upload aa.php |
执行成功后会出现如下提示:
3、测试下载文件
在浏览器中,输入上图中的url地址, tracker server会自动重定向到存储文件的storage server,文件下载成功。至此,已经成功搭建了FastDFS,编写你自己的client来进行访问吧:
|
1
|
http://10.0.2.15:9090/group1/M00/00/00/CgACD0-NYt_VkxRqAAAAEIG-AGI255_big.php |
4、常规命令
|
1
2
3
4
5
6
|
#重启trackerfdfs_trackerd /etc/fdfs/tracker.conf#重启storagedfdfs_storaged /etc/fdfs/storage.conf#监控dfsfdfs_monitor /etc/fdfs/storage.conf |
四:PHP扩展
1.安装PHP扩展
|
1
2
3
4
5
6
|
cd php_clientapt-get install php5-devphpize./configuremake make install |
2.配置PHP扩展
|
1
2
3
4
5
|
vi fastdfs_client.iniextension = fastdfs_client.sofastdfs_client.tracker_group_count = 2fastdfs_client.tracker_group0 = /etc/fdfs/client.conffastdfs_client.tracker_group1 = /etc/fdfs/client.conf |
3.测试
|
1
|
php fastdfs_test.php |
4.PHP用例test.php
|
1
2
3
4
5
6
7
8
9
10
|
< ?php//所上传的文件$filename = "aa.php";//调用FastDFS类$fdfs = new FastDFS();//上传文件 $filename 是所上传的文件,html是上传后的更名后缀名为.html$file_info = $fdfs->storage_upload_by_filename($filename,'html');//输出上传文件目录和文件名echo $file_info['filename'];?> |
五:centos下安装PHP扩展
1.安装PHP扩展
|
1
2
3
4
|
cd php_client/usr/local/php/bin/phpize./configure --with-php-config=/usr/local/php/bin/php-config --with-fastdfs_clientmake && make install |
2.配置PHP扩展
|
1
2
3
4
5
6
7
8
9
10
11
|
vi php.iniextension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"extension = fastdfs_client.sofastdfs_client.base_path = /var/www/fastdfsfastdfs_client.connect_timeout = 2fastdfs_client.network_timeout = 60fastdfs_client.log_level = infofastdfs_client.log_filename =fastdfs_client.http.anti_steal_secret_key =fastdfs_client.tracker_group_count = 1fastdfs_client.tracker_group0 = /etc/fdfs/client.conf |




posted on
浙公网安备 33010602011771号