Use docker to install FastDFS and use it simply

提示:以下是本篇文章正文内容,下面案例可供参考

1. About FastDFS

1. Internal composition of FastDFS

  1. FastDFS is a high-performance distributed file system.
  2. FastDFS is composed of Tracker server (tracking scheduling server) and Storage server (file storage server).
  3. Storage server (file storage server) is composed of multiple groups

2. Explanation of terms

Tracker server (tracking scheduling server)
Tracker server (tracking scheduling server), the role is load balancing and scheduling, through the Tracker server when uploading files, you can find the Storage server to provide file uploading services according to some strategies, so the tracker server is called For tracking servers or scheduling servers.

Storage server (file storage server)
Storage server (file storage server) is used for file storage. The files uploaded by the client are finally stored on the Storage server. The Storage server does not implement its own file system but uses the operating system file system to manage files, so storage is called a storage server.

Group (group)

  1. Since the Storage server (file storage server) is used to store files, it has a limited capacity. In order to solve this problem, expansion grouping is proposed, so the concept of group is extended.
  2. Each group stores some files, and the files saved in each group are different.
  3. In order to ensure the high availability of node services in each group, storage server clusters are allowed to be built in the group. Each group can have multiple members. The content stored in the group members is the same, and the status of the group members is consistent. There is no master-slave concept.
  4. Since the files are stored on the nodes of each group, in order to facilitate http access calls, each Storage server is also bound to an nginx.
  5. All groups add up to a completed Storage server (file storage server).
    insert image description here

3. Working principle Upload

insert image description here

  1. The Storage server (file storage server) regularly reports the status of each node to the Tracker server (tracking scheduling server), such as: disk remaining space, file synchronization status;
  2. When the Tracker server (tracking scheduling server) receives the upload request from the client;
  3. Tracker server (tracking scheduling server) will query the available Storage server (file storage server);
  4. Return information such as the IP and port of the available Storage server (file storage server) to the user;
  5. The client uploads the file to the available Storage server (file storage server) according to the returned IP and port;
  6. Storage server (file storage server) generates file id (path information and file name);
  7. And write the uploaded file into the disk of Storage server (file storage server);
  8. Return the generated file id (path information and file name) to the client;
  9. The client will store the file id (path information and file name) for query and download;

4. Working principle download

insert image description here

  1. The Storage server (file storage server) regularly reports the status of each node to the Tracker server (tracking scheduling server), such as: disk remaining space, file synchronization status;
  2. When the Tracker server (tracking scheduling server) receives the blind download request from the client;
  3. Tracker server (tracking scheduling server) will query the available Storage server (file storage server);
  4. Return information such as the IP and port of the available Storage server (file storage server) to the client;
  5. The client requests the available Storage server (file storage server) according to the file id (path information and file name);
  6. Storage server (file storage server) queries files according to file id (path information and file name);
  7. Storage server (file storage server) returns the queried file to the client for download;

Two, docker install FastDFS

1. Installation steps

  1. Pull the fastdfs mirror
 docker pull delron/fastdfs 
  1. Create a Tracker server (tracking scheduling server) container
    提示:在指定虚拟机镜像之后,还需要添加tracker命令,这样镜像就会根据tracker命令启动tracker服务
docker run -d  --name tracker --net=host -p 22122:22122 delron/fastdfs tracker

–net=host : Apply the network of the virtual machine to the container, that is to say, it is consistent with the host network

  1. Storage server (file storage server), you need to specify the ip and port of the Tracker
    提示:在指定虚拟机镜像之后,还需要添加storager命令,这样镜像就会根据storage命令启动storage服务
docker run -d --name storage --net=host  -p 8888:8888  -p 23000:23000  -e TRACKER_SERVER=192.168.136.160:22122 -e GROUP_NAME=group1 delron/fastdfs storage
  1. GROUP_NAME=group1 : Specify that the server is in the group group1 or add a new group called group1. If you want to add a new group for capacity expansion, run this command again and change the new group name.
  2. –net=host : Apply the network of the virtual machine to the container, that is to say, it is consistent with the host network

2. Modify nginx configuration

Nginx has been integrated inside the storage, and nginx here can make pictures accessible in the browser

  1. Enter the Storage container
    提示:进入Storage容器内【cd /etc/fdfs/ 】也有Storage和Tracker的配置
docker exec -it storage /bin/bash
  1. Edit the file [nginx.conf] file
 vi /usr/local/nginx/conf/nginx.conf 

insert image description here
3. If i has made configuration changes, it needs to be restarted

docker restart storage

3. Use steps

1. Import coordinates

     <!--fastdfs文件存储-->
        <dependency>
            <groupId>com.github.tobato</groupId>
            <artifactId>fastdfs-client</artifactId>
            <version>1.26.7</version>
            <exclusions>
                <exclusion>
                    <groupId>ch.qos.logback</groupId>
                    <artifactId>logback-classic</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

2. Add configuration

Add configuration in [application.yml]

fdfs:
  so-timeout: 1500 #读取超时时间
  connect-timeout: 600 #连接超时时间
  thumb-image: #缩略图参数
    width: 150
    height: 150
  tracker-list: 192.168.136.160:22122 #tracker服务器地址 可配置多个
  web-server-url: http://192.168.136.160:8888 #访问路径 storage中nginx地址

insert image description here

3. Code writing

import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.tanhua.server.AppServerApplication;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.File;
import java.io.IOException;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = AppServerApplication.class)
public class FastDFSTest {
    
    
    @Autowired
    protected FastFileStorageClient storageClient;
    @Autowired
    private FdfsWebServer fdfsWebServer;

    @Test
    public void testUpload() {
    
    
        String path = "D:\\IMAGE\\2e8e06bd-bfe3-4af3-9540-83eb20982656.jpg";
        File file = new File(path);
        try {
    
    
            //上传图片
            StorePath storePath = this.storageClient.uploadFile(FileUtils.openInputStream(file), file.length(), "jpg", null);
            //拼接路径   可通过该路径访问上传的照片  http://192.168.136.160:8888/group1/M00/00/00/wKiIoGMB7PmAUPZZAAHMYGEwMhg147.jpg
            String url = fdfsWebServer.getWebServerUrl() + "/" + storePath.getFullPath();
            System.out.println(url);
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }
}

Guess you like

Origin blog.csdn.net/packge/article/details/126451355