From 3ad69e5df52577bdafd29c04e6ee64866647e5f6 Mon Sep 17 00:00:00 2001 From: lsy <18332763730@163.com> Date: Wed, 4 Oct 2017 20:49:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=80=E5=8F=91boot-starter-fastdfs=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E5=AE=8C=E6=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- boot-starter-fastdfs-client/README.md | 14 +++ .../fastdfs/config/FastDFSClientConfig.java | 95 +++++++++++++++++++ .../param/FastDFSClientParamConfig.java | 92 ++++++++++++++++++ .../main/resources/META-INF/spring.factories | 2 + .../src/main/resources/application.yml | 9 ++ 5 files changed, 212 insertions(+) create mode 100644 boot-starter-fastdfs-client/README.md create mode 100644 boot-starter-fastdfs-client/src/main/java/com/micro/fast/fastdfs/config/FastDFSClientConfig.java create mode 100644 boot-starter-fastdfs-client/src/main/java/com/micro/fast/fastdfs/config/param/FastDFSClientParamConfig.java create mode 100644 boot-starter-fastdfs-client/src/main/resources/META-INF/spring.factories diff --git a/boot-starter-fastdfs-client/README.md b/boot-starter-fastdfs-client/README.md new file mode 100644 index 0000000..fda4b44 --- /dev/null +++ b/boot-starter-fastdfs-client/README.md @@ -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 (必要参数) + diff --git a/boot-starter-fastdfs-client/src/main/java/com/micro/fast/fastdfs/config/FastDFSClientConfig.java b/boot-starter-fastdfs-client/src/main/java/com/micro/fast/fastdfs/config/FastDFSClientConfig.java new file mode 100644 index 0000000..ee59703 --- /dev/null +++ b/boot-starter-fastdfs-client/src/main/java/com/micro/fast/fastdfs/config/FastDFSClientConfig.java @@ -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> entries = properties.entrySet(); + Iterator> iterator = entries.iterator(); + while (iterator.hasNext()) { + Map.Entry 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; + } +} diff --git a/boot-starter-fastdfs-client/src/main/java/com/micro/fast/fastdfs/config/param/FastDFSClientParamConfig.java b/boot-starter-fastdfs-client/src/main/java/com/micro/fast/fastdfs/config/param/FastDFSClientParamConfig.java new file mode 100644 index 0000000..6dc313a --- /dev/null +++ b/boot-starter-fastdfs-client/src/main/java/com/micro/fast/fastdfs/config/param/FastDFSClientParamConfig.java @@ -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; + } +} diff --git a/boot-starter-fastdfs-client/src/main/resources/META-INF/spring.factories b/boot-starter-fastdfs-client/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000..cb080d4 --- /dev/null +++ b/boot-starter-fastdfs-client/src/main/resources/META-INF/spring.factories @@ -0,0 +1,2 @@ +# 自动化装配 +org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.micro.fast.fastdfs.BootStartFastdfsClientApplication \ No newline at end of file diff --git a/boot-starter-fastdfs-client/src/main/resources/application.yml b/boot-starter-fastdfs-client/src/main/resources/application.yml index e69de29..bfe6156 100644 --- a/boot-starter-fastdfs-client/src/main/resources/application.yml +++ b/boot-starter-fastdfs-client/src/main/resources/application.yml @@ -0,0 +1,9 @@ +# 示例配置 +fastdfs: + connectTimeoutInSeconds : 5 + networkTimeoutInSeconds : 30 + charset : UTF-8 + httpAntiStealToken : false + httpSecretKey : FastDFS1234567890 + httpTrackerHttpPort : 80 + trackerServers : 192.168.0.107:22122 \ No newline at end of file