Linux配置FastDFS和使用

第一阶段:

第一步:准备环境

因为FastDFS有些文件编译等需要依赖C语言环境;所有执行下面命令安装C环境

# 没有make的需要安装一下make
yum install -y make
yum install -y gcc
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel

# 创建数据存储目录
cd ~
mkdir -p /fdfs/tracker /fdfs/storage /fdfs/logs /fdfs/tmp

第二步:安装 libfastcommon

libfastcommon是从 FastDFS 和 FastDHT 中提取出来的公共 C 函数库,基础环境,安装即可 

cd /usr/local/soft

# 下载libfastcommon
wget https://github.com/happyfish100/libfastcommon/archive/V1.0.7.tar.gz

# 解压文件
tar -zxvf V1.0.7.tar.gz -C /usr/local

cd /usr/local/libfastcommon-1.0.7
#执行下面脚本命令
./make.sh && ./make.sh install

# 复制文件,解决FastDFS中lib配置文件路径问题。
cp /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
cp /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so
#cp /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so
#cp /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so

# 如果上面的不能正常运行就使用下面这种方式
# 创建软连接  FastDFS 默认lib目录是 /usr/local/lib
# ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
# ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so
# ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so
# ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so

第三步:安装 FastDFS

cd /usr/local/soft

#下载
wget https://github.com/happyfish100/fastdfs/archive/V5.05.tar.gz

#解压
tar -zxvf V5.05.tar.gz -C /usr/local

cd /usr/local/fastdfs-5.05/
#执行下面脚本命令
./make.sh && ./make.sh install

第四步:配置FastDFS跟踪器(Tracker)

4.1、拷贝配置文件

# 将配置文件复制到/etc/fdfs目录下
cd /usr/local/fastdfs-5.05/conf
cp * /etc/fdfs/

4.2、配置跟踪器tracker

cd /etc/fdfs
vi tracker.conf

将下面配置信息修改tracker.conf里面的信息

port=22122
# Tracker 数据和日志目录地址(根目录必须存在,子目录会自动创建)
base_path=/fdfs/tracker

# HTTP 服务端口 根据自己需要配置即可
http.server_port=8888

4.3、配置存储器storage

# 在/etc/fdfs路径下打开这个配置文件
vi storage.conf

修改对应的配置信息

注意:下面配置本机IP的时候不能写127.0.0.1或者localhost

# Storage 数据和日志目录地址(根目录必须存在,子目录会自动生成)
base_path=/fdfs/storage

# 逐一配置 store_path_count 个路径,索引号基于 0。
# 如果不配置 store_path0,那它就和 base_path 对应的路径一样。
store_path0=/fdfs/storage

# FastDFS 存储文件时,采用了两级目录。这里配置存放文件的目录个数。 
# 如果本参数只为 N(如: 256),那么 storage server 在初次运行时,会在 store_path 下自动创建 
# N * N 个存放文件的子目录。
subdir_count_per_path=256

# tracker_server 的列表 ,会主动连接 tracker_server
# 有多个 tracker server 时,每个 tracker server 写一行
# 这里的IP地址就是本机IP地址,这里不能写127.0.0.1或者localhost
tracker_server=192.168.0.100:22122

# 允许系统同步的时间段 (默认是全天) 。一般用于避免高峰同步产生一些问题而设定。
sync_start_time=00:00
sync_end_time=23:59

# 访问端口
http.server_port=8888

4.4、配置client

# 在/etc/fdfs路径下打开这个配置文件
vi client.conf

修改一下对应的配置信息

#tracker节点
tracker_server=192.168.0.100:22122
#日志路径
base_path=/fdfs/logs
http.tracker_server_port=8888

第五步:启动FastDFS

# 这里要先启动tracker 再启动storage
fdfs_trackerd /etc/fdfs/tracker.conf start
fdfs_storaged /etc/fdfs/storage.conf start
netstat -unltp|grep fdfs # 查看fdfs运行状态

上面配置没有问题的话,那就可以配置下面配置信息了

第二阶段:

第一步:nginx安装fastdfs插件

1.1、fastdfs-nginx-module下载

cd /usr/local/soft

# 下载fastdfs-nginx-module模块
wget https://github.com/happyfish100/fastdfs-nginx-module/archive/5e5f3566bbfa57418b5506aaefbe107a42c9fcb1.zip

# 如果没有unzip的需要安装后才能解压
yum install unzip 
# 安装过程出现这种问题就填写y 然后按enter
Is this ok [y/d/N]: y  


# 解压
unzip 5e5f3566bbfa57418b5506aaefbe107a42c9fcb1.zip -d /usr/local
# 解压后的文件夹重命名
mv /usr/local/fastdfs-nginx-module-5e5f3566bbfa57418b5506aaefbe107a42c9fcb1 /usr/local/fastdfs-nginx-module

cp -r  /usr/local/fastdfs-nginx-module/ /fdfs/

1.2、nginx安装fastdfs

cd /usr/local/soft

# 下载nginx
wget http://nginx.org/download/nginx-1.12.1.tar.gz

# 解压
tar -zxvf nginx-1.12.1.tar.gz -C /usr/local/

# 安装nginx与fsatdfs-nginx-module
cd /usr/local/nginx-1.12.1

# 安装设置
./configure --prefix=/opt/nginx_fastdfs --sbin-path=/usr/bin/nginx_fastdfs --add-module=/fdfs/fastdfs-nginx-module/src

# 编译
make

# 安装
make install

1.3、 fastdfs插件配置

cd /fdfs/fastdfs-nginx-module/src

# 复制这个配置文件到这个路径下
cp mod_fastdfs.conf /etc/fdfs/

# 创建日志文件夹
mkdir -p /fdfs/tmp

vi /etc/fdfs/mod_fastdfs.conf

修改下面对应的配置信息

connect_timeout=10   #客户端访问文件连接超时时长(单位:秒)
base_path=/fdfs/tmp   #存储日志路径
tracker_server=192.168.0.100:22122   #tracker服务IP和端口
url_have_group_name=true  #访问链接前缀加上组名
group_name=group1  #和storage的groupname一一对应
store_path0=/fdfs/storage  #文件存储路径

1.4、配置nginx

cd /opt/nginx_fastdfs/conf/
vi nginx.conf
# 这个端口必须与storage server的http通信端口一致
listen: 8888
server_name: 192.168.0.100
location ~/group([0-9])/ {
	ngx_fastdfs_module;
}

1.5、启动nginx

nginx_fastdfs

如果有ngx_http_fastdfs_set pid=7257这样类似的信息说明启动成功

1.6、测试

使用xftp传输一张图片至 linux下的/usr/pic目录下,这里以pic01.jpg为例

# 上传文件至fdfs
fdfs_upload_file /etc/fdfs/client.conf /usr/pic/pic01.jpg

结果:

group1/M00/00/00/rB6Gc2N6VYeALlEEAAAMcGdeEj0255.png

上面都没问题的话,那么就可以配置下面的配置了

第三阶段:

在idea中配置如下结果:

获取代码:https://gitee.com/J-D-H/java_fastdfs.git

package org.csource;

import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;

import java.io.*;
import java.util.Arrays;
import java.util.Properties;

/**
 * @author jdh
 * @date 2019/7/13.
 */
public class FdfsTest {

    public static final String GEZI_JPEG = "gezi.jpeg";

    private static final String CONF_NAME = "/fastdfs-client.properties";

    private static StorageClient storageClient;

    private static TrackerServer trackerServer;

    private static String GROUP;

    private static String KEY;

    public static void main(String[] args) throws Exception {
        initStorageClient();   //初始化客户端
        upload();
        download();
        closeClient();
    }

    public static void initStorageClient() throws Exception {
        Properties prop = new Properties();
        prop.load(FdfsTest.class.getResourceAsStream("/fastdfs-client.properties"));
        ClientGlobal.initByProperties(prop);
        TrackerClient tracker = new TrackerClient();
        trackerServer = tracker.getTrackerServer();
        StorageServer storageServer = null;
        storageClient = new StorageClient(trackerServer, storageServer);
    }

    public static void closeClient() {
        if (storageClient != null) {
            try {
                storageClient.close();
            } catch (Exception e) {
                e.printStackTrace();
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    }

    public static void writeByteToFile(byte[] fbyte, String fileName) throws IOException {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = new File(fileName);
        try {
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(fbyte);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                bos.close();
            }
            if (fos != null) {
                fos.close();
            }
        }
        System.out.println("下载完成");
    }

    public static void upload() throws Exception {
        NameValuePair[] metaList = new NameValuePair[1];
        String local_filename = "rev.txt";  //这个是上传在fdfs中文件的名字
        metaList[0] = new NameValuePair("fileName", local_filename);

        //下面是读取本地的文件信息
        File file = new File("C:\\Users\\17851\\Desktop\\ts\\header.jpg");
        InputStream inputStream = new FileInputStream(file);
        int length = inputStream.available();
        byte[] bytes = new byte[length];
        inputStream.read(bytes);

        //客户端开始上传
        String[] result = storageClient.upload_file(bytes, null, metaList);
        GROUP = result[0];
        KEY = result[1];
        System.out.println(Arrays.toString(result));
    }

    public static void download() throws Exception {
//        String[] uploadresult = {"group1", "M00/00/01/CgAED2Px8COAa_XFAAABx93-0dc3216733"};
//        byte[] result = storageClient.download_file(uploadresult[0], uploadresult[1]);
        byte[] result = storageClient.download_file(GROUP, KEY);
        String local_filename = "C:\\Users\\17851\\Desktop\\ts\\header1.jpg";
        writeByteToFile(result, local_filename);
//        File file = new File(local_filename);
    }

    public static void testUploadDownload() throws Exception {
        NameValuePair[] metaList = new NameValuePair[1];
        String local_filename = "build.PNG";
        metaList[0] = new NameValuePair("fileName", local_filename);
        File file = new File("C:\\Users\\17851\\Desktop\\pic\\downLoad\\build.PNG");
        InputStream inputStream = new FileInputStream(file);
        int length = inputStream.available();
        byte[] bytes = new byte[length];
        inputStream.read(bytes);
        String[] result = storageClient.upload_file(bytes, null, metaList);
        byte[] resultbytes = storageClient.download_file(result[0], result[1]);
        writeByteToFile(resultbytes, local_filename);
        File downfile = new File(local_filename);
    }

}

配置文件:

## fastdfs-client.properties 1.15.233.195
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=8888
fastdfs.tracker_servers=192.168.0.100:22122

测试结果:

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
FastDFS是一个开源的轻量级分布式文件系统,可以用于存储大文件,支持文件上传、下载和删除等操作。下面是在Linux下安装和配置FastDFS的步骤: 1. 安装libfastcommon ``` $ git clone https://github.com/happyfish100/libfastcommon.git $ cd libfastcommon/ $ ./make.sh $ ./make.sh install ``` 2. 安装FastDFS ``` $ git clone https://github.com/happyfish100/fastdfs.git $ cd fastdfs/ $ ./make.sh $ ./make.sh install ``` 3. 配置Tracker服务器 在/etc/fdfs目录下创建tracker目录,并在该目录下创建tracker.conf文件,内容如下: ``` #bind_addr= # bind to all IP addresses port=22122 # base_path=/home/yuqing/fastdfs/tracker # the base storage path for storing data and logs # store_group=group1,group2 # the group name list that can be used in this tracker server # group_name=group1 # the group name of this tracker server ``` 4. 配置Storage服务器 在/etc/fdfs目录下创建storage目录,并在该目录下创建storage.conf文件,内容如下: ``` #base_path=/home/yuqing/fastdfs/storage # the base storage path for storing data and logs #store_path0=/home/yuqing/fastdfs/storage # the first storage path for storing data tracker_server=192.168.1.100:22122 #group_name=group1 # the group name of this storage server #http.server_port=8888 # the port that the HTTP server listens on ``` 其中,tracker_server为Tracker服务器的IP和端口号。 5. 启动Tracker和Storage服务器 启动Tracker服务器: ``` $ /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf ``` 启动Storage服务器: ``` $ /usr/bin/fdfs_storaged /etc/fdfs/storage.conf ``` 6. 测试上传和下载文件 上传文件: ``` $ /usr/bin/fdfs_upload_file /etc/fdfs/client.conf test.jpg ``` 下载文件: ``` $ /usr/bin/fdfs_download_file /etc/fdfs/client.conf group1 M00/00/00/test.jpg /tmp/test.jpg ``` 其中,group1为存储文件的组名,M00/00/00/test.jpg为文件的路径和文件名。 以上就是在Linux下安装和配置FastDFS的全部步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值