fastdfs · 2021-09-02 0

fastdfs搭建及基本使用

一、创建fastdfs容器

1.拉取镜像

docker pull delron/fastdfs:latest

2.创建容器

docker run -itd -e TZ=Asia/Shanghai -p 8888:8888 -p 8080:8080 -p 22122:22122 -e TRACKER_SERVER=fdfs-test:22122 --name fdfs-test delron/fastdfs:latest

3.进入容器

docker exec -it fdfs-test bash

二、配置fastdfs

1.配置tracker

修改文件 /etc/fdfs/tracker.conf

disabled=false #启用配置文件(默认启用)
port=22122 #设置tracker的端口号,通常采用22122这个默认端口
base_path=/var/fdfs #设置tracker的数据文件和日志目录
http.server_port=8080 #设置http端口号,默认为8080

2.配置storage

配置文件 /etc/fdfs/storage.conf

disabled=false #启用配置文件(默认启用)
group_name=group1 #组名,根据实际情况修改
port=23000 #设置storage的端口号,默认是23000,同一个组的storage端口号必须一致
base_path=/var/fdfs #设置storage数据文件和日志目录
store_path_count=1 #存储路径个数,需要和store_path个数匹配
store_path0=/var/fdfs #实际文件存储路径
tracker_server=fdfs-test:22122 #tracker 服务器的 IP地址和端口号,如果是单机搭建,IP不要写127.0.0.1,否则启动不成功
http.server_port=8888 #设置 http 端口号

3.配置client

配置文件 /etc/fdfs/client.conf

base_path=/var/fdfs #tracker服务器文件路径
tracker_server=fdfs-test:22122 #tracker服务器IP地址和端口号
http.tracker_server_port=8080 # tracker 服务器的 http 端口号,必须和tracker的设置对应起来

4.配置nginx

对于容器已经安装过 ngx_fastdfs_module 模块

配置文件 /etc/fdfs/mod_fastdfs.conf

connect_timeout=10 # 连接超时时间
tracker_server=fdfs-test:22122 # Tracker Server
storage_server_port=23000 # StorageServer 默认端口
url_have_group_name = true # 如果文件ID的uri中包含/group**,则要设置为true
store_path0=/var/fdfs # Storage 配置的store_path0路径,必须和storage.conf中的一致

配置文件 /usr/local/nginx/conf/nginx.conf

nginx访问fastdfs可能有权限问题,所以可以需要配置 user root

user root;

http {
    server {
        listen       8888;
        server_name  localhost;
        location ~/group[0-9]/ {
            ngx_fastdfs_module;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root html;
        }
    }
}

三、启动fastdfs

1.启动tracker

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

2.启动storage

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

3.启动nginx

启动nginx的命令:

/usr/local/nginx/sbin/nginx

关闭 nignx的命令:

/usr/local/nginx/sbin/nginx -s stop

4.检测启动

使用netstat -ltnp,查看端口是否启动

使用/usr/bin/fdfs_monitor /etc/fdfs/storage.conf,查看storage启动状态

四、命令操作fastdfs

1.上传

使用fdfs_upload_file命令,上传文件成功后,会返回一行数据,类似group1/M00/00/00/rBIAAmEveUWAMGZAAAAAIU_qGLA481.txt

/usr/bin/fdfs_upload_file  /etc/fdfs/client.conf  /root/test.txt

2.下载

/usr/bin/fdfs_download_file /etc/fdfs/client.conf group1/M00/00/00/rBIAAmEveUWAMGZAAAAAIU_qGLA481.txt

3.删除文件

/usr/bin/fdfs_delete_file /etc/fdfs/client.conf group1/M00/00/00/rBIAAmEveUWAMGZAAAAAIU_qGLA481.txt

4.web访问

http://localhost:8888/group1/M00/00/00/rBIAAmEveUWAMGZAAAAAIU_qGLA481.txt

五、java操作fastdfs

在github上下载 fastdfs-client-java 包,使用mvn install,安装到本地仓库

maven引入

<dependency>
    <groupId>org.csource</groupId>
    <artifactId>fastdfs-client-java</artifactId>
    <version>1.28-SNAPSHOT</version>
</dependency>

1.上传

@Test
public void testUpload() throws Exception {
    Properties prop = new Properties();
    prop.put("fastdfs.tracker_servers", "localhost:22122");
    // 加载配置
    ClientGlobal.initByProperties(prop);

    //创建客户端
    // new TrackerClient() 方法把 tracker 地址信息赋值给 tracker_group
    TrackerClient trackerClient = new TrackerClient();
    TrackerServer trackerServer = trackerClient.getTrackerServer();

    StorageClient storageClient = new StorageClient(trackerServer, null);

    // 参数1 文件路径, 参数2 文件的扩展名, 参数3 文件的元数据
    String[] strings = storageClient.upload_file("/home/zxm/test/t.txt", null, null);
    for (String string : strings) {
        // 返回的结果就是整个地址,不包括域名 ip
        System.out.println(string);
    }
    // group_name remoteFileName
    // group1
    // M00/00/00/rBIAAmEwLXyAUGdyAAAABUU3ntA600.txt
}

2.下载

@Test
public void testDownload() throws Exception {
    Properties prop = new Properties();
    prop.put("fastdfs.tracker_servers", "localhost:22122");
    // 加载配置
    ClientGlobal.initByProperties(prop);

    //创建客户端
    // new TrackerClient() 方法把 tracker 地址信息赋值给 tracker_group
    TrackerClient trackerClient = new TrackerClient();
    TrackerServer trackerServer = trackerClient.getTrackerServer();

    StorageClient storageClient = new StorageClient(trackerServer, null);

    String groupName = "group1";
    String remoteFileName = "M00/00/00/rBIAAmEwLXyAUGdyAAAABUU3ntA600.txt";
    byte[] bytes = storageClient.download_file(groupName, remoteFileName);

    String s = new String(bytes);
    System.out.println(s);

    FileInfo fileInfo = storageClient.query_file_info(groupName, remoteFileName);
    System.out.println(fileInfo);
}

3.删除文件

@Test
public void testDelete() throws Exception {
    Properties prop = new Properties();
    prop.put("fastdfs.tracker_servers", "localhost:22122");
    // 加载配置
    ClientGlobal.initByProperties(prop);

    //创建客户端
    // new TrackerClient() 方法把 tracker 地址信息赋值给 tracker_group
    TrackerClient trackerClient = new TrackerClient();
    TrackerServer trackerServer = trackerClient.getTrackerServer();

    StorageClient storageClient = new StorageClient(trackerServer, null);

    String groupName = "group1";
    String remoteFileName = "M00/00/00/rBIAAmEwLXyAUGdyAAAABUU3ntA600.txt";

    int flag = storageClient.delete_file(groupName, remoteFileName);

    System.out.println(flag);
    if (flag == 0) {
        System.out.println("delete success");
    }
}