SpringBoot2.2+commons-pool2 implements a complete project of multiple Ftp connection pools. It can be used out of the box and is stable and reliable after long-term production use.

1 Overview

In our actual production projects, whether it is uploading or downloading, you need to connect to Ftp first, and then perform the corresponding operations. If you can directly get the Ftp client when performing upload and download, will it save a lot of performance? This article is just like the database connection pool. It implements the ftp connection pool through Springboot+commons-pool2, and can configure multiple ftp connection pools. Let's take a look.

Linux command online tool: https://tools.qzxdp.cn/linux_command
Personal blog website: https://www.qzxdp.cn

2. The overall structure of the project

3. Project code

  • Project jar package dependencies
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.10.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <maven.version>3.5.0</maven.version>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
        <java.version>1.8</java.version>
        <maven-source-plugin.version>3.0.1</maven-source-plugin.version>
        <maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
        <maven-enforcer-plugin.version>3.0.0-M1</maven-enforcer-plugin.version>
        <maven-javadoc-plugin.version>3.0.1</maven-javadoc-plugin.version>
        <maven-release-plugin.version>2.5.3</maven-release-plugin.version>
        <docker-maven-plugin.version>0.4.14</docker-maven-plugin.version>
        <jacoco-maven-plugin.version>0.7.9</jacoco-maven-plugin.version>
        <spring-boot.version>2.2.10.RELEASE</spring-boot.version>
        <spring-cloud.version>Hoxton.SR8</spring-cloud.version>
        <spring-cloud-alibaba.version>2.2.5.RELEASE</spring-cloud-alibaba.version>
        <commons-lang3.version>3.5</commons-lang3.version>
        <fastjson.version>1.2.60</fastjson.version>
        <lombok.version>1.18.8</lombok.version>
        <logback.version>1.2.3</logback.version>
        <guava.version>23.0</guava.version>
        <hutool.version>5.3.10</hutool.version>
        <commons-net.version>3.6</commons-net.version>
        <commons-pool2.version>2.7.0</commons-pool2.version>
        <tika.version>1.22</tika.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!--spring cloud alibaba-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>            
            <!--json格式化-->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>${fastjson.version}</version>
            </dependency>

            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
            </dependency>
            <dependency>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct-jdk8</artifactId>
                <version>${mapstruct.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
                <version>${commons-lang3.version}</version>
            </dependency>
            <!--google 工具类-->
            <dependency>
                <groupId>com.google.guava</groupId>
                <artifactId>guava</artifactId>
                <version>${guava.version}</version>
            </dependency>
            <!--hutool工具类-->
            <dependency>
                <groupId>cn.hutool</groupId>
                <artifactId>hutool-all</artifactId>
                <version>${hutool.version}</version>
            </dependency>
            <!--hutool工具类-->
            <dependency>
                <groupId>cn.hutool</groupId>
                <artifactId>hutool-all</artifactId>
                <version>${hutool.version}</version>
            </dependency>
            <dependency>
                <groupId>commons-net</groupId>
                <artifactId>commons-net</artifactId>
                <version>${commons-net.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-pool2</artifactId>
                <version>${commons-pool2.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <!--json格式化-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
        </dependency>
        <!--ftp 依赖-->
        <dependency>
            <groupId>commons-net</groupId>
            <artifactId>commons-net</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>
        <!--spring-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <!--junit-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <!--文件类型检测-->
        <dependency>
            <groupId>org.apache.tika</groupId>
            <artifactId>tika-core</artifactId>
            <version>${tika.version}</version>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <!--deploy 时忽略-->
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>

  • project properties

package com.faea.properties;


import lombok.Getter;
import lombok.Setter;

/**
 * 单Ftp 属性配置
 *
 * @author liuchao
 * @date 2020-03-28
 */
@Getter
@Setter
public class FtpProperties {

    /**
     * Ip
     */
    private String host;

    /**
     * 端口
     */
    private Integer port;

    /**
     * 登录账号
     */
    private String name;

    /**
     * 登录密码
     */
    private String password;

    /**
     * 访问前缀
     */
    private String urlPrefix;
    /**
     * 是否被动模式
     */
    private boolean passiveMode = false;
    /**
     * 编码格式
     */
    private String encoding = "UTF-8";
    /**
     * 连接超时时间
     */
    private int connectTimeout = 30000;
    /**
     * 缓存
     */
    private int bufferSize = 8096;

    /**
     * 初始化连接池个数
     */
    private int initPoolSize = 4;

    /**
     * 最大连接池个数
     */
    private int maxPoolSize = 15;
}
package com.faea.properties;

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 多Ftp配置兼容
 *
 * @author liuchao
 * @date 2020-03-28
 */
@Getter
@Setter
@ConfigurationProperties(prefix = MultiFtpProperties.PREFIX, ignoreUnknownFields = false)
public class MultiFtpProperties {
    public static final String PREFIX = "faea.ftp-multi";


    @NestedConfigurationProperty
    private Map<String, FtpProperties> map = new LinkedHashMap<>();
}
  • Configure multiple Ftp yml configurations
faea:
  ftp-multi:
    map:
      master:
        host: 192.168.13.77
        port: 21
        name: test
        password: test_123
      other-ftp:
        host: 192.168.13.78
        port: 21
        name: test
        password: test_1112     

4. View the current usage of each connection pool through the endpoint

The connection availability check in the connection pool is implemented through the daemon thread, and the maximum and minimum number of connections are configured to prevent the number of Ftp connections from being full. After long-term production project practice, it is stable and reliable. Now I share it with friends in need. I hope it can help you. If you have any questions during use, please leave a message to share.

5. If necessary, please visit the link to download:

SpringBoot2.2+commons-pool2 implements a complete project of multiple Ftp connection pools, which is ready to use out of the box and is stable and reliable after long-term production use - Java document resources - CSDN download using JDK1.8, SpringBoot2.2.10.RELEASE, lombok1.18.8, gua For more download resources and learning materials, please visit the CSDN download channel. https://download.csdn.net/download/u011837804/85074791

Guess you like

Origin blog.csdn.net/u011837804/article/details/123951063