SpringBoot2 integrates FastDFS middleware for distributed file management

Keywords: Programming github Spring SpringBoot

Source code for this article: GitHub. Click here || GitEE. Click here

1. Introduction to FastDFS

1. Function of FastDFS

FastDFS is an open source, lightweight, distributed file system that manages files, including file storage, file synchronization, file upload, file download, and so on. It solves the problems of mass storage and load balancing.

Install connection:

Detailed installation process

2. Core Role

FastDFS consists of a trackerserver, a storage server, and a client.

1) Tracking Server

The coordinator of FastDFS, who manages all storage servers and groups, connects each store to the Tracker after it starts, informs him of the group he belongs to, and maintains a periodic heart rate. Based on the heart rate information of the store, the tracker establishes a map of the group to the [storage server list].

2) Storage Server

As a group, a group contains more than one storage machine, data is backed up each other, and storage space is based on the smallest storage volume in the group. Therefore, it is recommended that multiple storage within the group be configured as equally as possible to avoid wasting storage space.

3) Client

The originator of a business request uses the TCP/IP protocol to interact with the tracker server or storage node through a proprietary interface.

3. Operation process

1. Store service to upload status information to tracking service regularly;
2. The client initiates the request;
3. Tracker synchronizes memory state, returns storage service port and IP;
4. Clients perform file operations (upload, download), etc.

Integration with SpringBoot2

1. Core Steps

1) Configure FastDFS execution environment
 2) File upload configuration
 3) Integrate Swagger2 test interface

2. Core Dependency

<!-- FastDFS rely on -->
<dependency>
    <groupId>com.github.tobato</groupId>
    <artifactId>fastdfs-client</artifactId>
    <version>1.26.5</version>
</dependency>
<!-- Swagger2 Core Dependency -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.6.1</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.6.1</version>
</dependency>

3. Configure FastDFS

0) Core Profile

fdfs:
  # Link Timeout
  connect-timeout: 60
  # Read Time
  so-timeout: 60
  # Generate thumbnail parameters
  thumb-image:
    width: 150
    height: 150
  tracker-list: 192.168.72.130:22122

1) Core Configuration Class

@Configuration
@Import(FdfsClientConfig.class)
// Problems with Jmx duplicate registration bean s
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class DfsConfig {
}

2) File Tool Class

@Component
public class FileDfsUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(FileDfsUtil.class);
    @Resource
    private FastFileStorageClient storageClient ;
    /**
     * Upload Files
     */
    public String upload(MultipartFile multipartFile) throws Exception{
        String originalFilename = multipartFile.getOriginalFilename().
                                  substring(multipartFile.getOriginalFilename().
                                  lastIndexOf(".") + 1);
        StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage(
                              multipartFile.getInputStream(),
                              multipartFile.getSize(),originalFilename , null);
        return storePath.getFullPath() ;
    }
    /**
     * Delete Files
     */
    public void deleteFile(String fileUrl) {
        if (StringUtils.isEmpty(fileUrl)) {
            LOGGER.info("fileUrl == >>File path is empty...");
            return;
        }
        try {
            StorePath storePath = StorePath.parseFromUrl(fileUrl);
            storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
        } catch (Exception e) {
            LOGGER.info(e.getMessage());
        }
    }
}

4. File upload configuration

spring:
  application:
    name: ware-fast-dfs
  servlet:
    multipart:
      enabled: true
      max-file-size: 10MB
      max-request-size: 20MB

5. Configure Swagger2

It is mainly used to generate a test interface for file upload.

1) Configuration code class

@Configuration
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.fast.dfs"))
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("SpringBoot utilize Swagger structure API File")
                .description("Use RestFul style, Creator: Smiled")
                .termsOfServiceUrl("https://github.com/cicadasmile")
                .version("version 1.0")
                .build();
    }
}

2) Start class annotations

@EnableSwagger2

3. Demonstration Cases

1. Interface Code

@RestController
public class FileController {
    @Resource
    private FileDfsUtil fileDfsUtil ;
    /**
     * File Upload
     */
    @ApiOperation(value="Upload Files", notes="test FastDFS File Upload")
    @RequestMapping(value = "/uploadFile",headers="content-type=multipart/form-data", method = RequestMethod.POST)
    public ResponseEntity<String> uploadFile (@RequestParam("file") MultipartFile file){
        String result ;
        try{
            String path = fileDfsUtil.upload(file) ;
            if (!StringUtils.isEmpty(path)){
                result = path ;
            } else {
                result = "Upload failed" ;
            }
        } catch (Exception e){
            e.printStackTrace() ;
            result = "Service Exception" ;
        }
        return ResponseEntity.ok(result);
    }
    /**
     * File Delete
     */
    @RequestMapping(value = "/deleteByPath", method = RequestMethod.GET)
    public ResponseEntity<String> deleteByPath (){
        String filePathName = "group1/M00/00/00/wKhIgl0n4AKABxQEABhlMYw_3Lo825.png" ;
        fileDfsUtil.deleteFile(filePathName);
        return ResponseEntity.ok("SUCCESS") ;
    }
}

2. Execution process

1. Access the http://localhost:7010/swagger-ui.html test interface
 2. Call the file upload interface to get the file path in the FastDFS service
 3. Browser access: http://192.168.72.130/group1/M00/00/wKhIgl0n4AKABxQEABhlMYw_3Lo825.png
 4. Call Delete Interface, Delete Pictures on Server
 5. Empty the browser cache, access the picture Url again, return 404

4. Source code address

GitHub Address: Know a smile
https://github.com/cicadasmile/middle-ware-parent
 Code Cloud Address: Know a smile
https://gitee.com/cicadasmile/middle-ware-parent

Posted by faraco on Mon, 02 Sep 2019 17:35:44 -0700