搭建FastDFS文件服务器以及SpringBoot上传文件

  • 一、前言
  • 二、搭建FastDFS文件服务器
  • 三,安装nginx及插件实现资源的访问
  • 四、springboot访问FastDFS实现文件上传
  • 五,真实业务代码


一、前言

(1)FastDFS是什么

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

(2)FastDFS结构

FastDFS 架构包括 Tracker server和 Storage server。客户端请求 Tracker server进行文件上传、下载,通过 Tracker server 调度最终由 Storage server 完成文件上传和下载。

Tracker server 作用是负载均衡和调度,通过 Tracker server 在文件上传时可以根据一些策略找到 Storage server 提供文件上传服务。可以将tracker称为追踪服务器或调度服务器。

Storage server作用是文件存储,客户端上传的文件最终存储在 Storage 服务器上,Storageserver 没有实现自己的文件系统而是利用操作系统 的文件系统来管理文件。可以将storage称为存储服务器。

centos7 上传文件超过3g 上传文件到centos7_fastdfs

二、搭建FastDFS文件服务器

1、安装依赖,打开liunx命令窗口,输入以下命令。

yum -y install gcc-c++
yum -y install libevent

2、准备以下几个压缩包。

centos7 上传文件超过3g 上传文件到centos7_centos7 上传文件超过3g_02

3、在Linux服务器/usr/local文件夹中新建一个fastdfs文件夹,然后使用MobaXterm或xftp把这些压缩包上传到Linux服务器并全部解压。
解压命令如:

tar -zxvf libfastcommonV1.0.7.tar.gz

centos7 上传文件超过3g 上传文件到centos7_nginx_03


4、安装libfastcommon,进入libfastcommon文件夹命令如下:

[root@localhost libfastcommon-1.0.7]# ls
HISTORY  INSTALL  libfastcommon.spec  make.sh  README  src
[root@localhost libfastcommon-1.0.7]# ./make.sh
[root@localhost libfastcommon-1.0.7]# ./make.sh install

5、安装好之后会自动将库文件拷贝至usr/lib64下,由于FastDFS程序引用/usr/lib目录,所以需要把/usr/lib64下的库文件拷贝至/usr/lib下。
命令如下:

cd /usr/lib64
cp libfastcommon.so /usr/lib

查看/usr/lib目录下是否存在该文件

[root@localhost lib64]# find /usr/lib -name libfastcommon.so
/usr/lib/libfastcommon.so

6、安装FastDFS,进入到fastdfs-5.11文件夹

[root@localhost fastdfs]# cd fastdfs-5.11
[root@localhost fastdfs-5.11]# ./make.sh
[root@localhost fastdfs-5.11]# ./make.sh install

安装成功后将安装目录下的conf下的文件拷贝到/etc/fdfs下。(nginx需要)

conf文件目录在/usr/local/fastdfs/fastdfs-5.11/conf,进到这个conf目录

cp * /etc/fdfs/  拷贝全部文件到fdfs目录下

7、安装tracker服务(跟踪服务)

[root@localhost conf]# cd /usr/local/fastdfs/FastDFS/tracker/
[root@localhost tracker]# pwd
/usr/local/fastdfs/fastdfs-5.11/tracker   这个路径设置为base_path路径(下图)
# 编辑tracker.conf配置文件
[root@localhost tracker]# vim /etc/fdfs/tracker.conf

修改base_path路径为:/usr/local/fastdfs/fastdfs-5.11/tracker

centos7 上传文件超过3g 上传文件到centos7_centos7 上传文件超过3g_04


保存退出,启动tracker服务

# 启动
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
# 重启的话是restart
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart

启动成功,查看监听服务:

netstat -unltp|grep fdfs

centos7 上传文件超过3g 上传文件到centos7_fastdfs_05

8、安装storage服务(存储服务)

[root@localhost storage]# pwd
/usr/local/fastdfs/fastdfs-5.11/storage  这个路径设置为base_path路径(下图)
[root@localhost storage]# vim /etc/fdfs/storage.conf

修改base_path路径为:/usr/local/fastdfs/fastdfs-5.11/storage

centos7 上传文件超过3g 上传文件到centos7_nginx_06


修改store_path0、tracker_server

centos7 上传文件超过3g 上传文件到centos7_服务器_07


启动,命令如下,每一种都可以启动

/usr/bin/fdfs_storaged /etc/fdfs/storage.conf
/etc/init.d/fdfs_storaged start
service fdfs_storaged start
systemctl start fdfs_storaged  #centos7 推荐

启动Storage前确保Tracker是启动的。初次启动成功,会在 /usr/local/fastdfs/fastdfs-5.11/storage(base_path) 目录下创建 data、 logs 两个目录。
查看是否启动成功

netstat -unltp|grep fdfs

centos7 上传文件超过3g 上传文件到centos7_上传_08


关闭Storage,关闭Tracker服务同操作,将fdfs_storaged改成fdfs_trackerd

[root@localhost fdfs]# service fdfs_storaged stop
[root@localhost fdfs]# systemctl stop fdfs_storaged #centos7 推荐
[root@localhost fdfs]# /etc/init.d/fdfs_storaged stop

9、查看Storage和Tracker是否在通信:

/usr/bin/fdfs_monitor /etc/fdfs/storage.conf

centos7 上传文件超过3g 上传文件到centos7_fastdfs_09


设置 Storage 开机启动

chkconfig fdfs_storaged on
或
systemctl enable fdfs_storaged.service  (推荐)
或者:
vim /etc/rc.d/rc.local
加入配置:
/etc/init.d/fdfs_storaged  start

10、将/usr/local/fastdfs/fastdfs-5.11/client里面的libfdfsclient.so拷贝到/usr/lib下

cd /usr/local/fastdfs/fastdfs-5.11/client/
cp libfdfsclient.so /usr/lib

11、修改 Tracker 服务器中的客户端配置文件

cd /etc/fdfs
cp client.conf.sample client.conf
vim client.conf

centos7 上传文件超过3g 上传文件到centos7_nginx_10


12、测试上传

在root根目录新建一个hello.html文件,内容为Hello World

上传

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

返回url地址:http://8.140.101.119/group1/M00/00/00/rBdBumBhc7aAGnE5AAAADfMBXHU89_big.html即为上传成功

centos7 上传文件超过3g 上传文件到centos7_fastdfs_11


返回的文件ID由group、存储目录、两级子目录、fileid、文件后缀名(由客户端指定,主要用于区分文件类型)拼接而成。

centos7 上传文件超过3g 上传文件到centos7_fastdfs_12

文件存在这个文件夹下了:/usr/local/fastdfs/FastDFS/storage/data/00/00

[root@localhost 00]# pwd
/usr/local/fastdfs/FastDFS/storage/data/00/00

此时并不能通过这个url直接访问文件,需要安装nginx来处理http协议。

三,安装nginx及插件实现资源的访问

nginx作用:因为FastDFS本身并不具有处理http协议的能力,而客户端正是通过http协议访问的,因此需要通过nginx来实现资源的访问。

如何实现:需要nginx服务器作为中间件,因为nginx具有处理http协议的能力,然后安装nginx+fastDFS的插件,目的是建立nginx和FastDFS服务器的联系以供访问。

centos7 上传文件超过3g 上传文件到centos7_服务器_13


1、安装nginx及nginx插件

yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel

2、进到/usr/local/fastdfs/fastdfs-nginx-module-1.20/src下,修改config配置文件

把路径上的local全部去掉,并保存退出。

centos7 上传文件超过3g 上传文件到centos7_服务器_14


3、把/usr/local/fastdfs/fastdfs-nginx-module-1.20/src下的mod_fastdfs.conf文件复制到/etc/fdfs目录下,并编辑该文件。

[root@192 src]# cp mod_fastdfs.conf /etc/fdfs/
[root@192 src]# vim /etc/fdfs/mod_fastdfs.conf

centos7 上传文件超过3g 上传文件到centos7_fastdfs_15


4、安装nginx

关联nginx和nginx的插件

[root@192 fastdfs]# cd nginx-1.12.1/
# 关联操作
[root@192 nginx-1.12.1]# ./configure --add-module=/usr/local/fastdfs/fastdfs-nginx-module-1.20/src
[root@192 nginx-1.12.1]# make  编译
[root@192 nginx-1.12.1]# make install  安装

可以看到,nginx被安装到了/usr/local目录下

centos7 上传文件超过3g 上传文件到centos7_上传_16


到/usr/local/nginx/conf目录下,修改nginx的配置文件nginx.conf,保存退出。

centos7 上传文件超过3g 上传文件到centos7_nginx_17


启动nginx

[root@192 sbin]# pwd
/usr/local/nginx/sbin
[root@192 sbin]# ./nginx

然后现在就可以在浏览器访问刚才那个文件的路径了。

centos7 上传文件超过3g 上传文件到centos7_上传_18

四、springboot访问FastDFS实现文件上传

1,添加依赖

<dependency>
            <groupId>com.github.tobato</groupId>
            <artifactId>fastdfs-client</artifactId>
            <version>1.26.1-RELEASE</version>
        </dependency>

2,在启动类上添加注解引入配置文件类

@Import(FdfsClientConfig.class)

3,在application.properties加入FastDFS的相关配置

fdfs:
  # 超时时间
  so-timeout: 2500
  # 连接的超时时间
  connect-timeout: 600
  # 缩略图尺寸
  thumb-image:
    width: 100
    height: 100
  # tracker服务跟踪器的地址
  tracker-list:
    - 8.140.101.119:22122
  ## 连接池最大数量
  pool:
    max-total: 200
  ## 每个tracker地址的最大连接数
    max-total-per-key: 50
  ## 连接耗尽时等待获取连接的最大毫秒数
    max-wait-millis: 5000

这里插一句,提前先把linux的防火墙设置为22122端口和23000端口放行,否则后续的测试会因为连不上跟踪器报错。

@Autowired
    FastFileStorageClient fastFileStorageClient;//直接引入

    @RequestMapping("/fastdfs")
    @ResponseBody
    public String fastdfs() throws FileNotFoundException {
        File file=new File("D://文件笔记//image//1571884758247.png");
        //文件名
        String fileName=file.getName();
        //后缀名
        String extName=fileName.substring(fileName.lastIndexOf(".")+1);
        //创建流
        FileInputStream fileInputStream=new FileInputStream(file);
        //四个参数(输入流,文件大小,后缀名,null),返回一个路径
        StorePath storePath = fastFileStorageClient.uploadFile(fileInputStream, file.length(), extName, null);
        //不同路径
        System.out.println(storePath.getFullPath());
        System.out.println(storePath.getPath());
        System.out.println(storePath.getGroup());
        return "图片上传成功,并调皮的给您返回一个路径";
    }

分别看浏览器和控制台

centos7 上传文件超过3g 上传文件到centos7_nginx_19


然后在浏览器输入访问路径(nginx的ip地址和端口加上storePath.getFullPath()的路径),例如

http://192.168.186.129:80/group1/M00/00/00/wKi6gV26SV6ALJu6AAB5lQx82SU564.png 然后就可以访问刚才上传的图片了

五,真实业务代码

controller上传业务代码

@Autowired
    FastFileStorageClient fastFileStorageClient;

    /**
     *fastDFS服务器测试文件上传
     */
    @RequestMapping("/fastdfs")
    @ResponseBody
    public String fastdfs(@RequestParam(value = "test") MultipartFile test) throws IOException {

        //文件名
        String fileName=test.getOriginalFilename();
        //后缀名
        String extName=fileName.substring(fileName.lastIndexOf(".")+1);
        //四个参数(输入流,文件大小,后缀名,null),返回一个路径
        StorePath storePath = fastFileStorageClient.uploadFile(test.getInputStream(),test.getSize(), extName, null);
        //不同路径
        System.out.println(storePath.getFullPath());
        System.out.println(storePath.getPath());
        System.out.println(storePath.getGroup());
        return "图片上传成功,并调皮的给您返回一个路径";
    }

控制台打印

centos7 上传文件超过3g 上传文件到centos7_centos7 上传文件超过3g_20


把第一条路径,也就是storePath.getFullPath()这样得到的路径拼接到nginx的ip地址和端口后面,在浏览器进行访问,可以访问到上传的图片。

前端也可以通过这个路径来访问下载文件

说明:如果上传的是一个文件,这里比如上传一个docx格式的word文档,那么上传成功之后返回的那个路径,group1/M00/00/00/wKi6gV26VG2AG36RAAeqJLEbOEY03.docx,拼接路径之后http://192.168.186.129/group1/M00/00/00/wKi6gV26VG2AG36RAAeqJLEbOEY03.docx,在浏览器直接访问这个路径的话会直接把这个word文档下载下来,图片的话就只是查看。