负载均衡基本改造,增加redis、miniio等基本中间件
This commit is contained in:
@@ -33,6 +33,10 @@
|
||||
<groupId>cn.dev33</groupId>
|
||||
<artifactId>sa-token-spring-boot3-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
@@ -53,6 +57,10 @@
|
||||
<groupId>org.apache.httpcomponents.client5</groupId>
|
||||
<artifactId>httpclient5</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.minio</groupId>
|
||||
<artifactId>minio</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -3,40 +3,82 @@ package tech.easyflow.common.filestorage.impl;
|
||||
import org.dromara.x.file.storage.core.FileInfo;
|
||||
import org.dromara.x.file.storage.core.recorder.FileRecorder;
|
||||
import org.dromara.x.file.storage.core.upload.FilePartInfo;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 文件记录器
|
||||
* 文件记录器(基于 Redis 存储上传记录,满足多实例共享)
|
||||
*/
|
||||
@Service
|
||||
public class FileRecoderImpl implements FileRecorder {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(FileRecoderImpl.class);
|
||||
private static final String FILE_RECORD_KEY_PREFIX = "easyflow:xfile:record:";
|
||||
|
||||
@Autowired(required = false)
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
|
||||
private String buildKey(String url) {
|
||||
return FILE_RECORD_KEY_PREFIX + url;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean save(FileInfo fileInfo) {
|
||||
if (fileInfo == null || !StringUtils.hasText(fileInfo.getUrl())) {
|
||||
return false;
|
||||
}
|
||||
if (fileInfo.getCreateTime() == null) {
|
||||
fileInfo.setCreateTime(new Date());
|
||||
}
|
||||
if (redisTemplate != null) {
|
||||
redisTemplate.opsForValue().set(buildKey(fileInfo.getUrl()), fileInfo);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(FileInfo fileInfo) {
|
||||
|
||||
save(fileInfo);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileInfo getByUrl(String url) {
|
||||
if (!StringUtils.hasText(url) || redisTemplate == null) {
|
||||
return null;
|
||||
}
|
||||
Object value = redisTemplate.opsForValue().get(buildKey(url));
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
if (value instanceof FileInfo) {
|
||||
return (FileInfo) value;
|
||||
}
|
||||
LOG.warn("FileInfo redis记录类型异常,url={}, class={}", url, value.getClass().getName());
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean delete(String url) {
|
||||
return true;
|
||||
if (!StringUtils.hasText(url) || redisTemplate == null) {
|
||||
return false;
|
||||
}
|
||||
Boolean deleted = redisTemplate.delete(buildKey(url));
|
||||
return Boolean.TRUE.equals(deleted);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveFilePart(FilePartInfo filePartInfo) {
|
||||
|
||||
// 当前业务未使用手动分片上传,不做持久化
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteFilePartByUploadId(String uploadId) {
|
||||
|
||||
// 当前业务未使用手动分片上传,不做持久化
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ public class LocalFileStorageServiceImpl implements FileStorageService {
|
||||
|
||||
@Value("${easyflow.storage.local.root:}")
|
||||
private String root;
|
||||
@Value("${easyflow.storage.local.prefix}")
|
||||
@Value("${easyflow.storage.local.prefix:}")
|
||||
private String prefix;
|
||||
|
||||
@EventListener(ApplicationReadyEvent.class)
|
||||
|
||||
@@ -5,6 +5,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import tech.easyflow.common.filestorage.FileStorageService;
|
||||
import tech.easyflow.common.filestorage.utils.PathGeneratorUtil;
|
||||
@@ -22,17 +23,33 @@ public class XFIleStorageServiceImpl implements FileStorageService {
|
||||
|
||||
@Override
|
||||
public String save(MultipartFile file) {
|
||||
return save(file, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String save(MultipartFile file, String prePath) {
|
||||
String uploadPath = PathGeneratorUtil.generateUserPath("");
|
||||
if (StringUtils.hasText(prePath)) {
|
||||
String normalized = prePath.replaceAll("^/+", "").replaceAll("/+$", "");
|
||||
uploadPath = "/" + normalized + uploadPath;
|
||||
}
|
||||
FileInfo fileInfo = fileStorageService.of(file)
|
||||
.setPath(PathGeneratorUtil.generateUserPath(""))
|
||||
.setPath(uploadPath)
|
||||
.setSaveFilename(file.getOriginalFilename())
|
||||
.setContentType(getFileContentType(file))
|
||||
.upload();
|
||||
return fileInfo == null ? "上传失败!" : fileInfo.getUrl();
|
||||
if (fileInfo == null || !StringUtils.hasText(fileInfo.getUrl())) {
|
||||
throw new RuntimeException("文件上传失败");
|
||||
}
|
||||
return fileInfo.getUrl();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String path) {
|
||||
fileStorageService.delete(path);
|
||||
boolean deleted = fileStorageService.delete(path);
|
||||
if (!deleted) {
|
||||
LOG.warn("删除文件失败或文件不存在,path={}", path);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user