FastDFS分布式文件上传系统的搭建

FastDFS分布式文件上传的搭建

1、FastDFS+Nginx实现文件服务器

1.1.1、架构图

这里写图片描述

1.2、FastDFS–tracker安装

在虚拟机centos 7上(192.168.25.128)上安装tracker。

1.2.1、下载
tracker和storage使用相同的安装包,下载地址:
https://github.com/happyfish100或者
https://sourceforge.net/projects/fastdfs/

本次下载的fastdfs版本为:fastdfs-5.11.tar.gz

1.2.2、FastDFS安装环境
安装FastDFS需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc:

**yum install gcc-c++**

上传环境rz搭建需要安装lrzsz:

    yum install lrzsz

安装fastdfs前需要下载安装fastdfs的一些依赖库

fastDFS依赖libevent库,因此需要安装:

yum -y install libevent

libfastcommon是FastDFS官方提供的,libfastcommon包含了FastDFS运行所需要的一些基础库。因此需要下载安装:https://github.com/happyfish100/

将libfastcommon-1.0.38.tar.gz拷贝至/usr/local/下

cd /usr/local
tar -zxvf libfastcommon-1.0.38.tar.gz
cd libfastcommon-1.0.38
./make.sh
./make.sh install

注意:libfastcommon安装好后会自动将库文件拷贝至/usr/lib64下,由于FastDFS程序引用usr/lib目录所以需要将/usr/lib64下的库文件拷贝至/usr/lib下(64位的系统不需要,会自动拷贝)。
要拷贝的文件如下:
这里写图片描述

1.2.3tracker编译安装
将fastdfs-5.11.tar.gz拷贝至/usr/local/下

tar -zxvf fastdfs-5.11.tar.gz
cd fastdfs
./make.sh
./make.sh install

安装成功将安装目录下的conf下的文件拷贝到/etc/fdfs/下。

1.2.4配置
安装成功后进入/etc/fdfs目录:
修改tracker.conf

vi tracker.conf
base_path=/home/yuqing/fastdfs   
改为:
base_path=/home/fastdfs

1.2.5启动
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
启动的日志显示先停止1426进程(实际环境不是1426)再启动,如下图:
这里写图片描述
注意:如果没有显示上图要注意是否正常停止原有进程。

设置开机自动启动。

cp /usr/local/fastdfs-5.11/init.d/fdfs_trackerd /etc/init.d/
chkconfig --add fdfs_trackerd 
chkconfig fdfs_trackerd on 

1.3、FastDFS–storage安装(如果是同一台虚拟机的话直接跳到1.3.4)

在192.168.101.3上安装storage。

1.3.1安装libevent
同tracker安装

1.3.2安装libfastcommon
同tracker安装。

1.3.3storage编译安装
同tracker编译安装。

1.3.4配置
安装成功后进入/etc/fdfs目录:

cd /etc/fdfs

修改storage.conf

vi storage.conf
group_name=group1
base_path=/home/yuqing/FastDFS改为:base_path=/home/fastdfs
store_path0=/home/yuqing/FastDFS改为:store_path0=/home/fastdfs

如果有多个挂载磁盘则定义多个store_path,如下

store_path1=…..

store_path2=……

tracker_server=192.168.25.128:22122 #配置tracker服务器:IP

如果有多个则配置多个tracker

tracker_server=192.168.25.128:22122

1.3.5启动

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

启动的日志显示先停止1649进程(实际环境不是1649)再启动,如下图:
这里写图片描述
注意:如果没有显示上图要注意是否正常停止原有进程。
如果出现启动不成功可以通过查看日志来找出问题。
日志中出现:

No route to host 时,

可通过关闭防火墙来解决问题:
关闭防火墙的方法为:
1. 永久性生效

开启:chkconfig iptables on
关闭:chkconfig iptables off

2. 即时生效,重启后失效

开启:service iptables start
关闭:service iptables stop

设置开机自启动

cp /usr/local/fastdfs-5.11/init.d/fdfs_storaged    /etc/init.d/ 
chkconfig --add fdfs_storaged 
chkconfig fdfs_storaged on 

1.4上传图片测试

1.4.1通过fdfs_test程序
FastDFS安装成功可通过/usr/bin/fdfs_test测试上传、下载等操作。

修改/etc/fdfs/client.conf

base_path=/home/fastdfs
tracker_server=192.168.25.128:22122

使用格式:
/usr/bin/fdfs_test 客户端配置文件地址 upload 上传文件

比如将/home下的图片上传到FastDFS中:

/usr/bin/fdfs_test /etc/fdfs/client.conf upload /root/hello.jpg

http://192.168.25.128/group1/M00/00/00/wKhlBVVY2M-AM_9DAAAT7-0xdqM485_big.jpg就是文件的下载路径。
对应storage服务器上的
/home/fastdfs/data/00/00/wKhlBVVY2M-AM_9DAAAT7-0xdqM485_big.png文件。

由于现在还没有和nginx整合无法使用http下载。

1.5FastDFS 和nginx整合

1.5.1在tracker上安装nginx
首先,由于 nginx 的一些模块依赖一些 lib 库,所以在安装 nginx 之前,必须先安装这些 lib 库。这些依赖库主要有 g++、gcc、openssl-devel、pcre-devel 和 zlib-devel 所以执行如下命令安装

yum install gcc-c++ 
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel

将nginx-1.8.0.tar.gz拷贝至linux服务器。
解压:

tar -zxvf nginx-1.8.0.tar.gz
cd nginx-1.8.0

1、configure
./configure –help查询详细参数(参考本教程附录部分:nginx编译参数)

参数设置如下:

./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

注意:上边将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录

2、编译安装

make
make install

安装成功查看安装目录

这里写图片描述

启动ngnix:

cd /usr/local/nginx/sbin/
./nginx 

查询nginx进程:

ps aux|grep nginx

注意:执行./nginx启动nginx,这里可以-c指定加载的nginx配置文件,如下:

./nginx -c /usr/local/nginx/conf/nginx.conf

如果不指定-c,nginx在启动时默认加载conf/nginx.conf文件,此文件的地址也可以在编译安装nginx时指定./configure的参数(–conf-path= 指向配置文件(nginx.conf))

停止nginx
方式1,快速停止:

cd /usr/local/nginx/sbin
./nginx -s stop

此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。

方式2,完整停止(建议使用):

cd /usr/local/nginx/sbin
./nginx -s quit

此方式停止步骤是待nginx进程处理任务完毕进行停止。

重启:

方式1,先停止再启动(建议使用):
对nginx进行重启相当于先停止nginx再启动nginx,即先执行停止命令再执行启动命令。
如下:

./nginx -s quit
./nginx

方式2,重新加载配置文件:
当nginx的配置文件nginx.conf修改后,要想让配置生效需要重启nginx,使用-s reload不用先停止nginx再启动nginx即可将配置信息在nginx中生效,如下:

./nginx -s reload

3.设置执行权限、注册成服务、设置开机启动

chmod a+x /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on

1.5.2在Storage上安装nginx
1.5.2.1FastDFS-nginx-module
将fastdfs-nginx-module_v1.16.tar.gz传至/usr/local/下

cd /usr/local
tar -zxvf fastdfs-nginx-module_v1.16.tar.gz
cd fastdfs-nginx-module/src

修改config文件将/usr/local/路径改为/usr/
这里写图片描述

将fastdfs-nginx-module/src下的mod_fastdfs.conf拷贝至/etc/fdfs/下

cp mod_fastdfs.conf /etc/fdfs/

并修改mod_fastdfs.conf的内容:

vi /etc/fdfs/mod_fastdfs.conf
base_path=/home/fastdfs
tracker_server=192.168.25.128:22122
url_have_group_name=true        #url中包含group名称
store_path0=/home/fastdfs   #指定文件存储路径

tracker_server=192.168.25.129:22122(多个tracker配置多行)

将libfdfsclient.so拷贝至/usr/lib下

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

创建nginx/client目录

mkdir -p /var/temp/nginx/client

1.5.2.2nginx安装

添加FastDFS-nginx-module模块

./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=/usr/local/fastdfs-nginx-module/src

nginx安装

make
make install

1.5.2.3添加nginx配置文件

cd /usr/local/nginx/conf
vim ngnix.conf

添加server:

server {
        listen       80;
        server_name  192.168.25.128;

    location /group1/M00/{
            #root /home/FastDFS/fdfs_storage/data;
            ngx_fastdfs_module;
    }
}

这里写图片描述
说明:
server_name指定本机ip
location /group1/M00/:group1为nginx 服务fastdfs的分组名称,M00是fastdfs自动生成编号,对应store_path0=/home/fastdfs,如果fastdfs定义store_path1,这里就是M01

1.5.3测试
通过java客户端上传文件,使用浏览器访问,比如访问上传图片测试的文件:

public class FastdfsClientTest {
public static void main(String[] args) throws Exception {
    // 1、加载配置文件,配置文件中的内容就是 tracker 服务的地址。
    ClientGlobal.init("D:/workspace/rabbitMq-producer/src/main/resources/fdfs_client.conf");
    // 2、创建一个 TrackerClient 对象。直接 new 一个。
    TrackerClient trackerClient = new TrackerClient();
    // 3、使用 TrackerClient 对象创建连接,获得一个 TrackerServer 对象。
    TrackerServer trackerServer = trackerClient.getConnection();
    // 4、创建一个 StorageServer 的引用,值为 null
    StorageServer storageServer = null;
    // 5、创建一个 StorageClient 对象,需要两个参数 TrackerServer 对象、StorageServer 的引用
    StorageClient storageClient = new StorageClient(trackerServer, storageServer);
    // 6、使用 StorageClient 对象上传图片。
    //扩展名不带“.”
    String[] strings = storageClient.upload_file("D:/setup/hello.jpg", "jpg",
            null);
    // 7、返回数组。包含组名和图片的路径。
    for (String string : strings) {
        System.out.println(string);
        }
    }
}

配置文件fdfs_client.conf:

//......
tracker_server=192.168.25.128:22122
//......

根据java客户端返回值在浏览器上访问:
http://192.168.25.128/group1/M00/00/00/wKhlBVVY2M-AM_9DAAAT7-0xdqM485_big.png

问题:
重启虚拟机后,再次重启nginx会报错: open() “/var/run/nginx/nginx.pid” failed (2: No such file or directory)
解决方法:

(1) 进入 cd /usr/local/nginx/conf/ 目录,编辑配置文件nginx.conf ;
(2) 在配置文件中有个注释的地方: #pid logs/nginx.pid;
这里写图片描述
(3) 将注释放开,并修改为:pid /usr/local/nginx/logs/nginx.pid;
这里写图片描述
(4) 在 /usr/local/nginx 目录下创建 logs 目录:mkdir /usr/local/nginx/logs
(5) 启动nginx服务:/usr/local/nginx/sbin/nginx

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值