FastDFS+nginx配置,并集成SpringBoot

本文主要记录单机版FastDFS的配置安装

1.下载FastDFS安装包

https://github.com/happyfish100/FastDFS

我在安装过程中,使用的是FastDFS_v5.05.tar.gz的版本

2.FastDFS安装环境

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

libevent 库:
    FastDFS 依赖 libevent 库,需要安装:
    yum -y install libevent

libfastcommon :
    libfastcommon 是 FastDFS 官方提供的,libfastcommon 包含了 FastDFS 运行所需
    要的一些基础库。本文使用 libfastcommonV1.0.7.tar.gz版本:
    将 libfastcommonV1.0.7.tar.gz 拷贝至/usr/local/下
    cd /usr/local
    tar -zxvf libfastcommonV1.0.7.tar.gz
    cd libfastcommon-1.0.7
    ./make.sh
    ./make.sh install
    
    注意:libfastcommon 安装好后会自动将libfastcommon.so文件拷贝至/usr/lib64 下,由于 FastDFS                                                            
    程序引用 usr/lib 目录,所以需要将/usr/lib64 下的库文件拷贝至/usr/lib 下:
    cp /usr/lib64/libfastcommon.so /usr/lib

如果编译报错: 执行下面的命令
yum -y install zlib zlib-devel pcre pcre-devel gcc gcc-c++ openssl openssl-devel libevent libevent-devel perl unzip net-tools wget 

3.安装tracker和storage

将 FastDFS_v5.05.tar.gz 拷贝至/usr/local/下
tar -zxvf FastDFS_v5.05.tar.gz
cd FastDFS
./make.sh 编译
./make.sh install 安装

安装成功后将安装目录下的所有配置文件拷贝到/etc/fdfs/下:
cp /usr/local/FastDFS/conf/client.conf http.conf mime.types storage.conf storage_ids.conf tracker.conf /etc/fdfs

tracker安装:
    复制完成后,进入/etc/fdfs目录
    如果只有一份tracker.conf.sample 文件则复制一份新的配置文件:
    cp tracker.conf.sample tracker.conf
    修改tracker.conf
    base_path=/home/yuqing/FastDFS 改为 base_path=/home/FastDFS
    修改端口
    http.server_port=80

storage安装:
    复制完成后,进入/etc/fdfs目录
    如果只有一份storage.conf.sample 文件则复制一份新的配置文件:
    cp storage.conf.sample storage.conf
    修改storage.conf
    group_name=group1
    base_path=/home/yuqing/FastDFS 改为: base_path=/home/FastDFS
    store_path0=/home/yuqing/FastDFS 改为:store_path0=/home/FastDFS/fdfs_storage
    /home/FastDFS/fdfs_storage 该文件夹需要自己创建,否则报错
    tracker_server=xxx.xxx.x.xxx:22122 #配置 tracker 服务器:IP
    修改端口
    http.server_port=80

4.启动tracker和storage

/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart

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

启动成功会显示

       

       

5.测试上传操作

修改/etc/fdfs/client.conf

base_path=/home/FastDFS
tracker_server=xxx.xxx.x.xxx:22122

所有修改配置文件后记得重启

重启后:
    使用格式:
    /usr/bin/fdfs_test 客户端配置文件地址 upload 上传文件
    
    例:/usr/bin/fdfs_test /etc/fdfs/client.conf upload /root/测试.png

上传成功后会打印如下内容:
    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.

    [2021-04-26 14:31:18] DEBUG - base_path=/home/FastDFS, 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=xxx.xxx.x.xxx, port=23000

    group_name=group1, ip_addr=xxx.xxx.x.xxx, port=23000
    storage_upload_by_filename
    group_name=group1, remote_filename=M00/00/00/wKgBbmCGXjaAar8VAAhwEakFJfc543.jpg
    source ip address: xxx.xxx.x.xxx
    file timestamp=2021-04-26 14:31:18
    file size=552977
    file crc32=2835686903
    example file url:         
    http://xxx.xxx.x.xxx/group1/M00/00/00/wKgBbmCGXjaAar8VAAhwEakFJfc543.jpg
    storage_upload_slave_by_filename
    group_name=group1, remote_filename=M00/00/00/wKgBbmCGXjaAar8VAAhwEakFJfc543_big.jpg
    source ip address: xxx.xxx.x.xxx
    file timestamp=2021-04-26 14:31:19
    file size=552977
    file crc32=2835686903
    example file url:     
    http://xxx.xxx.x.xxx/group1/M00/00/00/wKgBbmCGXjaAar8VAAhwEakFJfc543_big.jpg

    其中 http://xxx.xxx.x.xxx/group1/M00/00/00/wKgBbmCGXjaAar8VAAhwEakFJfc543.jpg这个
    地址先记下来,下面整合nginx的时候可以用来测试

6.安装FastDFS的nginx模块*****安装过nginx的请先卸载nginx后再整合模块*****

    在 storage server 上安装 nginx 的目的是对外通过 http 访问 storage server 上的文
件。使用 nginx 的模块 FastDFS-nginx-module 的作用是通过 http 方式访问 storage 中
的文件,当 storage 本机没有要找的文件时向源 storage 主机代理请求文件。

    将 fastdfs-nginx-module_v1.16.tar.gz 传 至 fastDFS 的 storage 服 务 器 的
/usr/local/下,执行如下命令:
    cd /usr/local
    tar -zxvf fastdfs-nginx-module_v1.16.tar.gz
    cd fastdfs-nginx-module/src
    修改该目录下的config配置文件
    修改源文件中的这两行
    CORE_INCS="$CORE_INCS /usr/local/include/fastdfs /usr/local/include/fastcommon/"
    CORE_LIBS="$CORE_LIBS -L/usr/local/lib -lfastcommon -lfdfsclient"
    把这两行中的'local'删掉
    CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"
    CORE_LIBS="$CORE_LIBS -L/usr/lib -lfastcommon -lfdfsclient"

    然后将fastdfs-nginx-module/src下的mod_fastdfs.conf复制到/etc/fdfs/下:
    cp /usr/local/fastdfs-nginx-module/src mod_fastdfs.conf /etc/fdfs/
    并修改/etc/fdfs中该文件的内容
    base_path=/home/FastDFS
    tracker_server=xxx.xxx.x.xxx:22122
    url_have_group_name=true     #url 中包含 group 名称
    store_path0=/home/fastdfs/fdfs_storage     #指定文件存储路径
    在incloude 下添加如下命令
    #include http.conf


    最后 将 libfdfsclient.so 拷贝至/usr/lib 下
    cp /usr/lib64/libfdfsclient.so /usr/lib/

7.nginx的安装

将 nginx-1.8.0.tar.gz 拷贝到/usr/local 下
解压 nginx-1.8.0.tar.gz
*****解压完成后,请将压缩包移动到除/usr/local的任意文件夹下,否则会报一个属于同一文件的错误*****
进入 nginx-1.8.0 目录,执行如下配置命令
./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

注意:最后一行为添加Fast的nginx模块,路径一定不要配错了

执行完命令后:
make
make install

如果执行make报错:请 

vi /usr/local/nginx-1.8.0/objs/Makefile

将上图中间的安全级别 -wrong 去掉

8.nginx配置文件修改

修改nginx的配置文件 
vi /usr/local/nginx-1.8.0/conf/nginx.conf

修改用户等级:

修改server:

上图红框部分一定要严格按照配置的路径和属性去填写

9.启动并访问

启动nginx 
 /usr/local/nginx/sbin/nginx -c /usr/local/nginx-1.8.0/conf/nginx.conf 
重启
/usr/local/nginx/sbin/nginx -s reload /usr/local/nginx/conf/nginx.conf 
指定配置文件重启
./nginx -s reload -c /usr/local/nginx/conf/nginx.conf

还记得上面让你记住的图片地址吗,现在就可以直接去测试访问啦!

10.SpringBoot集成

在项目的的yml文件同路径下,创建fdfs_client.conf文件
文件内容为:
# 连接超时时间,针对socket套接字函数connect,默认为30秒
connect_timeout=30000
# 网络通讯超时时间,默认是60秒
network_timeout=60000
# 服务地址端口
tracker_server = 192.168.1.110:22122

创建上传工具类
import org.csource.common.MyException;
import org.csource.fastdfs.*;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.stream.Stream;

public class FastDFSUpload {


    public static String uploadImage(MultipartFile multipartFile) throws IOException, MyException {
        // 1、初始化全局配置。加载一个配置文件。
        String filePath = new ClassPathResource("fdfs_client.conf").getFile().getAbsolutePath();
        ClientGlobal.init(filePath);

        // 2、创建一个TrackerClient对象。
        TrackerClient trackerClient = new TrackerClient();
        // 3、创建一个TrackerServer对象。
        TrackerServer trackerServer = trackerClient.getConnection();
        // 4、声明一个StorageServer对象,null。
        StorageServer storageServer = null;
        // 5、获得StorageClient对象。
        StorageClient storageClient = new StorageClient(trackerServer, storageServer);
        byte[] bytes = multipartFile.getBytes();// 获得上传的二进制对象
        String[] strings = storageClient.upload_file(bytes, "jpg", null);
        Stream.of(strings).forEach(System.out::println);
        String imagurl = "http://xxx.xxx.x.xxx/"+strings[0]+"/"+strings[1];
        //返回值0代表删除成功
        //int result = storageClient.delete_file("group1",         "M00/00/00/wKgAaFyM3fSANCgXAABMnNm0e54098.jpg");
        //System.out.println("result="+result);
        return imagurl;
    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值