FastDFS 搭建笔记

403 阅读2分钟

1 安装 Tracker

注:在编辑配置文件的过程中,ip 需写服务器外网 ip,亲测写 127.0.0.1 会报错

#一个环境
yum install gcc-c++
​
#两个库,第一个 libevent
yum -y install libevent
# 安装第二个库 libfastcommon
cd /usr/local
wget https://github.com/happyfish100/libfastcommon/archive/V1.0.43.tar.gz
tar -zxvf V1.0.43.tar.gz
./make.sh
./make.sh install
​
#一个安装包 tracker
cd /usr/local
wget https://github.com/happyfish100/fastdfs/archive/V6.06.tar.gz
tar -zxvf V6.06.tar.gz
cd fastdfs-6.06/
./make.sh
./make.sh install
#安装成功后将安装目录内 conf 目录下的配置文件拷贝到 /etc/fdfs 目录下
cd conf/
cp ./* /etc/fdfs/
​
#进入 /etc/fdfs 下修改配置
cd /etc/fdfs
vi tracker.conf
#启动 tracker
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start
​

2 安装 Storage

Storage 同样依赖 libevent 和 libfastcommon 这两个库,在同一台机器上,安装了 Tracker 时相当于已经安装了 Storage,只是需要配置 Storage

# 进入/etc/fdfs 配置 Storage
# 配置完成后,启动
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf start

3 安装 Nginx

Nginx 可以算是 FasfDFS 的重要搭档

步骤分为两步

  • 安装 nginx
  • 首先在 Storage 下安装 fasfdfs-nginx-module
  • 安装 nginx
cd /usr/local
wget http://nginx.org/download/nginx-1.17.0.tat.gz
tar -zxvf nginx-1.17.0.tat.gz
yum -y install pcre-devel
yum -y install openssl openssl-devel
​
#解压完成后进入安装目录开始编译安装
cd nginx-1.17.0
./configure
make
make install
​
#安装完成后,默认位置在 /usr/local/nginx/sbin/nginx
cd /usr/local/nginx/sbin
#启动 nginx
./nginx
# 如果修改了配置文件,即可通过以下命令重新加载
./nginx -s reload
  • 安装 fastdfs-nginx-module
# cd /usr/local
wget https://github.com/happyfish100/fastdfs-nginx-module/archive/V1.22.tar.gz
tar -zxvf V1.22.tar.gz
#将/usr/local/fastdfs-nginx-module-1.22/src/mod_fastdfs.conf 文件拷贝到 /etc/fdfs/ 目录下,并修改内容
cd /fastdfs-nginx-module-1.22/src/
cp mod_fastdfs.conf /etc/fdfs
​
# 进入到nginx 解压目录
cd /usr/local/nginx-1.17.0
./configure --add-module=/usr/local/fastdfs-nginx-module-1.22/src
make
make install
​
#然后按照下图红线修改 nginx 配置文件
vi /usr/local/nginx/conf/nginx.conf
# 修改完成后重新启动或加载 nginx
./nginx -s reload
​

image-20210806114037658

4 Java 客户端测试

  • 创建一个 maven 工程,在 pom 中引入相关依赖
<dependency>
    <groupId>net.oschina.zcx7878</groupId>
    <artifactId>fastdfs-client-java</artifactId>
    <version>1.27.0.0</version>
</dependency>
  • 在 resources 下新建 fastdfs 的配置文件
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=IP:PORT
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
  • 编写上传测试案例

注:NameValuePair为org.csource.common.NameValuePair,log 是 lombok 插件提供的,在类上使用@Slf4j 注解

@Test
    public 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("/Users/markcwg/Pictures/background9.jpg", "jpg", nvp);
            log.info(fileId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
​
//测试结果
16:02:41.099 [main] INFO com.study.springbootdemo.FastDfsTest - group1/M00/00/00/ecSfkGEM7KGAJwAHABGW7rUy8jQ119.jpg
  
​
  • 编写下载案例
@Test
    public void testDownload() {
        try {
            ClientGlobal.initByProperties("fastdfs-client.properties");
            TrackerClient tracker = new TrackerClient();
            TrackerServer trackerServer = tracker.getConnection();
            StorageServer storageServer = null;
            StorageClient1 client = new StorageClient1(trackerServer, storageServer);
            byte[] bytes = client.download_file1("group1/M00/00/00/ecSfkGEM7KGAJwAHABGW7rUy8jQ119.jpg");
            FileOutputStream fos = new FileOutputStream(new File("/Users/markcwg/Desktop/666.jpg"));
            fos.write(bytes);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

\