FastDFS + Nginx文件上传

Nginx

环境依赖

  • gcc

yum install gcc-c++

  • pcre

yum install -y pcre pcre-devel

  • zlib

**yum install -y zlib zlib-devel

  • openssl

yum install -y openssl openssl-devel

具体操作

  • 下载

wget http://nginx.org/download/nginx-1.12.0.tar.gz

  • 解压

tar -zxvf nginx-1.12.0.tar.gz

  • 配置

./configure

  • 编译、安装

make && make install

  • 验证

cd /usr/local/nginx,存在conf、sbin、html文件夹,安装成功!

使用

  • 启动

/usr/local/nginx/sbin/nginx,使用虚拟机ip进行访问。

  • 停止

cd /usr/local/nginx/sbin && ./nginx -s quit(推荐)

  • 再启
    • 先停再启 cd /usr/local/nginx/sbin && ./nginx -s quit && ./nginx(推荐)
    • 直接重启 cd /usr/local/nginx/sbin && ./nginx -s reload

FastDFS

Tracker

  • 环境、库、安装包
    • gcc yum install gcc-c++
    • libevent yum -y install libevent
    • libfastcommon_url_1 cd /usr/local && tar -zxvf libfastcommon-1.0.43.tar.gz && cd libfastcommon-1.0.43 && ./make.sh && ./make.sh install
    • tracker_url_2 cd /usr/local/ && tar -zxvf fastdfs-6.06.tar.gz && cd fastdfs-6.06/ && ./make.sh && ./make.sh install
  • 配置文件拷贝

cd conf/ && cp ./* /etc/fdfs/

  • 配置

    cd /etc/fdfs/ && vim tracker.conf

    • 端口保持默认:22122
    • 修改元数据存储目录:base_path = /home/dj/dev/fastdfs
  • 启动

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

Storage

  • 环境、库、安装包

    • libevent、libfastcommon、storage(同tracker)
  • 配置

    cd /etc/fdfs/ && vim storage.conf

    • 修改:base_path = /home/dj/dev/fastdfs
    • 修改:store_path0 = /home/dj/dev/fastdfs
    • 修改:tracker_server = 192.168.1.11:22122(保留一个即可)
  • 启动

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

fastdfs-nginx-module

下载

fastdfs-nginx-module_url:

cd /usr/local && tar -zxvf fastdfs-nginx-module-1.22.tar.gz && cd fastdfs-nginx-module-1.22

文件拷贝

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

配置

vim /etc/fdfs/mod_fastdfs.conf

  • 修改:tracker_server=192.168.1.11:22122
  • 修改:url_have_group_name = true
  • 修改:store_path0:/home/dj/dev/fastdfs

重新编译、安装

前往nginx安装目录并执行:

cd /home/dj/dev/nginx/nginx-1.12.0/ && ./configure --add-module=/usr/local/fastdfs-nginx-module-1.22/src && make && make install

修改配置文件

vim /usr/local/nginx/conf/nginx.conf

location ~/group([0-9]) {
	ngx_fastdfs_module;
}

Java客户端

建一个Maven工程

  • 添加依赖
<dependency>
    <groupId>net.oschina.zcx7878</groupId>
    <artifactId>fastdfs-client-java</artifactId>
    <version>1.27.0.0</version>
</dependency>
  • 配置文件
fastdfs.connect_timeout_in_seconds = 5
fastdfs.network_timeout_in_seconds = 30
fastdfs.charset = UTF-8
fastdfs.http_anti_steal_token = false
fastdfs.http_secret_key = FastDFS1234567890
fastdfs.http_tracker_http_port = 80
fastdfs.tracker_servers = 192.168.1.11:22122
fastdfs.connection_pool.enabled = true
fastdfs.connection_pool.max_count_per_entry = 500
fastdfs.connection_pool.max_idle_time = 3600
fastdfs.connection_pool.max_wait_time_in_ms = 1000

文件上传

package top.dj;

import lombok.extern.slf4j.Slf4j;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;

/**
 * @Author: DengJia
 * @Date: 2021/9/7 21:49
 * @Description:
 */

@SpringBootTest
@Slf4j
public class DFSTest {

    private final static Logger logger = LoggerFactory.getLogger(DFSTest.class);

    @Test
    void testUpload() {
        try {
            ClientGlobal.initByProperties("fastdfs-client.properties");
            TrackerClient tracker = new TrackerClient();
            TrackerServer trackerServer = tracker.getConnection();
            StorageServer storageServer = null;
            StorageClient1 client = new StorageClient1(trackerServer, storageServer);
            NameValuePair nvp[] = null;
            //上传到文件系统
            String fileId = client.upload_file1("F:\\ps\\centos-docker.png", "png", nvp);
            logger.info(fileId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

上一篇:手把手教你用 FastDFS 构建分布式文件管理系统


下一篇:分布式文件系统技术选型