# fastdfs-spring-boot-starter **Repository Path**: springsummerr/fastdfs-spring-boot-starter ## Basic Information - **Project Name**: fastdfs-spring-boot-starter - **Description**: 一个简单的基于org.csource.fastdfs.StorageClient1二次封装并提供连接池功能的fastdfs客户端starter. - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 4 - **Created**: 2023-03-12 - **Last Updated**: 2023-03-12 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # fastdfs-spring-boot-starter > 一个简单的基于org.csource.fastdfs.StorageClient1二次封装并提供连接池功能的fastdfs客户端starter. ## 介绍 ### 说明 工程提供三大接口(扩展点): ​ 操作接口->FastDFSClient, ​ StorageClient1对象池接口->StorageClientPool, ​ FastDFS操作模板接口->FastDFSTemplate 基于Java SPI机制,扩展方便,工程默认内部实现是基于commons-pool2将org.csource.fastdfs.StorageClient1池化。 ### 项目结构 ``` fastdfs-spring-boot-starter └── src ├── main │ ├── java │ │ └── com.javacoo │ │ ├────── fastdfs │ │ │ ├──────client │ │ │ │ ├── api │ │ │ │ │ ├── client │ │ │ │ │ │ ├── FastDFSClient 操作接口 │ │ │ │ │ │ └── FastDFSClientFactory FastDFSClient工厂 │ │ │ │ │ ├── pool │ │ │ │ │ │ ├── StorageClient1Wrapper 包装对象 │ │ │ │ │ │ ├── StorageClient1WrapperPool 包装对象池接口 │ │ │ │ │ │ └── StorageClient1WrapperPoolFactory 接口工厂 │ │ │ │ │ └── template │ │ │ │ │ ├── Executor 执行器函数式接口 │ │ │ │ │ ├── FastDFSTemplate FastDFS操作模板接口 │ │ │ │ │ └── FastDFSTemplateFactory 接口工厂 │ │ │ │ ├── config 参数配置 │ │ │ │ │ └── FastDFSConfig FastDFS配置类 │ │ │ │ ├── exception 异常 │ │ │ │ │ └── JccfcFastDFSException FastDFS异常类 │ │ │ │ └── internal 接口内部实现 │ │ │ │ ├── client │ │ │ │ │ ├── DefaultFastDFSClient 操作接口默认实现 │ │ │ │ │ └── DefaultFastDFSClientFactory 工厂默认实现 │ │ │ │ ├── pool │ │ │ │ │ ├── DefaultStorageClient1WrapperPool 默认实现 │ │ │ │ │ ├── DefaultStorageClient1WrapperPoolFactory │ │ │ │ │ ├── PoolConfig 连接池配置 │ │ │ │ │ └── StorageClient1WrapperFactory 包装对象工厂 │ │ │ │ └── template │ │ │ │ ├── DefaultFastDFSTemplate 默认实现 │ │ │ │ └── DefaultFastDFSTemplateFactory 工厂默认实现 │ │ │ └──────starter │ │ │ └── FastDFSAutoConfiguration 自动配置类 │ │ ├────── spi │ │ │ ├── Factory 工厂函数式接口 │ │ │ ├── Spi SPI注解 │ │ │ └── SpiLoader SPI加载器 │ └── resource │ ├── META-INF │ └── services │ ├── com.javacoo.fastdfs.client.api.client.FastDFSClientFactory │ ├── com.javacoo...api.pool.StorageClient1WrapperPoolFactory │ └── com.javacoo.fastdfs.client.api.template.FastDFSTemplateFactory │ └── test 测试 ``` ### 相关包及版本: * spring boot 2.1.8.RELEASE * net.oschina.zcx7878 fastdfs-client-java 1.27.0.0 * commons-pool2 2.7.0 * lombok 1.18.6 # 如何使用 1. pom依赖: ``` com.javacoo fastdfs-spring-boot-starter 1.0.0 ``` 2. 配置参数,如: ``` #tracker 地址 多个以逗号分隔 fastdfs.tracker-servers = http://127.0.0.1:22122 #连接超时时间,单位:毫秒 fastdfs.connect-timeout = 10000 #网络超时时间,单位:毫秒 fastdfs.network-timeout = 30000 #tracker 端口,HTTP访问服务的端口号 fastdfs.tracker-http-port = 80 fastdfs.secret-key = FastDFS1234567890 #是否需要防盗链token,默认true fastdfs.anti-steal-token = false #链接池中最大空闲的连接数,默认也为8 fastdfs.pool-config.max-idle = 10 #链接池中最大连接数,默认为8 fastdfs.pool-config.max-total = 10 ``` 3. 注入FastDFSClient,如: ``` @Autowired private FastDFSClient fastDFSClient; ``` # SPI扩展 1、实现功能接口,及工厂接口,如默认实现: ``` /** * FastDFSClient默认实现类 *

说明:

*
  • 基于org.csource.fastdfs.StorageClient1 1.27.0.0
  • * * @author duanyong * @date 2020/5/1 23:14 */ public class DefaultFastDFSClient implements FastDFSClient { ... } /** * FastDFSClient默认实现类工厂 *

    说明:

    *
  • * @author duanyong * @date 2020/5/3 22:32 */ @Spi(order = 1) public final class DefaultFastDFSClientFactory implements FastDFSClientFactory { @Override public FastDFSClient get() { return new DefaultFastDFSClient(); } } ``` 2、注意工程接口实现时注解order排序应大于1,如@Spi(order = 2)。 ``` 因为工程对SPI加载机制进行了封装,提供了缓存功能,返回对象时,如果接口有多个实现,则根据Spi注解中order降序排序,取第一个。 ``` # 附件 1、FastDFSConfig配置参数: ``` /** FastDFS是否可用,默认值*/ public static final String PREFIX = "fastdfs"; /** FastDFS是否可用,默认值*/ public static final String ENABLED = "enabled"; /** 连接超时时间,默认值,单位:毫秒*/ public static final int DEFAULT_CONNECTION_TIMEOUT = 5 * 1000; /** 网络超时时间,默认值,单位:毫秒*/ public static final int DEFAULT_NETWORK_TIMEOUT = 30 * 1000; /** 编码,默认值*/ public static final String DEFAULT_CHARSET = "UTF-8"; /** tracker 端口HTTP访问服务的端口号,默认值*/ public static final int DEFAULT_TRACKER_HTTP_PORT = 80; /** tracker 地址 多个以逗号分隔,默认值*/ public static final String DEFAULT_TRACKER_SERVERS = ""; /** nginx 地址 多个以逗号分隔,默认值*/ public static final String DEFAULT_NGINX_SERVERS = ""; /** FastDFS key,默认值*/ public static final String DEFAULT_SECRET_KEY = ""; /** 是否需要防盗链token,默认值*/ public static final boolean DEFAULT_ANTI_STEAL_TOKEN = true; /**FastDFS是否可用*/ private String enabled = ENABLED; /** 连接超时时间 */ private int connectTimeout = DEFAULT_CONNECTION_TIMEOUT; /** 网络超时时间 */ private int networkTimeout = DEFAULT_NETWORK_TIMEOUT; /** 编码 */ private String charset = DEFAULT_CHARSET; /** tracker 端口,HTTP访问服务的端口号 */ private int trackerHttpPort = DEFAULT_TRACKER_HTTP_PORT; /** tracker 地址 多个以逗号分隔*/ private String trackerServers = DEFAULT_TRACKER_SERVERS; /** nginx 地址 多个以逗号分隔*/ private String nginxAddress = DEFAULT_NGINX_SERVERS; /** FastDFS key*/ private String secretKey = DEFAULT_SECRET_KEY; /** 是否需要防盗链token */ private boolean antiStealToken = DEFAULT_ANTI_STEAL_TOKEN; /**FastDFS连接池配置*/ private PoolConfig poolConfig = new PoolConfig(); ``` 2、PoolConfig配置参数: ``` /** 链接池中最大空闲的连接数,默认也为8.*/ private int maxIdle = 10; /** 连接池中最少空闲的连接数,默认为0.*/ private int minIdle = 0; /** 链接池中最大连接数,默认为8.*/ private int maxTotal = 10; /** 当连接池资源耗尽时,等待时间,超出则抛异常,默认为-1即永不超时.*/ private long maxWaitMillis = 5 * 1000; /** 默认false,create的时候检测是有有效,如果无效则从连接池中移除,并尝试获取继续获取.*/ private boolean testOnCreate = false; /** 默认false,borrow的时候检测是有有效,如果无效则从连接池中移除,并尝试获取继续获取.*/ private boolean testOnBorrow = true; ```