修改 FastDFS 的集成方式。
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
package xyz.zhouxy.plusone.oss;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import org.csource.common.MyException;
|
||||
import org.csource.fastdfs.ClientGlobal;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import xyz.zhouxy.plusone.oss.FastDFSProperties.ConnectionPool;
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(FastDFSProperties.class)
|
||||
@ConditionalOnClass(FastDFSUtil.class)
|
||||
@EnableAutoConfiguration
|
||||
public class FastDFSAutoConfig {
|
||||
|
||||
@Bean
|
||||
@SuppressWarnings("all")
|
||||
FastDFSUtil fastDFSUtil(FastDFSProperties props) throws IOException, FastDFSException {
|
||||
|
||||
List<String> trackerServerStrList = props.getTrackerServers();
|
||||
if (CollectionUtils.isEmpty(trackerServerStrList)) {
|
||||
throw new FastDFSException(
|
||||
String.format("configure item %s is required - ", ClientGlobal.PROP_KEY_TRACKER_SERVERS));
|
||||
}
|
||||
try {
|
||||
InetSocketAddress[] trackerServers = trackerServerStrList.stream()
|
||||
.map(trackerServer -> {
|
||||
String[] hostPort = trackerServer.trim().split(":");
|
||||
String host = hostPort[0].trim();
|
||||
int port = Integer.parseInt(hostPort[1].trim());
|
||||
return new InetSocketAddress(host, port);
|
||||
})
|
||||
.toArray(InetSocketAddress[]::new);
|
||||
ClientGlobal.initByTrackers(trackerServers);
|
||||
|
||||
var connectTimeoutInSecondsConf = props.getConnectTimeoutInSeconds();
|
||||
if (connectTimeoutInSecondsConf != null) {
|
||||
ClientGlobal.setG_connect_timeout(connectTimeoutInSecondsConf * 1000);
|
||||
}
|
||||
var networkTimeoutInSecondsConf = props.getNetworkTimeoutInSeconds();
|
||||
if (networkTimeoutInSecondsConf != null) {
|
||||
ClientGlobal.setG_network_timeout(networkTimeoutInSecondsConf * 1000);
|
||||
}
|
||||
var charsetConf = props.getCharset();
|
||||
if (StringUtils.hasText(charsetConf)) {
|
||||
ClientGlobal.setG_charset(charsetConf);
|
||||
}
|
||||
var httpAntiStealTokenConf = props.getHttpAntiStealToken();
|
||||
if (httpAntiStealTokenConf != null) {
|
||||
ClientGlobal.setG_anti_steal_token(httpAntiStealTokenConf);
|
||||
}
|
||||
var httpSecretKeyConf = props.getHttpSecretKey();
|
||||
if (StringUtils.hasText(httpSecretKeyConf)) {
|
||||
ClientGlobal.setG_secret_key(httpSecretKeyConf);
|
||||
}
|
||||
var httpTrackerHttpPortConf = props.getHttpTrackerHttpPort();
|
||||
if (httpTrackerHttpPortConf != null) {
|
||||
ClientGlobal.setG_tracker_http_port(httpTrackerHttpPortConf);
|
||||
}
|
||||
|
||||
ConnectionPool connectionPool = props.getConnectionPool();
|
||||
var poolEnabled = Objects.nonNull(connectionPool)
|
||||
&& Boolean.TRUE.equals(connectionPool.getEnabled());
|
||||
|
||||
if (poolEnabled) {
|
||||
var poolMaxCountPerEntry = connectionPool.getMaxCountPerEntry();
|
||||
if (poolMaxCountPerEntry != null) {
|
||||
ClientGlobal.g_connection_pool_max_count_per_entry = poolMaxCountPerEntry;
|
||||
}
|
||||
var poolMaxIdleTime = connectionPool.getMaxIdleTime();
|
||||
if (poolMaxIdleTime != null) {
|
||||
ClientGlobal.g_connection_pool_max_idle_time = poolMaxIdleTime * 1000;
|
||||
}
|
||||
var poolMaxWaitTimeInMS = connectionPool.getMaxWaitTimeInMs();
|
||||
if (poolMaxWaitTimeInMS != null) {
|
||||
ClientGlobal.g_connection_pool_max_wait_time_in_ms = poolMaxWaitTimeInMS;
|
||||
}
|
||||
}
|
||||
return new FastDFSUtil();
|
||||
} catch (MyException e) {
|
||||
throw new FastDFSException(e);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,5 +1,10 @@
|
||||
package xyz.zhouxy.plusone.oss;
|
||||
|
||||
/**
|
||||
* 封装 FastDFS 的 {@link org.csource.common.MyException}
|
||||
*
|
||||
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
||||
*/
|
||||
public class FastDFSException extends Exception {
|
||||
|
||||
public FastDFSException() {
|
||||
|
@@ -0,0 +1,32 @@
|
||||
package xyz.zhouxy.plusone.oss;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@ConfigurationProperties("fastdfs")
|
||||
public class FastDFSProperties {
|
||||
private Integer connectTimeoutInSeconds;
|
||||
private Integer networkTimeoutInSeconds;
|
||||
private String charset;
|
||||
private Boolean httpAntiStealToken;
|
||||
private String httpSecretKey;
|
||||
private Integer httpTrackerHttpPort;
|
||||
private List<String> trackerServers;
|
||||
|
||||
private ConnectionPool connectionPool;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public static class ConnectionPool {
|
||||
private Boolean enabled;
|
||||
private Integer maxCountPerEntry;
|
||||
private Integer maxIdleTime;
|
||||
private Integer maxWaitTimeInMs;
|
||||
}
|
||||
}
|
@@ -6,7 +6,6 @@ import java.io.InputStream;
|
||||
|
||||
import org.csource.common.MyException;
|
||||
import org.csource.common.NameValuePair;
|
||||
import org.csource.fastdfs.ClientGlobal;
|
||||
import org.csource.fastdfs.FileInfo;
|
||||
import org.csource.fastdfs.StorageClient;
|
||||
import org.csource.fastdfs.StorageServer;
|
||||
@@ -14,38 +13,20 @@ import org.csource.fastdfs.TrackerClient;
|
||||
import org.csource.fastdfs.TrackerServer;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
public class FastDFSUtil {
|
||||
|
||||
private static TrackerClient trackerClient;
|
||||
private static TrackerServer trackerServer;
|
||||
private static StorageServer storageServer;
|
||||
private final TrackerServer trackerServer;
|
||||
private final StorageServer storageServer;
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(FastDFSUtil.class);
|
||||
|
||||
private FastDFSUtil() {
|
||||
throw new IllegalStateException("Utility class");
|
||||
}
|
||||
|
||||
static {
|
||||
try {
|
||||
String filePath = new ClassPathResource("fdfs_client.conf").getFile().getAbsolutePath();
|
||||
ClientGlobal.init(filePath);
|
||||
/*
|
||||
* TODO【重构】 将配置信息集成在 SpringBoot 配置文件中,使用加载 Properties 对象的方式进行配置,去除 fdfs_client.conf
|
||||
* Properties props = new Properties();
|
||||
* props.put(ClientGlobal.PROP_KEY_TRACKER_SERVERS, "10.0.11.101:22122,10.0.11.102:22122");
|
||||
* ClientGlobal.initByProperties(props);
|
||||
*/
|
||||
trackerClient = new TrackerClient();
|
||||
trackerServer = trackerClient.getTrackerServer();
|
||||
storageServer = trackerClient.getStoreStorage(trackerServer);
|
||||
} catch (Exception e) {
|
||||
logger.error("FastDFS Client Init Fail!", e);
|
||||
}
|
||||
FastDFSUtil() throws IOException, MyException {
|
||||
TrackerClient trackerClient = new TrackerClient();
|
||||
this.trackerServer = trackerClient.getTrackerServer();
|
||||
this.storageServer = trackerClient.getStoreStorage(trackerServer);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -62,7 +43,7 @@ public class FastDFSUtil {
|
||||
* return null if fail
|
||||
* @throws FastDFSException
|
||||
*/
|
||||
public static String[] upload(FastDFSFile file) throws FastDFSException {
|
||||
public String[] upload(FastDFSFile file) throws FastDFSException {
|
||||
logger.info("File Name: {}, File Length: {}", file.getName(), file.getContent().length);
|
||||
|
||||
NameValuePair[] metaList = new NameValuePair[1];
|
||||
@@ -72,7 +53,7 @@ public class FastDFSUtil {
|
||||
StorageClient storageClient = null;
|
||||
String[] uploadResults = null;
|
||||
try {
|
||||
storageClient = new StorageClient(trackerServer, storageServer);
|
||||
storageClient = new StorageClient(this.trackerServer, this.storageServer);
|
||||
uploadResults = storageClient.upload_file(file.getContent(), file.getExt(), metaList);
|
||||
|
||||
if (uploadResults == null) {
|
||||
@@ -90,9 +71,9 @@ public class FastDFSUtil {
|
||||
return uploadResults;
|
||||
}
|
||||
|
||||
public static FileInfo getFile(String groupName, String remoteFileName) throws FastDFSException {
|
||||
public FileInfo getFile(String groupName, String remoteFileName) throws FastDFSException {
|
||||
try {
|
||||
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
|
||||
StorageClient storageClient = new StorageClient(this.trackerServer, this.storageServer);
|
||||
return storageClient.get_file_info(groupName, remoteFileName);
|
||||
} catch (IOException e) {
|
||||
throw new FastDFSException("IO Exception: Get File from Fast DFS failed", e);
|
||||
@@ -101,9 +82,9 @@ public class FastDFSUtil {
|
||||
}
|
||||
}
|
||||
|
||||
public static InputStream downFile(String groupName, String remoteFileName) throws FastDFSException {
|
||||
public InputStream downFile(String groupName, String remoteFileName) throws FastDFSException {
|
||||
try {
|
||||
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
|
||||
StorageClient storageClient = new StorageClient(this.trackerServer, this.storageServer);
|
||||
byte[] fileByte = storageClient.download_file(groupName, remoteFileName);
|
||||
InputStream ins = new ByteArrayInputStream(fileByte);
|
||||
return ins;
|
||||
@@ -114,8 +95,8 @@ public class FastDFSUtil {
|
||||
}
|
||||
}
|
||||
|
||||
public static void deleteFile(String groupName, String remoteFileName) throws FastDFSException {
|
||||
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
|
||||
public void deleteFile(String groupName, String remoteFileName) throws FastDFSException {
|
||||
StorageClient storageClient = new StorageClient(this.trackerServer, this.storageServer);
|
||||
try {
|
||||
int i = storageClient.delete_file(groupName, remoteFileName);
|
||||
if (i == 0) {
|
||||
|
Reference in New Issue
Block a user