图片服务器(FastDFS)的搭建

1.1 什么是FastDFS

FastDFS是用c语言编写的一款开源的分布式文件系统。FastDFS为互联网量身定制,充分考虑了冗余备份、负载均衡、线性扩容等机制,并注重高可用、高性能等指标,使用FastDFS很容易搭建一套高性能的文件服务器集群提供文件上传、下载等服务。

1.2 文件上传流程

1.3 文件下载流程

1.4 上传文件的文件名

客户端上传文件后存储服务器文件ID返回给客户端,此文件ID用于以后访问该文件的索引信息。文件索引信息包括:组名,虚拟磁盘路径,数据两级目录,文件名。

组名:文件上传后所在的storage组名称,在文件上传成功后有storage服务器返回,需要客户端自行保存。

n 虚拟磁盘路径:storage配置的虚拟路径,与磁盘选项store_path*对应。如果配置了store_path0则是M00,如果配置了store_path1则是M01,以此类推。

n 数据两级目录:storage服务器在每个虚拟磁盘路径下创建的两级目录,用于存储数据文件。

文件名:与文件上传时不同。是由存储服务器根据特定信息生成,文件名包含:源存储服务器IP地址、文件创建时间戳、文件大小、随机数和文件拓展名等信息。

1.5 FastDFS搭建

可以使用一台虚拟机来模拟,只有一个Tracker、一个Storage服务。

配置nginx访问图片。

1.5.1 搭建步骤

第一步:把fastDFS都上传到linux系统。

第二步:安装FastDFS之前,先安装libevent工具包。

yum -y install libevent

第三步:安装libfastcommonV1.0.7工具包。

1、解压缩

2、./make.sh

3、./make.sh install

4、/usr/lib64/libfastcommon.so文件向/usr/lib/下复制一份

第四步:安装Tracker服务。

1、解压缩

2、./make.sh

3、./make.sh install

安装后在/usr/bin/目录下有以fdfs开头的文件都是编译出来的。

配置文件都放到/etc/fdfs文件夹

4、/root/FastDFS/conf目录下的所有的配置文件都复制到/etc/fdfs下。

5、配置tracker服务。修改/root/FastDFS/conf/tracker.conf文件。

6、启动tracker/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf

重启使用命令:/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart

第五步:安装storage服务。

1、如果是在不同的服务器安装,第四步的1~4需要重新执行。

2、配置storage服务。修改/root/FastDFS/conf/storage.conf文件

 

3、启动storage服务。

/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart

第六步:测试服务。

1、修改配置文件/etc/fdfs/client.conf

2、测试

/usr/bin/fdfs_test /etc/fdfs/client.conf upload anti-steal.jpg

第七步:搭建nginx提供http服务。

可以使用官方提供的nginx插件。要使用nginx插件需要重新编译。

fastdfs-nginx-module_v1.16.tar.gz

1、解压插件压缩包

2、修改/root/fastdfs-nginx-module/src/config文件,把其中的local去掉。

 

3、nginx重新config

./configure \

--prefix=/usr/local/nginx \

--pid-path=/var/run/nginx/nginx.pid \

--lock-path=/var/lock/nginx.lock \

--error-log-path=/var/log/nginx/error.log \

--http-log-path=/var/log/nginx/access.log \

--with-http_gzip_static_module \

--http-client-body-temp-path=/var/temp/nginx/client \

--http-proxy-temp-path=/var/temp/nginx/proxy \

--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \

--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \

--http-scgi-temp-path=/var/temp/nginx/scgi \

--add-module=/root/fastdfs-nginx-module/src

2、make

3、make install

4、/root/fastdfs-nginx-module/src/mod_fastdfs.conf文件复制到/etc/fdfs目录下。编辑:

 

 

1、nginx的配置

nginx的配置文件中添加一个Server

server {

        listen       80;

        server_name  192.168.101.3;

 

        location /group1/M00/{

                #root /home/FastDFS/fdfs_storage/data;

                ngx_fastdfs_module;

        }

}

2、libfdfsclient.so拷贝至/usr/lib

cp /usr/lib64/libfdfsclient.so /usr/lib/

3、启动nginx

nginx 400 Bad Request,详情可参考以下链接:

[root@web2 ~]# vim /etc/fdfs/mod_fastdfs.conf 
url_have_group_name = false 改为 true      #关于啥意思,配置文件中有解释
解决方法

 http://www.it165.net/admin/html/201308/1628.html

  1 fdfs_upload_file 
  2 Usage: fdfs_upload_file <config_file> <local_filename> [storage_ip:port] [store_path_index]
  3 
  4 上传文件:
  5 [root@gmxfjr-dev17 ~]# fdfs_upload_file /etc/fdfs/client.conf /etc/passwd
  6 group1/M00/00/00/Co91HFlbq2WAQvMDAAAImyi5gXI5136722
  7 
  8 下载文件:
  9 [root@gmxfjr-dev17 ~]# fdfs_download_file /etc/fdfs/client.conf group1/M00/00/00/Co91HFlbq2WAQvMDAAAImyi5gXI5136722
 10 
 11 显示文件信息
 12 [root@gmxfjr-dev17 ~]# fdfs_file_info /etc/fdfs/client.conf group1/M00/00/00/Co91HFlbq2WAQvMDAAAImyi5gXI5136722
 13 source storage id: 0
 14 source ip address: 10.143.117.28
 15 file create timestamp: 2017-07-04 22:51:17
 16 file size: 2203
 17 file crc32: 683245938 (0x28B98172)
 18 
 19 删除文件:
 20 [root@gmxfjr-dev17 ~]# fdfs_delete_file /etc/fdfs/client.conf group1/M00/00/00/Co91HFlbq2WAQvMDAAAImyi5gXI5136722
 21 
 22 文件追加
 23 [root@gmxfjr-dev17 ~]# echo "hello" >> append.txt 
 24 [root@gmxfjr-dev17 ~]# echo "world" >> append2.txt
 25 [root@gmxfjr-dev17 ~]# fdfs_upload_appender /etc/fdfs/client.conf append.txt 
 26 group1/M00/00/00/Co91G1lbrbGEL3ShAAAAAHcc3SA110.txt
 27 [root@gmxfjr-dev17 ~]# fdfs_download_file /etc/fdfs/client.conf group1/M00/00/00/Co91G1lbrbGEL3ShAAAAAHcc3SA110.txt
 28 [root@gmxfjr-dev17 ~]# cat Co91G1lbrbGEL3ShAAAAAHcc3SA110.txt
 29 hello
 30 [root@gmxfjr-dev17 ~]# fdfs_append_file /etc/fdfs/client.conf group1/M00/00/00/Co91G1lbrbGEL3ShAAAAAHcc3SA110.txt append2.txt 
 31 [root@gmxfjr-dev17 ~]# fdfs_download_file /etc/fdfs/client.conf group1/M00/00/00/Co91G1lbrbGEL3ShAAAAAHcc3SA110.txt
 32 [root@gmxfjr-dev17 ~]# cat Co91G1lbrbGEL3ShAAAAAHcc3SA110.txt
 33 hello
 34 world
 35 [root@gmxfjr-dev17 ~]# 
 36 
 37 健康状况
 38 [root@gmxfjr-dev17 ~]# fdfs_monitor 
 39 Usage: fdfs_monitor <config_file> [-h <tracker_server>] [list|delete|set_trunk_server <group_name> [storage_id]]
 40 [root@gmxfjr-dev17 ~]# fdfs_monitor /etc/fdfs/client.conf
 41 [2017-07-04 23:06:07] DEBUG - base_path=/data/fastdfs/fastdfs/client, connect_timeout=30, network_timeout=60, tracker_server_count=2, 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
 42 
 43 server_count=2, server_index=1
 44 
 45 tracker server is 10.143.117.28:22122
 46 
 47 group count: 1
 48 
 49 Group 1:
 50 group name = group1
 51 disk total space = 80505 MB
 52 disk free space = 54857 MB
 53 trunk free space = 0 MB
 54 storage server count = 2
 55 active server count = 2
 56 storage server port = 23000
 57 storage HTTP port = 8888
 58 store path count = 1
 59 subdir count per path = 256
 60 current write server index = 0
 61 current trunk file id = 0
 62 
 63     Storage 1:
 64         id = 10.143.117.27
 65         ip_addr = 10.143.117.27 (localhost)  ACTIVE
 66         http domain = 
 67         version = 5.08
 68         join time = 2017-06-25 10:40:23
 69         up time = 2017-06-25 22:26:21
 70         total storage = 80505 MB
 71         free storage = 54857 MB
 72         upload priority = 10
 73         store_path_count = 1
 74         subdir_count_per_path = 256
 75         storage_port = 23000
 76         storage_http_port = 8888
 77         current_write_path = 0
 78         source storage id = 
 79         if_trunk_server = 0
 80         connection.alloc_count = 256
 81         connection.current_count = 1
 82         connection.max_count = 2
 83         total_upload_count = 8
 84         success_upload_count = 8
 85         total_append_count = 1
 86         success_append_count = 1
 87         total_modify_count = 0
 88         success_modify_count = 0
 89         total_truncate_count = 0
 90         success_truncate_count = 0
 91         total_set_meta_count = 6
 92         success_set_meta_count = 6
 93         total_delete_count = 1
 94         success_delete_count = 1
 95         total_download_count = 3
 96         success_download_count = 3
 97         total_get_meta_count = 0
 98         success_get_meta_count = 0
 99         total_create_link_count = 0
100         success_create_link_count = 0
101         total_delete_link_count = 0
102         success_delete_link_count = 0
103         total_upload_bytes = 102166
104         success_upload_bytes = 102166
105         total_append_bytes = 6
106         success_append_bytes = 6
107         total_modify_bytes = 0
108         success_modify_bytes = 0
109         stotal_download_bytes = 1994
110         success_download_bytes = 1994
111         total_sync_in_bytes = 2215
112         success_sync_in_bytes = 2215
113         total_sync_out_bytes = 0
114         success_sync_out_bytes = 0
115         total_file_open_count = 15
116         success_file_open_count = 15
117         total_file_read_count = 3
118         success_file_read_count = 3
119         total_file_write_count = 12
120         success_file_write_count = 12
121         last_heart_beat_time = 2017-07-04 23:05:56
122         last_source_update = 2017-07-04 23:03:29
123         last_sync_update = 2017-07-04 22:59:48
124         last_synced_timestamp = 2017-07-04 22:59:40 (0s delay)
125     Storage 2:
126         id = 10.143.117.28
127         ip_addr = 10.143.117.28  ACTIVE
128         http domain = 
129         version = 5.08
130         join time = 2017-06-25 22:22:24
131         up time = 2017-06-25 23:00:18
132         total storage = 80505 MB
133         free storage = 70629 MB
134         upload priority = 10
135         store_path_count = 1
136         subdir_count_per_path = 256
137         storage_port = 23000
138         storage_http_port = 8888
139         current_write_path = 0
140         source storage id = 10.143.117.27
141         if_trunk_server = 0
142         connection.alloc_count = 256
143         connection.current_count = 1
144         connection.max_count = 2
145         total_upload_count = 2
146         success_upload_count = 2
147         total_append_count = 1
148         success_append_count = 1
149         total_modify_count = 0
150         success_modify_count = 0
151         total_truncate_count = 0
152         success_truncate_count = 0
153         total_set_meta_count = 0
154         success_set_meta_count = 0
155         total_delete_count = 1
156         success_delete_count = 1
157         total_download_count = 4
158         success_download_count = 4
159         total_get_meta_count = 0
160         success_get_meta_count = 0
161         total_create_link_count = 0
162         success_create_link_count = 0
163         total_delete_link_count = 0
164         success_delete_link_count = 0
165         total_upload_bytes = 2209
166         success_upload_bytes = 2209
167         total_append_bytes = 6
168         success_append_bytes = 6
169         total_modify_bytes = 0
170         success_modify_bytes = 0
171         stotal_download_bytes = 4197
172         success_download_bytes = 4197
173         total_sync_in_bytes = 127563
174         success_sync_in_bytes = 102466
175         total_sync_out_bytes = 0
176         success_sync_out_bytes = 0
177         total_file_open_count = 22
178         success_file_open_count = 22
179         total_file_read_count = 4
180         success_file_read_count = 4
181         total_file_write_count = 18
182         success_file_write_count = 18
183         last_heart_beat_time = 2017-07-04 23:05:54
184         last_source_update = 2017-07-04 22:59:40
185         last_sync_update = 2017-07-04 23:03:30
186         last_synced_timestamp = 2017-07-04 23:03:30 (-1s delay)
187 [root@gmxfjr-dev17 ~]#
188 摘除storage:
189 1)先删除节点
190 [root@gmxfjr-dev17 ~]# fdfs_monitor /etc/fdfs/client.conf delete group1 10.143.117.28
191 [2017-07-04 23:09:06] DEBUG - base_path=/data/fastdfs/fastdfs/client, connect_timeout=30, network_timeout=60, tracker_server_count=2, 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
192 
193 server_count=2, server_index=0
194 
195 tracker server is 10.143.117.27:22122
196 
197 delete storage server group1::10.143.117.28 fail, error no: 16, error info: Device or resource busy
198 2)停止服务
199 [root@gmxfjr-dev18 ~]# /usr/bin/fdfs_storaged /etc/fdfs/storage.conf stop
200 waiting for pid [10500] exit ...
201 pid [10500] exit.
202 [root@gmxfjr-dev18 ~]# 
203 
204     last_synced_timestamp = 2017-07-04 22:59:40 (0s delay)
205     Storage 2:
206         id = 10.143.117.28
207         ip_addr = 10.143.117.28  OFFLINE
208         http domain = 
209         version = 5.08
210         join time = 2017-06-25 22:22:24
211         up time = 
212         total storage = 80505 MB
client基本使用命令

 

整合php:
[root@gmxfjr-dev17 php-5.6.11]# yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libdn libdn-devel openssl openssl-devel openssldap openldap-devel nss_ldap openldap-clients

/data/fastdfs/FastDFS/php_client

测试php和fastdfs的融合
/usr/local/php/bin/php fastdfs_test.php /data/fastdfs/FastDFS/php_client

 

查看php安装路径:
[root@gmxfjr-dev17 php_client]# /usr/local/php/bin/php -i |grep php.ini
Configuration File (php.ini) Path => /usr/local/php/lib
Loaded Configuration File => /usr/local/php/etc/php.ini
PHP Warning: Unknown: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in Unknown on line 0

学习代码地址:
https://github.com/happyfish100


fastdfs+nginx的整合
https://github.com/happyfish100/fastdfs-nginx-module

Nginx整合
[root@gmxfjr-dev17 nginx-1.8.1]# ./configure --prefix=/usr/local/nginx1.8 --user=www --group=www --with-http_ssl_module --with=pcre=/usr/local/src/pcre-8.36 --add-module=/usr/local/src/fastdfs-nginx-module/src/

posted @ 2017-06-25 15:19  李永三  阅读(5449)  评论(0编辑  收藏  举报