fastDFS使用操作

107 阅读2分钟

1.3 FastDFS服务端搭建

(1)虚拟机中拉取镜像

docker pull morunchang/fastdfs

(2)运行tracker


docker run -d --name tracker --restart=always --net=host morunchang/fastdfs sh tracker.sh

(3)运行storage


docker volume create fdfs-data
docker run -d --name storage --net=host -e TRACKER_IP=192.168.211.128:22122 -e GROUP_NAME=group1 -v fdfs-data:/data/fast_data morunchang/fastdfs sh storage.sh

说明:


使用的网络模式是–net=host, 192.168.211.128是宿主机的IP
group1是组名,即storage的组
如果想要增加新的storage服务器,再次运行该命令,注意更换 新组名

查询storage是否启动成功:


docker logs -f storage

有以下Successfully connect to tracker server ....

设置开启自启动(可以不做):

docker update --restart=always tracker
​

【ECS服务器】的同学:网络安全: 进站与出站 开放TCP端口 22122 8080 23000 8888

(4)修改nginx

Nginx在这里主要提供对FastDFS图片访问的支持,Docker容器中已经集成了Nginx,我们需要修改nginx的配置,进入storage的容器内部,修改nginx.conf

(4.1)进入容器

docker exec -it storage /bin/bash

(4.2)修改配置

vi /etc/nginx/conf/nginx.conf

修改访问端口为如下:

(4.3) 重启storage

docker restart storage

小结

  1. 创建数据卷
  2. 创建tracker
  3. 创建storage, 看日志 docker logs -f storage
  4. 进入storage容器,修改nginx的端口
  5. 重启storage

【注意】:不正常关闭虚拟,可能导致报错 找不到访问节点。

  1. 重启dokcer,再启动storage
  2. storage容器没有启动,docker start storage pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
​
    <groupId>com.itheima</groupId>
    <artifactId>leadnews-fastdfs-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.8.RELEASE</version>
    </parent>
​
    <dependencies>
        <!--fastdfs-->
        <dependency>
            <groupId>com.github.tobato</groupId>
            <artifactId>fastdfs-client</artifactId>
            <version>1.26.5</version>
        </dependency>
​
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
​
    </dependencies>
​
​
</project>

(3)编写启动类


package com.itheima;
​
import com.github.tobato.fastdfs.FdfsClientConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Import;
​
/**
 * @version 1.0
 * @description 标题
 * @package com.itheima
 */
@SpringBootApplication
public class FastdfsApplication {
    public static void main(String[] args) {
        SpringApplication.run(FastdfsApplication.class,args);
    }
}
​

(5)yml中


fdfs:
  so-timeout: 1501
  connect-timeout: 601
  thumb-image:             #缩略图生成参数
    width: 150
    height: 150
  tracker-list:            #TrackerList参数,支持多个
    - 192.168.211.128:22122
  web-server-url: http://192.168.211.128/

1.4.2 编写测试实现文件的基本操作

编写测试类test下创建如下类 com.itheima.FastadfsTest:


package com.itheima;
​
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.proto.storage.DownloadCallback;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.StringUtils;
​
import java.io.*;
​
/**
 * @version 1.0
 * @description 标题
 * @package com.itheima
 */@SpringBootTest
public class FastadfsTest {
​
    @Autowired
    private FastFileStorageClient storageClient;
​
    //上传图片
    @Test
    public void uploadFile() throws IOException {
​
        //创建流对象
        File file = new File("C:\Users\admin\Pictures\45.png");
        FileInputStream inputStream = new FileInputStream(file);
        long length = file.length();
        //获取文件的扩展名不带点
        String extName = StringUtils.getFilenameExtension(file.getName());
        //上传图片
        StorePath storePath = storageClient.uploadFile(
                inputStream,
                length,
                extName,
                null);
​
        System.out.println(storePath);
        System.out.println(storePath.getFullPath());
    }
​
    //删除图片
    //
    @Test
    public void deleteFile() {
        //group + path
        storageClient.deleteFile("group1/M00/00/00/wKjTiF_BIUqAMwDrAAAl8vdCW2Y127.png");
    }
​
    //下载图片
    @Test
    public void download() throws Exception{
        byte[] group1s = storageClient.downloadFile("group1", "M00/00/00/wKjTiF_BIrCAAn9IAAAl8vdCW2Y205.png", new DownloadCallback<byte[]>() {
            @Override
            public byte[] recv(InputStream ins) throws IOException {
​
                //获取字节数组
                byte[] bytes = IOUtils.toByteArray(ins);
                return bytes;
            }
        });
​
        //下载
        FileOutputStream fileOutputStream = new FileOutputStream(new File("e:/abc.png"));
        fileOutputStream.write(group1s);
        fileOutputStream.close();
    }
​
​
}
​