开发boot-starter-fastdfs模块完毕

This commit is contained in:
lsy
2017-10-04 20:49:20 +08:00
parent b7c11005f9
commit 3ad69e5df5
5 changed files with 212 additions and 0 deletions
+14
View File
@@ -0,0 +1,14 @@
# 欢迎查看boot-starter-fastdfs项目说明
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
## 项目介绍
`boot-starter-fastdfs`项目用于快速开启spring集成fastdfs的项目,将本项目使用maven发布到本地仓库,在其他项
目中引入该依赖进行相关配置即可使用java操作fastdfs进行文件的上传
## application.yml文件中的相关配置
- `fastdfs.connectTimeoutInSeconds` 5 链接超时时间
- `fastdfs.networkTimeoutInSeconds` 30 网络超时时间
- `fastdfs.charset` UTF-8 编码
- `fastdfs.httpAntiStealToken` false 
- `fastdfs.httpSecretKey` FastDFS1234567890
- `fastdfs.httpTrackerHttpPort` 8888 
- `fastdfs.trackerServers` 192.168.0.107:22122,192.163.0.208:22122 (必要参数)
@@ -0,0 +1,95 @@
package com.micro.fast.fastdfs.config;
import com.micro.fast.fastdfs.config.param.FastDFSClientParamConfig;
import org.csource.common.MyException;
import org.csource.fastdfs.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
/**
* fastdfs java client bean configuration class
* @author lishouyu
* @version 1.0
* @since 1.0
*/
@Configuration
public class FastDFSClientConfig {
/**
* 记录日志
*/
private static final Logger logger = LoggerFactory.getLogger(FastDFSClientConfig.class);
@Autowired
private FastDFSClientParamConfig fastDFSClientParamConfig;
/**
* init trackerServer bean
* @return trackerServer trackerServer object
*/
@Bean
public TrackerServer trackerServer() {
TrackerServer trackerServer = null;
//填充properties参数
Properties properties = new Properties();
properties.put(ClientGlobal.PROP_KEY_TRACKER_SERVERS, fastDFSClientParamConfig.getTrackerServers());
properties.put(ClientGlobal.PROP_KEY_HTTP_SECRET_KEY, fastDFSClientParamConfig.getHttpSecretKey());
properties.put(ClientGlobal.PROP_KEY_HTTP_ANTI_STEAL_TOKEN, fastDFSClientParamConfig.getHttpAntiStealToken());
properties.put(ClientGlobal.PROP_KEY_HTTP_TRACKER_HTTP_PORT, fastDFSClientParamConfig.getHttpTrackerHttpPort());
properties.put(ClientGlobal.PROP_KEY_CHARSET, fastDFSClientParamConfig.getCharset());
properties.put(ClientGlobal.PROP_KEY_CONNECT_TIMEOUT_IN_SECONDS, fastDFSClientParamConfig.getConnectTimeoutInSeconds());
properties.put(ClientGlobal.PROP_KEY_NETWORK_TIMEOUT_IN_SECONDS, fastDFSClientParamConfig.getNetworkTimeoutInSeconds());
//对fatsdfs的配置信息进行日志输出
if (!properties.isEmpty()) {
Set<Map.Entry<Object, Object>> entries = properties.entrySet();
Iterator<Map.Entry<Object, Object>> iterator = entries.iterator();
while (iterator.hasNext()) {
Map.Entry<Object, Object> keyVlaue = iterator.next();
String key = (String) keyVlaue.getKey();
String value = (String) keyVlaue.getValue();
if (logger.isInfoEnabled()) {
logger.info("fastdfs ClientGlobal properties {} : {} ", key, value);
}
}
}
try {
//使用properties初始化全局配置
ClientGlobal.initByProperties(properties);
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
if (logger.isErrorEnabled()) {
logger.error("fastdfs java client init error", e);
}
}
//获取tracker客户端对象
TrackerClient trackerClient = new TrackerClient();
try {
//获取tracker服务端对象
trackerServer = trackerClient.getConnection();
} catch (IOException e) {
if (logger.isErrorEnabled()) {
logger.error("fastdfs java client TrackerServer init error", e);
}
}
return trackerServer;
}
/**
* init storageClient bean
* @return storageClient storageClient object
*/
@Bean
public StorageClient storageClient(){
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer(), storageServer);
return storageClient;
}
}
@@ -0,0 +1,92 @@
package com.micro.fast.fastdfs.config.param;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.NotNull;
/**
* fastdfs java client properties auto config class
* @author lsy
* @version 1.0
* @since 1.0
*/
@Configuration
@Validated
@ConfigurationProperties(prefix = "fastdfs")
@AutoConfigureBefore({com.micro.fast.fastdfs.config.FastDFSClientConfig.class})
public class FastDFSClientParamConfig {
private String connectTimeoutInSeconds;
private String networkTimeoutInSeconds;
private String charset;
private String httpAntiStealToken;
private String httpSecretKey;
private String httpTrackerHttpPort;
@NotNull
private String trackerServers;
public String getConnectTimeoutInSeconds() {
return connectTimeoutInSeconds;
}
public void setConnectTimeoutInSeconds(String connectTimeoutInSeconds) {
this.connectTimeoutInSeconds = connectTimeoutInSeconds;
}
public String getNetworkTimeoutInSeconds() {
return networkTimeoutInSeconds;
}
public void setNetworkTimeoutInSeconds(String networkTimeoutInSeconds) {
this.networkTimeoutInSeconds = networkTimeoutInSeconds;
}
public String getCharset() {
return charset;
}
public void setCharset(String charset) {
this.charset = charset;
}
public String getHttpAntiStealToken() {
return httpAntiStealToken;
}
public void setHttpAntiStealToken(String httpAntiStealToken) {
this.httpAntiStealToken = httpAntiStealToken;
}
public String getHttpSecretKey() {
return httpSecretKey;
}
public void setHttpSecretKey(String httpSecretKey) {
this.httpSecretKey = httpSecretKey;
}
public String getHttpTrackerHttpPort() {
return httpTrackerHttpPort;
}
public void setHttpTrackerHttpPort(String httpTrackerHttpPort) {
this.httpTrackerHttpPort = httpTrackerHttpPort;
}
public String getTrackerServers() {
return trackerServers;
}
public void setTrackerServers(String trackerServers) {
this.trackerServers = trackerServers;
}
}
@@ -0,0 +1,2 @@
# 自动化装配
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.micro.fast.fastdfs.BootStartFastdfsClientApplication
@@ -0,0 +1,9 @@
# 示例配置
fastdfs:
connectTimeoutInSeconds : 5
networkTimeoutInSeconds : 30
charset : UTF-8
httpAntiStealToken : false
httpSecretKey : FastDFS1234567890
httpTrackerHttpPort : 80
trackerServers : 192.168.0.107:22122