fastDFS & SpringBoot

  • Docker installation fastDFS
    • 1. Pull the shared FastdFS image
    • 2. Run the fastDFS image
  • Springboot integration fastDFS

Docker installation fastDFS

After the Docker environment is installed on the server, the setup process is simple. Build reference: Docker + Fastdfs + Springboot one-click build distributed file server

The setup process consists of two steps:

1. Pull the shared FastdFS image

docker pull qbanxiaoli/fastdfs
Copy the code

2. Run the fastDFS image

docker run -d \
    --restart=always \
    --privileged=true \
    --net=host \
    --name=fastdfs \
    -e IP=192.168.104.5 \
    -e WEB_PORT=80 \
    -v /usr/soft/fastdfs:/var/local/fdfs \
    qbanxiaoli/fastdfs
Copy the code

-e WEB_PORT=80 Specifies the nginx port. There is no need to install nginx. The fastdFS image already contains Nginx. If there are projects on the host that occupy port 80, this should fail to start.

The nginx welcome page should be displayed when accessing the IP address used above: 192.168.104.5.



Another test method, directly into the docker image inside, using the command, upload an HTML file.

First inside the FastDFS image:

docker exec -it fastdfs /bin/bash
Copy the code

Create a new index.html file:

echo "Hello FastDFS!">index.html
Copy the code

Uploading files:

fdfs_test /etc/fdfs/client.conf upload index.html
Copy the code

If a URL is returned, fastdfs is working.

If the nginx welcome page is not displayed, you need to check whether port 80 is enabled on the firewall. Otherwise, the URL of the returned file cannot be opened after the file is uploaded using the Fastdfs file server.

You also need to open ports 22122 and 23000, otherwise you cannot use FastdFS in your application, because the internal fastDFS tracking server, Tracker_Server, depends on port 22122, and the storage server storage_server depends on port 23000. If the two ports are not opened, the program may report an ERROR: error-file: connection_pool.c, line: 130, connect to IP:23000 fail

Centos 7 Operating firewall

Run the systemctl status firewalld command to enable or disable the firewall. Run the systemctl start firewalld command to disable the firewall. Change start to stop. Firewall-cmd --list-ports Open fixed ports: firewall-cmd --zone=public --add-port=22122Description of the/TCP --permanent command: --zone # scope --add-port=80/ TCP # Add a port. The format is: port/communication protocol. --permanent # PermanentCopy the code

Firewall-cmd –reload command is used to restart the firewall

To open the firewall port on an earlier Linux operating system, run the iptables -I INPUT -p TCP –dport 22122 -j ACCEPT command.

Port 22122 and 23000 are open, but port 80 is not shown in the list. The welcome page of nginx is displayed. The address returned by uploading an image file is also accessible.

Springboot integration fastDFS

Add a Maven dependency, which I used here

Add dependency <dependency> <groupId>com.github. Tobato </groupId> <artifactId>fastdfs-client</artifactId> <version>1.26.7</version>  </dependency>Copy the code

Add the following to the SpringBoot boot class:

@Import(FdfsClientConfig.class)
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
Copy the code

FastDFSClient tool class: Fastdfsclient. Java, if you are using a lower version, the tool class will prompt you to find the method or package, so you may need to adjust the package path in the tool class. There are not many files in the jar package, if you can’t find the package, you can check to see if the package structure of different versions has changed. The package structure of 1.26.7 and 1.26.2 has been adjusted. Fortunately, I have adjusted the path of import by looking at the files in the source package.

package gc.cnnvd.framework.common;
 
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.*;

/** * FastDFSClient tool class *@Auther linmengmeng
 * @DateThe 2021-04-01 * /
@Component
public class FastDFSClient {
 
    private static Logger log = LoggerFactory.getLogger(FastDFSClient.class);
 
    private static FastFileStorageClient fastFileStorageClient;
 
    private static FdfsWebServer fdfsWebServer;
 
    @Autowired
    public void setFastDFSClient(FastFileStorageClient fastFileStorageClient, FdfsWebServer fdfsWebServer) {
        FastDFSClient.fastFileStorageClient = fastFileStorageClient;
        FastDFSClient.fdfsWebServer = fdfsWebServer;
    }
 
    / * * *@paramMultipartFile File object *@returnReturns the file address *@descriptionUpload files */
    public static String uploadFile(MultipartFile multipartFile) {
        try {
            StorePath storePath = fastFileStorageClient.uploadFile(multipartFile.getInputStream(), multipartFile.getSize(), FilenameUtils.getExtension(multipartFile.getOriginalFilename()), null);
            return storePath.getFullPath();
        } catch (IOException e) {
            log.error(e.getMessage());
            return null; }}/ * * *@paramMultipartFile Image object *@returnReturn picture address *@descriptionUpload thumbnail */
    public static String uploadImageAndCrtThumbImage(MultipartFile multipartFile) {
        try {
            StorePath storePath = fastFileStorageClient.uploadImageAndCrtThumbImage(multipartFile.getInputStream(), multipartFile.getSize(), FilenameUtils.getExtension(multipartFile.getOriginalFilename()), null);
            return storePath.getFullPath();
        } catch (Exception e) {
            log.error(e.getMessage());
            return null; }}/ * * *@paramFile File object *@returnReturns the file address *@descriptionUpload files */
    public static String uploadFile(File file) {
        try {
            FileInputStream inputStream = new FileInputStream(file);
            StorePath storePath = fastFileStorageClient.uploadFile(inputStream, file.length(), FilenameUtils.getExtension(file.getName()), null);
            return storePath.getFullPath();
        } catch (Exception e) {
            log.error(e.getMessage());
            return null; }}/ * * *@paramFile Image object *@returnReturn picture address *@descriptionUpload thumbnail */
    public static String uploadImageAndCrtThumbImage(File file) {
        try {
            FileInputStream inputStream = new FileInputStream(file);
            StorePath storePath = fastFileStorageClient.uploadImageAndCrtThumbImage(inputStream, file.length(), FilenameUtils.getExtension(file.getName()), null);
            return storePath.getFullPath();
        } catch (Exception e) {
            log.error(e.getMessage());
            return null; }}/ * * *@paramBytes Byte array *@paramFileExtension fileExtension *@returnReturns the file address *@descriptionUpload the byte array to generate a file */
    public static String uploadFile(byte[] bytes, String fileExtension) {
        ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
        StorePath storePath = fastFileStorageClient.uploadFile(stream, bytes.length, fileExtension, null);
        return storePath.getFullPath();
    }
 
    / * * *@paramFileUrl File access address *@paramFile File saving path *@descriptionDownload the file */
    public static boolean downloadFile(String fileUrl, File file) {
        try {
            StorePath storePath = StorePath.parseFromUrl(fileUrl);
            byte[] bytes = fastFileStorageClient.downloadFile(storePath.getGroup(), storePath.getPath(), new DownloadByteArray());
            FileOutputStream stream = new FileOutputStream(file);
            stream.write(bytes);
        } catch (Exception e) {
            log.error(e.getMessage());
            return false;
        }
        return true;
    }
 
    / * * *@paramFileUrl File access address *@descriptionDelete files */
    public static boolean deleteFile(String fileUrl) {
        if (StringUtils.isEmpty(fileUrl)) {
            return false;
        }
        try {
            StorePath storePath = StorePath.parseFromUrl(fileUrl);
            fastFileStorageClient.deleteFile(storePath.getGroup(), storePath.getPath());
        } catch (Exception e) {
            log.error(e.getMessage());
            return false;
        }
        return true;
    }

    /** * Encapsulate the full URL of the file *@param path
     * @return* /
    public static String getResAccessUrl(String path) {
        String url = fdfsWebServer.getWebServerUrl() + path;
        log.info("Upload file address: {}", url);
        returnurl; }}Copy the code

Configuration file, directly pasted above the blog inside the configuration file, I split the configuration file here, post it will be a bit messy.

# Distributed file system FastdFS configuration
fdfs:
  Timeout duration of socket connection
  soTimeout: 1500
  Specifies the timeout period for connecting to the tracker server
  connectTimeout: 600
  pool:
    # The maximum number of objects borrowed from the pool
    max-total: 153
    The maximum number of milliseconds to wait for a connection is 100
    max-wait-millis: 102
  # thumbnail generation parameter, optional
  thumbImage:
    width: 150
    height: 150
  X :port = -x.x.x. x:port = -x.x.x. x:port
  trackerList:
    - 192.168127.131.: 22122
  #
  Storage_server access address
  web-server-url: http://192.168.127.131/
  spring:
    http:
      multipart:
        max-file-size: 100MB # Maximum supported file size
        max-request-size: 100MB # Maximum request size supported
Copy the code

The test class:

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import gc.cnnvd.SpringBootPlusApplication;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;


@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringBootApplication.class)
public class BaseTest {
    protected final static Log log = LogFactory.get(BaseTest.class);
}
Copy the code
import gc.cnnvd.framework.common.FastDFSClient;
import org.junit.Test;

import java.io.File;

/ * * *@Auther linmengmeng
 * @DateThe 2021-04-01 15:42 * /

public class FastDFSTest extends BaseTest{

    @Test
    public void upload(a){
// String fileUrl = this.getClass().getResource("/test.jpg").getPath();
        String fileUrl = "C:\\Users\\16322\\Desktop\\only-one.jpg";
        File file = newFile(fileUrl); String str = FastDFSClient.uploadFile(file); String resAccessUrl = FastDFSClient.getResAccessUrl(str); System.out.println(resAccessUrl); }}Copy the code

If the upload is successful, the following image will be displayed:



If port 22122 or 23000 of the server is not open, the upload fails, and the console will display a clear message.

In this case, copy the link to the browser, the normal display of the image content