fastdfs在linux系统上的搭建

上传、下载流程简介:

上传文件交互过程:
1. client询问tracker上传到的storage,不需要附加参数;
2. tracker返回一台可用的storage;
3. client直接和storage通讯完成文件上传。
FastDFS file download

下载文件交互过程:
1. client询问tracker下载文件的storage,参数为文件标识(卷名和文件名);
2. tracker返回一台可用的storage;
3. client直接和storage通讯完成文件下载。

1、安装FastDFS

1.1,安装编译工具


yum -y groupinstall 'Development Tools' 
yum -y install wget 

1.2,安装libfastcommon类库

必须先安装这个,否则报错:

cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O -DDEBUG_FLAG -DOS_LINUX -DIOEVENT_USE_EPOLL -c -o ../common/fdfs_global.o ../common/fdfs_global.c  -I../common -I/usr/include/fastcommon
../common/fdfs_global.c:20:20: fatal error: logger.h: No such file or directory
 #include "logger.h"
                    ^
compilation terminated.
........

直接下载安装即可:

wget https://github.com/happyfish100/libfastcommon/archive/master.zip
unzip master.zip
cd libfastcommon-master
./make.sh
./make.sh install

1.3,安装FastDFS

wget  https://github.com/happyfish100/fastdfs/archive/V5.05.tar.gz
tar -zxvf V5.05.tar.gz 
cd fastdfs-5.05/
./make.sh
./make.sh install

安装之后配置文件在目录/etc/fdfs , 将下面的两个文件重命名:

mv storage.conf.sample  storage.conf
mv tracker.conf.sample tracker.conf

1.4,tracker相关配置,以及启动

1.4.1、 修改配置文件:tracker.conf
vim /etc/fdfs/tracker.conf

#一般只需改动以下几个参数即可:
disabled=false            #启用配置文件,这个地方有点特殊,false表示启用
port=22122                #设置tracker的端口号
base_path=/usr/local/fdfs/tracker   #设置tracker的数据文件和日志目录(需预先创建)
http.server_port=9090     #设置http端口号
1.4.2、 启动tracker:
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start
1.4.3、 查看运行状态:
[root@bxcs_repository FastDFS]# netstat -unltp | grep fdfs
tcp        0      0 0.0.0.0:22122               0.0.0.0:*                   LISTEN      2225/fdfs_trackerd
[root@bxcs_repository FastDFS]# ps -ef |grep fdfs
root      2225     1  0 00:50 ?        00:00:00 fdfs_trackerd /etc/fdfs/tracker.conf restart
1.4.4、 去tracker的日志看看有没有错:
[root@bxcs_repository FastDFS]# tail -f /data/fdfs/logs/trackerd.log
[2014-06-26 00:50:48] INFO - FastDFS v5.01,

base_path=/usr/local/fdfs/tracker, run_by_group=, run_by_user=,

connect_timeout=30s, network_timeout=60s, port=22122, bind_addr=,

max_connections=256, accept_threads=1, work_threads=4, store_lookup=2,

rotate_error_log=0, error_log_rotate_time=00:00, rotate_error_log_size=0,

g_connection_pool_max_idle_time=3600s
....

[2014-06-26 00:50:48] INFO - local_host_ip_count: 2,  127.0.0.1  

1.4.5、 开机启动Tracker:
vim /etc/rc.local
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
whereis fdfs_trackerd #查看fdfs_trackerd所在路径

1.5,启动storage,以及启动

1.5.1、修改配置文件,启动storage

多个tracker可以配置多次tracker_server,这样可以解决tracker_server的单点问题:

vim /etc/fdfs/storage.conf

#一般只需改动以下几个参数即可:
disabled=false                    #启用配置文件,false表示启用
group_name=group1                 #组名,根据实际情况修改
port=23000                        #设置storage的端口号
base_path=/fdfs/storage           #设置storage的日志目录(需预先创建)
store_path_count=1                #存储路径个数,需要和store_path个数匹配
base_path=/usr/local/fdfs/storage
store_path0=建/data/fdfs/storage #存储路径,store_path#, based 0, if # store_path0 not exists, it's value is base_path
tracker_server=192.168.3.30:22122 #tracker服务器的IP地址和端口号
tracker_server=192.168.3.33:22122 #tracker服务器的IP地址和端口号

http.server_port=9090 #设置http端口号

必须先创建/data/fdfs/storage(可根据具体情况创建)目录,否则启动报错:

ERROR - file: ../common/process_ctrl.c, line: 189,
"/usr/local/fdfs/storage" can't be accessed, error info: 
No such file or directory
1.5.2、 启动storage:
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf start
1.5.3、 查看fdfs进程:
# ps -ef | grep fdfs
roo 20050   1  0 22:08 ? 00:00:00 /usr/bin/fdfs_storaged /etc/fdfs/storage.conf start
root     20060 19915  0 22:08 pts/1  00:00:00 grep --color=auto fdfs
1.5.4、 看日志没有错误就说明启动成功:
tail -f /usr/local/fdfs/storage/logs/storaged.log

[2014-06-26 03:52:47] INFO - local_host_ip_count: 2,  127.0.0.1  192.168.3.31
[2014-06-26 03:52:47] INFO - file: tracker_client_thread.c, line: 308, successfully connect to tracker server 192.168.3.30:22122, as a tracker client, my ip is 192.168.3.31
[2014-06-26 03:52:47] INFO - file: tracker_client_thread.c, line: 308, successfully connect to tracker server 192.168.3.33:22122, as a tracker client, my ip is 192.168.3.31
[2014-06-26 03:52:47] INFO - file: tracker_client_thread.c, line: 1124, tracker server 192.168.3.30:22122, set tracker leader: 192.168.3.30:22122
1.5.5、 使用fdfs_monitor查看Storage是否已登记到Tracker:(注意ip_addr那一行):
fdfs_monitor /etc/fdfs/storage.conf

group name = group1
disk total space = 10079 MB
disk free space = 7267 MB
trunk free space = 0 MB
storage server count = 1
active server count = 1
storage server port = 23000
storage HTTP port = 9090
store path count = 1
subdir count per path = 256
current write server index = 0
current trunk file id = 0

        Storage 1:
                id = 192.168.3.31
                ip_addr = 192.168.3.31 (bxcsweb)  ACTIVE
                http domain =
                version = 5.01
                join time = 2014-06-26 01:13:14
                up time = 2014-06-26 01:13:14
                total storage = 10079 MB
1.5.6、设置开机启动:
[root@bxcsweb bin]# vim /etc/rc.local
#start FastDFS
/usr/local/bin/fdfs_storaged /etc/fdfs/storage.conf restart

1.6话题延伸

如果是 两台Tracker(30/33)、四台Storage【分为两个Group,分别命名为group1(31/32)、group2(34/35)】安装完之后的信息:
[root@bxcs_mongoSlave FastDFS]# fdfs_monitor /etc/fdfs/storage.conf
[2014-06-26 05:25:20] DEBUG - base_path=/usr/local/fdfs/storage, 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

server_count=2, server_index=0

tracker server is 192.168.3.30:22122

group count: 2

Group 1:
group name = group1
disk total space = 10079 MB
disk free space = 7229 MB
trunk free space = 0 MB
storage server count = 2
active server count = 2
storage server port = 23000
storage HTTP port = 9090
store path count = 1
subdir count per path = 256
current write server index = 0
current trunk file id = 0

        Storage 1:
                id = 192.168.3.31
                ip_addr = 192.168.3.31 (localhost)  ACTIVE
                。。。
        Storage 2:
                id = 192.168.3.32
                ip_addr = 192.168.3.32 (localhost)  ACTIVE
                。。。

Group 2:
group name = group2
disk total space = 10079 MB
disk free space = 7477 MB
trunk free space = 0 MB
storage server count = 2
active server count = 2
storage server port = 23000
storage HTTP port = 9090
store path count = 1
subdir count per path = 256
current write server index = 0
current trunk file id = 0

        Storage 1:
                id = 192.168.3.34
                ip_addr = 192.168.3.34 (localhost)  ACTIVE
                ...
        Storage 2:
                id = 192.168.3.35
                ip_addr = 192.168.3.35 (bxcs_mongoSlave)  ACTIVE
                ...

2、storage节点安装nginx和fastdfs-nginx-module模块:

在storage上安装的nginx主要为了提供http的访问服务,同时解决group中storage服务器的同步延迟问题。

2.1、先安装zlib和pcre包(必须安装)。

nginx依赖以下模块:

l、gzip模块需要 zlib 库

2、rewrite模块需要 pcre 库

3、ssl 功能需要openssl库

1、yum安装方式:
yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel
2、编译安装方式:
安装pcre
a、获取pcre编译安装包,在http://www.pcre.org/上可以获取当前最新的版本
b、解压缩pcre-xx.tar.gz包。
c、进入解压缩目录,执行./configure
d、make & make install

2.安装openssl
a、获取openssl编译安装包,在http://www.openssl.org/source/上可以获取当前最新的版本。
b、解压缩openssl-xx.tar.gz包。
c、进入解压缩目录,执行./config。
d、make & make install

3.安装zlib
a、获取zlib编译安装包,在http://www.zlib.net/上可以获取当前最新的版本

b、进入解压缩目录
c、执行./configure

d、make & make install

2.2、 下载fastdfs-nginx-module、nginx安装包

wget https://github.com/happyfish100/fastdfs-nginx-module/archive/master.zip
unzip fastdfs-nginx-module-master.zip
wget http://openresty.org/download/ngx_openresty-1.7.10.1.tar.gz
tar -zxvf ngx_openresty-1.7.10.1.tar.gz

2.3、编译安装:这里注意下,需要增加参数

cd ngx_openresty-1.7.10.1 #进入到nginx目录

#执行编译操作
./configure 
--prefix=/opt/nginx/ #指定安装路径,这里路劲需要自己先创建
--with-luajit 
--with-http_stub_status_module 
--with-http_ssl_module 
--with-http_realip_module 
--add-module=/fastdfs-nginx-module-master/src #fastdfs-nginx-module所在路径中的src目录

#安装
gmake
gmake install
2.3.1 ngninx在gmake时可能出现找不到fdfs_define.h问题
2.3.1.1 错误信息
root/fastdfs-nginx-module/src//common.c:21:25: fatal error:
fdfs_define.h: No such file or directory
 #include "fdfs_define.h"

则需要再添加如下配置

 #把/usr/lib64/libfdfsclient.so库拷贝到/usr/lib/目录下:
$ sudo cp /usr/lib64/libfdfsclient.so /usr/lib/

配置/usr/local/src/fastdfs-nginx-module/src/目录下的config文件,
CORE_INCS和CORE_LIBS的所有路径都修改为/usr/include和/usr/lib:

$  vim /usr/local/src/fastdfs-nginx-module/src/config
...
CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"
CORE_LIBS="$CORE_LIBS -L/usr/lib -lfastcommon -lfdfsclient"
...

/usr/include/fastdfs /usr/include/fastcommon/可根据具体路径配置

之后就可以执行编译和安装了:

$ sudo make && sudo make install
2.3.1.2 ngninx在gmake时可能出现的版本兼容问题
/usr/local/fastdfs-nginx-module-master/src/ngx_http_fastdfs_module.c
In file included from /usr/local/fastdfs-nginx-module-master/src/ngx_http_fastdfs_module.c:6:0:
/usr/local/fastdfs-nginx-module-master/src/common.c: 在函数‘fdfs_http_request_handler’中:
/usr/local/fastdfs-nginx-module-master/src/common.c:1245:61: 错误:‘FDFSHTTPParams’没有名为‘support_multi_range’的成员
   (pContext->range_count > 1 && !g_http_params.support_multi_range))
                                                             ^
make[1]: *** [objs/addon/src/ngx_http_fastdfs_module.o] 错误 1
make[1]: 离开目录“/usr/local/nginx-1.12.0”  

这是因为fastdfs-nginx-module版本问题,我搭建的版本是Version 5.05 你如果没有下载最近的,整合nginx的时候可能会遇到这个问题。
遇到这个错误的原因是,在fastdfs-nginx-module的HISTORY中你可以看到:

整合的时候,fastdfs-nginx-module中的support_multi_range在Version 5.10以下的版本中找不到。
    Version 5.11对应的fastdfs-nginx-module的Version 1.20 
    Version 5.10对应的fastdfs-nginx-module的Version 1.19
所以, 我这边fastdfs-nginx-module选择1.20以前的版本:
重新解压,config,编译等了一会编译成功了,
make install,Nginx安装成功了。

2.4、将fastdfs-nginx-module下的配置文件复制一份到/etc/fdfs/目录

cp /usr/local/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/

2.5、修改mod_fastdfs.conf配置文件

[root@bxcs_mongoSlave conf]# vim /etc/fdfs/mod_fastdfs.conf

一般只需改动以下几个参数即可:
base_path=/usr/local/fdfs/storage #保存日志目录,这个要与storaged一致
tracker_server=192.168.3.30:22122 #tracker服务器的IP地址以及端口号
tracker_server=192.168.3.33:22122 #tracker服务器的IP地址以及端口号
storage_server_port=23000         #storage服务器的端口号
group_name=group1                 #当前服务器的group名
url_have_group_name = true        #文件url中是否有group名
store_path_count=1                #存储路径个数,需要和store_path个数匹配
store_path0=/usr/local/fdfs/storage  #存储路径,这个要与storaged一致
http.need_find_content_type=true  #从文件扩展名查找文件类型(nginx时为true)没找到这个配置参数,可能新版移除了。
group_count = 2                   #设置组的个数

在末尾增加3个组的具体信息:
[group1]
group_name=group1
storage_server_port=23000
store_path_count=1
store_path0=/usr/local/fdfs/storage

# group settings for group #2
# since v1.14
# when support multi-group, uncomment following section as neccessary
[group2]
group_name=group2
storage_server_port=23000
store_path_count=1
store_path0=/usr/local/fdfs/storage

2.6 拷贝配置文件http.conf、mime.types

如果不拷贝的话,nginx启动不了,报错

[2015-04-28 11:30:14] ERROR - file: ini_file_reader.c, line: 315, include file "http.conf" not exists, line: "#include http.conf"
[2015-04-28 11:30:14] ERROR - file: /root/fastdfs-nginx-module-master/src/common.c, line: 155, load conf file "/etc/fdfs/mod_fastdfs.conf" fail, ret code: 2
2015/04/28 11:30:14 [alert] 19969#0: worker process 2753 exited with fatal code 2 and cannot be respawned

解决办法:

cp /root/opt/env/fastdfs-5.05/conf/http.conf  /root/opt/env/fastdfs-5.05/conf/mime.types /etc/fdfs

2.7 nginx配置

建立软连接:(暂时不明白什么意思,不清楚为什么nginx和此处的M00有何对应,想必应该是Tracker会在URL后面加上M00)

[root@bxcs_mongoSlave sbin]# ln -s /data/fdfs/storage/data/ /data/fdfs/storage/data/M00

nginx配置:

server {
        listen       80;
        server_name  localhost;

        charset utf8;

        location /group([0-9])/M00 {
                alias /data/fdfs/storage/data;
                ngx_fastdfs_module;
        }

重启nginx,查看日志文件:

/root/opt/env/nginx/logs/error.log

如果出现以下错误:

[2015-10-25 19:29:43] ERROR - file: ../storage/trunk_mgr/trunk_shared.c, line: 177,"Permission denied" can't be accessed, error info: /home/zq/fastdfs
2015/10/25 19:29:43 [alert] 9094#0: worker process 9095 exited with fatal code 2 and cannot be respawned
2015/10/25 19:30:58 [notice] 9112#0: signal process started
ngx_http_fastdfs_process_init pid=9196

原因以及解决方案:


意思是说/home/zq/fastdfs文件夹不能访问,遇到这个问题首先想到的是权限不够,但是当前我是以root账号启动的啊,应该拥有最高的权限,为什么说我root却不能访问这个文件了,这个问题确实把我搞晕了,有可能nginx启动的后台线程使用的不是root用户,后来把mod_fastdfs.conf文件的base_path和store_path0改为/home/zq,发现启动不报错了,这太奇怪了,这个问题折腾了我好久,上网也没找到解决的办法。后来发现nginx.conf配置的最前面里有配置user,只不过被注释掉了,把user改为root就搞定了
user  root; #在nginx.conf文件的最上部加上这句话


再次重启nginx,启动成功

2.8 测试上传功能

echo "test fastdfs" > test.html
 /usr/bin/fdfs_upload_file /etc/fdfs/client.conf test.html

上传成功,执行结果:

This is FastDFS client test program v5.05

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.

[2015-04-25 18:06:09] DEBUG - base_path=/data/fdfs, 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

tracker_query_storage_store_list_without_group:
        server 1. group_name=, ip_addr=192.168.1.36, port=23000

group_name=group1, ip_addr=192.168.1.36, port=23000
storage_upload_by_filename
group_name=group1, remote_filename=M00/00/00/wKgC1FU7ZxGAF19vAAAADcvBslg01.html
source ip address: 192.168.1.36
file timestamp=2015-04-25 18:06:09
file size=13
file crc32=3418468952
example file url: http://192.168.1.36/group1/M00/00/00/wKgC1FU7ZxGAF19vAAAADcvBslg01.html
storage_upload_slave_by_filename
group_name=group1, remote_filename=M00/00/00/wKgC1FU7ZxGAF19vAAAADcvBslg01_big.html
source ip address: 192.168.1.36
file timestamp=2015-04-25 18:06:09
file size=13
file crc32=3418468952
example file url: http://192.168.1.36/group1/M00/00/00/wKgC1FU7ZxGAF19vAAAADcvBslg01_big.html

测试上传结果:

curl http://192.168.1.36/M00/00/00/wKgC1FU7ZxGAF19vAAAADcvBslg01_big.html

如果返回“test fastdfs”说明配置成功。上面的每一步修改都要正确才可以

2.9、fastDFS上传图片额外产生了一个“原文件名-m"的文件

原因:
上传的时候指定了一个NameValuePair[],meta_list参数,该参数用来存储图片的元信息。

解决方案:将该参数指定为null

// 设置元信息 
NameValuePair[] metaList = new NameValuePair[3]; 
metaList[0] = new NameValuePair(“fileName”, uploadFileName); 
metaList[1] = new NameValuePair(“fileExtName”, fileExtName); 
metaList[2] = new NameValuePair(“fileLength”, String.valueOf(fileLength));

3、总结:

FastDFS非常好的分布式文件系统。配置简单,可以多机器部署,同时官方给了一个Nginx模块,可以直接用nginx访问,非常方便。这样减少了中间的tomcat层,效率高。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值