1、概述
在我们实际生产项目中,不管是上传和下载都需要先连接上Ftp,然后再执行相应的操作。假如可以在执行上传和下载的时候直接拿到Ftp客户端,是不是就节省了很多性能呢?本篇文章就像数据库连接池一样,通过Springboot+commons-pool2实现了ftp连接池,并且可以配置多个ftp的连接池,下面我们就来看下。
Linux命令在线工具:https://tools.qzxdp.cn/linux_command
个人博客网站:https://www.qzxdp.cn
2、项目整体结构
3、项目代码
- 项目jar包依赖
<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>
-
项目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<>();
}
- 配置多Ftp yml配置
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、通过端点,查看当前各个连接池的使用情况
通过守护线程实现连接池内连接可用性校验,配置最大、最小连接个数防止Ftp连接数打满,经过长期生产项目实战,稳定可靠,现在分享给有需要的朋友,希望能帮到各位朋友,在使用期间有疑问,欢迎留言分享。