<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.27.3</version>
</dependency>
该包从 https://github.com/tobato/FastDFS_Client 1.27.3-SNAPSHOT版本
修改关键信息日志级别并修改 FdfsClientConstants 配置前缀
fdfs -> fastdfs
pom.xml
<dependencies>
<dependency>
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.27.3</version>
</dependency>
</dependencies>
FastDfsService.java
public interface FastDfsService extends FastFileStorageClient {
String uploadFile(String group, File file, String fileExtName);
}
FastDfsServiceImpl.java
@Slf4j
@Service
public class FastDfsServiceImpl extends DefaultFastFileStorageClient implements FastDfsService {
@Value("${fastdfs.default-group:group0}")
private String defaultGroup;
@Override
public String uploadFile(String group, File file, String fileExtName) {
if (StrUtil.isEmpty(fileExtName)) {
fileExtName = getExtension(FileUtil.getName(file));
}
if (StrUtil.isEmpty(group)) {
group = defaultGroup;
}
BufferedInputStream inputStream = FileUtil.getInputStream(file);
try (inputStream) {
StorePath storePath = uploadFile(group, inputStream, FileUtil.size(file), fileExtName);
return storePath.getFullPath();
} catch (IOException e) {
throw new RuntimeException("上传失败[]" + e.getMessage());
}
}
@Override
public void deleteFile(String filePath) {
try {
super.deleteFile(filePath);
} catch (FdfsServerException e) {
if (e.getErrorCode() == ErrorCodeConstants.ERR_NO_ENOENT) {
log.info("文件已删除[{}]", filePath);
} else {
throw e;
}
}
}
private String getExtension(String fileName) {
if (fileName != null && fileName.length() > 0 && fileName.indexOf(".") > -1) {
int i = fileName.indexOf(".");
String end = fileName.substring(i + 1);
if (end.length() > 6) {
return end.substring(0, 6);
} else {
return end;
}
}
return "";
}
}
增强 - 重试
@Slf4j
@Service
public class FastDfsRetryServiceImpl implements FastDfsRetryService {
@Recover
public Object recover(RuntimeException e) {
log.error("重试{}次,依然失败[{}]", 5, e.getMessage());
return null;
}
@Override
@Retryable(value = RuntimeException.class, maxAttempts = 5, backoff = @Backoff(delay = 3000L, multiplier = 1.5))
public Object around(ProceedingJoinPoint joinPoint) throws RuntimeException {
try {
return joinPoint.proceed();
} catch (Throwable e) {
throw new RuntimeException(e.getMessage());
}
}
}
FastDfsAop.java
@Slf4j
@Aspect
@Component
public class FastDfsAop {
@Resource
private FastDfsRetryService fastDfsRetryService;
@Pointcut("execution(* com.guotie.dpc.fastdfs.service.FastDfsService.*(..))")
public void pointcut() {
}
@Around("pointcut()")
public Object around(ProceedingJoinPoint joinPoint) {
try {
return fastDfsRetryService.around(joinPoint);
} catch (Exception e) {
log.error(e.getMessage());
return null;
}
}
}
yml
fastdfs:
so-timeout: 1501
connect-timeout: 601
default-group: group0
tracker-list: #TrackerList参数,支持多个
- 10.2.2.168:22122
- 10.2.2.169:22122
pool:
#从池中借出的对象的最大数目(配置为-1表示不限制)
max-total: -1
#获取连接时的最大等待毫秒数(默认配置为5秒)
max-wait-millis: 5000
#每个key最大连接数
max-total-per-key: 50
#每个key对应的连接池最大空闲连接数
max-idle-per-key: 10
#每个key对应的连接池最小空闲连接数
min-idle-per-key: 5