FastDFS+Nginx搭建文件存储服务器(单机版本)以及整合springboot

fastdfs的github地址: https://github.com/happyfish100/fastdfs

fastdfs的java客户端github地址: https://github.com/happyfish100/fastdfs-client-java(这个是官方的客户端) / https://github.com/tobato/FastDFS_Client(用这个客户端)

搭建环境 centos

一、准备工作

1.上传所需文件并解压

fastdfs-5.11.tar.gz
fastdfs-nginx-module-1.20.tar.gz
libfastcommon-1.0.39.tar.gz
nginx-1.14.1.tar.gz

2.安装libevent库

yum -y install libevent

4.进入libfastcommon-1.0.39目录中执行安装命令(安装后能在/usr/lib64或者/usr/lib目录中找到)

./make.sh && ./make.sh install

5.在上面两个目录中查找libfastcommon.so , 如果只在64目录中找到就把它复制到/usr/lib目录中

二、安装tracker

6.进入fasfdfs的解压目录下

cd fastdfs-5.11

7.执行编译和安装命令, 会安装在/usr/bin 和/etc/fdfs/目录下,使用 ll /usr/bin/fdfs* 查看

./make.sh && ./make.sh install

8.进入fastdfs5.11/conf目录中,把里面所有的配置文件复制到/etc/fdfs目录中

cp * /etc/fdfs

9.进入/etc/fdfs/目录 中使用命令打开

vim tracker.conf
    修改basepath为 /fastdfs/tracker
    
到根目录下创建这些目录
    mkdir /fastdfss/tracker -p
    mkdir /fastdfs/storage -p
    mkdir /fastdfs/client -p
    mkdir /fastdfs/temp -p

10.使用命令和配置启动tracker

/usr/bin/fdfs_trackerd /etc/fdfs/tracker_conf
/usr/bin/fdfs_trackerd /etc/fdfs/tracker_conf restart 重启服务

三、安装storage

11.到/etc/fdfs目录下修改 storage.conf

basepath=/fasddfs/storage
store_path0=/fastdfs/storage
group_name=sl(这个组名自己定义)
tracker_server=192.168.213.120:22122(本机ip)

12.使用命令启动

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

13.查看状态

ps aux|grep tracker  ps aux|grep storage

四、测试上传

14.到/etc/fdfs/目录下修改client.conf

basepath=/fastdfs/client
tracker_server=192.169.213.120:22122(本机)

15.使用下面的测试命令上传一张图片,会返回地址

命令:/usr/bin/fdfs_test /etc/fdfs/client.conf upload yusheng.jpg

返回

This is FastDFS client test program v5.11

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.

[2018-11-20 04:36:38] DEBUG - base_path=/fastdfs/client, 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.213.120, port=23000

group_name=sl, ip_addr=192.168.213.120, port=23000
storage_upload_by_filename
group_name=sl, remote_filename=M00/00/00/wKjVeFvz_9aAcMNOAAEJsJdjMjM460.jpg
source ip address: 192.168.213.120
file timestamp=2018-11-20 04:36:38
file size=68016
file crc32=2539860531
example file url: http://192.168.213.120/sl/M00/00/00/wKjVeFvz_9aAcMNOAAEJsJdjMjM460.jpg
storage_upload_slave_by_filename
group_name=sl, remote_filename=M00/00/00/wKjVeFvz_9aAcMNOAAEJsJdjMjM460_big.jpg
source ip address: 192.168.213.120
file timestamp=2018-11-20 04:36:38
file size=68016
file crc32=2539860531
example file url: http://192.168.213.120/sl/M00/00/00/wKjVeFvz_9aAcMNOAAEJsJdjMjM460_big.jpg

16.可以看到返回信息中显示图片上传到了这个目录中 /fastdfs/storage/data/00/00

五、安装配置nginx

17.安装nginx的fastdfs模块

进入解压后的这个目录中
    /root/fastDFS/fastdfs-nginx-module-1.20/src
    
修改此目录下的config文件

    ngx_addon_name=ngx_http_fastdfs_module
    
    if test -n "${ngx_module_link}"; then
        ngx_module_type=HTTP
        ngx_module_name=$ngx_addon_name
        ngx_module_incs="/usr/local/include"
        ngx_module_libs="-lfastcommon -lfdfsclient"
        ngx_module_srcs="$ngx_addon_dir/ngx_http_fastdfs_module.c"
        ngx_module_deps=
        CFLAGS="$CFLAGS -D_FILE_OFFSET_BITS=64 -DFDFS_OUTPUT_CHUNK_SIZE='256*1024' -DFDFS_MOD_CONF_FILENAME='\"/etc/fdfs/mod_fastdfs.conf\"'"
        . auto/module
    else
        HTTP_MODULES="$HTTP_MODULES ngx_http_fastdfs_module"
        NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_fastdfs_module.c"
        CORE_INCS="$CORE_INCS /usr/local/include"
        CORE_LIBS="$CORE_LIBS -lfastcommon -lfdfsclient"
        CFLAGS="$CFLAGS -D_FILE_OFFSET_BITS=64 -DFDFS_OUTPUT_CHUNK_SIZE='256*1024' -DFDFS_MOD_CONF_FILENAME='\"/etc/fdfs/mod_fastdfs.conf\"'"
    fi

    把上面这两个地方修改成下面的样子
    ngx_module_incs="/usr/include/fastdfs /usr/include/fastcommon/"
    CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"

18.安装nginx支持库

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

19.在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 \
--add-module=/root/fastDFS/fastdfs-nginx-module-1.20/src


添加返回结果

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/var/run/nginx/nginx/pid"
  nginx error log file: "/var/log/nginx/error.log"
  nginx http access log file: "/var/log/nginx/access.log"
  nginx http client request body temporary files: "/var/temp/nginx/client"
  nginx http proxy temporary files: "/var/temp/nginx/proxy"
  nginx http fastcgi temporary files: "/var/temp/nginx/fastcgi"
  nginx http uwsgi temporary files: "/var/temp/nginx/uwsgi"
  nginx http scgi temporary files: "/var/temp/nginx/scgi"


19.进入nginx目录 使用命令编译和安装(不用/make, 直接make)

make && make install 
安装后会在/usr/local目录下有一个nginx文件夹,这个文件夹就是安装后的nginx程序

20.进入fastdfs-nginx-module目录中,复制模块配置(这个是fastdfs和nginx配合的配置文件)

cp mod_fastdfs.conf /etc/fdfs/

21 进入/etc/fdfs/目录中,修改mod_fastdfs.conf文件

basepath=/fastdfs/temp
group_name=sl # (同上面storage的组名一样)
url_have_group_name = true
store_path0=/fastdfs/storage # (同storage的配置目录,这个是用于存储文件的目录)

21.进入nginx的安装目录/usr/local/中,修改它的配置文件nginx.conf

# 添加一个server
server {
    listen  88;
    server_name  localhost;

    # 这个/slM00是上面设置的组名和自动生成的MOO前缀
    location /sl/M00 {
        # 这里是我们添加的模块名
        ngx_fastdfs_module;
    }
}

22.进入/usr/local/nginx/sbin目录执行相关命令

检查参数 ./nginx -t

[root@localhost nginx]# ./sbin/nginx -t
ngx_http_fastdfs_set pid=23514
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: [emerg] mkdir() "/var/temp/nginx/client" failed (2: No such file or directory)
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed

报错说找不到一个目录(/var/temp/nginx/client),我们去创建这个目录
mkdir /var/temp/nginx/client -p

在次使用 ./sbin/nginx -t 测试即可通过测试

开启nginx ./nginx

23.关闭防火墙

[root@localhost nginx]# /etc/init.d/iptables stop
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Unloading modules:                               [  OK  ]

24.访问192.168.213.120可以看到nginx默认首页

25.重启所有服务(tracker storage nginx)

访问图片
http://192.168.213.120:88/sl/M00/00/00/wKjVeFvz_9aAcMNOAAEJsJdjMjM460.jpg
成功

26.重启机器后重启服务的四条命令

[root@localhost ~]# /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
[root@localhost ~]# /usr/bin/fdfs_storaged /etc/fdfs/storage.conf
[root@localhost ~]# /usr/local/nginx/sbin/nginx
[root@localhost ~]# /etc/init.d/iptables stop

六、整合springboot

1.在springboot工程中添加fastdfs-client-java的maven依赖

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

2.在工程中添加配置类

/**
 * 导入FastDFS-Client组件
 * 
 * @author tobato
 *
 */
@Configuration
@Import(FdfsClientConfig.class)
// 解决jmx重复注册bean的问题
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class ComponetImport {
    // 导入依赖组件
}
注意:@EnableMBeanExport解决问题JMX重复注册问题,issue #8 issue #18,不要再配置 spring.jmx.enabled=false,以免影响SpringBoot默认的JMX监控。

3.在application.yml当中配置Fdfs相关参数

# ===================================================================
# 分布式文件系统FDFS配置
# ===================================================================
fdfs:
  so-timeout: 1501
  connect-timeout: 601 
  thumb-image:             #缩略图生成参数
    width: 80
    height: 80
  tracker-list:            #TrackerList参数,支持多个
    - 192.168.213.120:22122

4.整合好后就可以在java代码中注入相关的工具进行上传下载操作了

@Autowired
private FastFileStorageClient storageClient;

这个类的方法
    storageClient.uploadFile
    storageClient.uploadFile2
    storageClient.uploadQRCode
    storageClient.uploadFace
    storageClient.uploadBase64
    storageClient.uploadFile
    storageClient.deleteFile
    storageClient.storageClient
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容