diff --git a/README.md b/README.md index e66824f..ba978b1 100644 --- a/README.md +++ b/README.md @@ -30,9 +30,9 @@ mvn install:install-file -DgroupId=org.csource -DartifactId=fastdfs-client-java ``` -## 配置文件、所在目录、加载优先顺序 +## .conf 配置文件、所在目录、加载优先顺序 - 配置文件名fdfs_client.conf(或使用其它文件名xxx.conf) + 配置文件名fdfs_client.conf(或使用其它文件名xxx_yyy.conf) 文件所在位置可以是项目classpath(或OS文件系统目录比如/opt/): /opt/fdfs_client.conf @@ -46,7 +46,7 @@ mvn install:install-file -DgroupId=org.csource -DartifactId=fastdfs-client-java connect_timeout = 2 network_timeout = 30 charset = UTF-8 -http.tracker_http_port = 8080 +http.tracker_http_port = 80 http.anti_steal_token = no http.secret_key = FastDFS1234567890 @@ -55,5 +55,73 @@ tracker_server = 10.0.11.248:22122 tracker_server = 10.0.11.249:22122 ``` - 注:tracker_server指向您自己IP地址和端口,1-n个 + 注1:tracker_server指向您自己IP地址和端口,1-n个 + 注2:除了tracker_server,其它配置项都是可选的 + +## .properties 配置文件、所在目录、加载优先顺序 + + 配置文件名 fastdfs-client.properties(或使用其它文件名 xxx-yyy.properties) + + 文件所在位置可以是项目classpath(或OS文件系统目录比如/opt/): + /opt/fastdfs-client.properties + C:\Users\James\config\fastdfs-client.properties + + 优先按OS文件系统路径读取,没有找到才查找项目classpath,尤其针对linux环境下的相对路径比如: + fastdfs-client.properties + config/fastdfs-client.properties + +``` +fastdfs.connect_timeout_in_seconds = 5 +fastdfs.network_timeout_in_seconds = 30 +fastdfs.charset = UTF-8 +fastdfs.http_anti_steal_token = false +fastdfs.http_secret_key = FastDFS1234567890 +fastdfs.http_tracker_http_port = 80 + +fastdfs.tracker_servers = 10.0.11.201:22122,10.0.11.202:22122,10.0.11.203:22122 +``` + + 注1:properties 配置文件中属性名跟 conf 配置文件不尽相同,并且统一加前缀"fastdfs.",便于整合到用户项目配置文件 + 注2:fastdfs.tracker_servers 配置项不能重复属性名,多个 tracker_server 用逗号","隔开 + 注3:除了fastdfs.tracker_servers,其它配置项都是可选的 + + +## 加载配置示例 + + 加载原 conf 格式文件配置: + ClientGlobal.init("fdfs_client.conf"); + ClientGlobal.init("config/fdfs_client.conf"); + ClientGlobal.init("/opt/fdfs_client.conf"); + ClientGlobal.init("C:\\Users\\James\\config\\fdfs_client.conf"); + + 加载 properties 格式文件配置: + ClientGlobal.initByProperties("fastdfs-client.properties"); + ClientGlobal.initByProperties("config/fastdfs-client.properties"); + ClientGlobal.initByProperties("/opt/fastdfs-client.properties"); + ClientGlobal.initByProperties("C:\\Users\\James\\config\\fastdfs-client.properties"); + + 加载 Properties 对象配置: + Properties props = new Properties(); + props.put(ClientGlobal.PROP_KEY_TRACKER_SERVERS, "10.0.11.101:22122,10.0.11.102:22122"); + ClientGlobal.initByProperties(props); + + 加载 trackerServers 字符串配置: + String trackerServers = "10.0.11.101:22122,10.0.11.102:22122"; + ClientGlobal.initByTrackers(trackerServers); + + +## 检查加载配置结果: + + System.out.println("ClientGlobal.configInfo(): " + ClientGlobal.configInfo()); +``` +ClientGlobal.configInfo(): { + g_connect_timeout(ms) = 5000 + g_network_timeout(ms) = 30000 + g_charset = UTF-8 + g_anti_steal_token = false + g_secret_key = FastDFS1234567890 + g_tracker_http_port = 80 + trackerServers = 10.0.11.101:22122,10.0.11.102:22122 +} +``` diff --git a/fastdfs-client.properties b/fastdfs-client.properties new file mode 100644 index 0000000..5031a45 --- /dev/null +++ b/fastdfs-client.properties @@ -0,0 +1,13 @@ +## fastdfs-client.properties + +fastdfs.connect_timeout_in_seconds = 5 +fastdfs.network_timeout_in_seconds = 30 + +fastdfs.charset = UTF-8 + +fastdfs.http_anti_steal_token = false +fastdfs.http_secret_key = FastDFS1234567890 +fastdfs.http_tracker_http_port = 80 + +fastdfs.tracker_servers = 10.0.11.201:22122,10.0.11.202:22122,10.0.11.203:22122 + diff --git a/src/main/java/org/csource/common/IniFileReader.java b/src/main/java/org/csource/common/IniFileReader.java index 3ade675..ce66100 100644 --- a/src/main/java/org/csource/common/IniFileReader.java +++ b/src/main/java/org/csource/common/IniFileReader.java @@ -30,7 +30,7 @@ public class IniFileReader { loadFromFile(conf_filename); } - private static ClassLoader classLoader() { + public static ClassLoader classLoader() { ClassLoader loader = Thread.currentThread().getContextClassLoader(); if (loader == null) { loader = ClassLoader.getSystemClassLoader(); @@ -38,6 +38,25 @@ public class IniFileReader { return loader; } + public static InputStream loadFromOsFileSystemOrClasspathAsStream(String filePath) { + InputStream in = null; + try { + // 优先从文件系统路径加载 + if (new File(filePath).exists()) { + in = new FileInputStream(filePath); + //System.out.println("loadFrom...file path done"); + } + // 从类路径加载 + else { + in = classLoader().getResourceAsStream(filePath); + //System.out.println("loadFrom...class path done"); + } + } catch (Exception ex) { + ex.printStackTrace(); + } + return in; + } + /** * get the config filename * @@ -128,18 +147,8 @@ public class IniFileReader { } private void loadFromFile(String confFilePath) throws IOException { - InputStream in = null; + InputStream in = loadFromOsFileSystemOrClasspathAsStream(confFilePath); try { - // 优先从文件系统路径加载 - if (new File(confFilePath).exists()) { - in = new FileInputStream(confFilePath); - //System.out.println("loadFrom...file path done"); - } - // 从类路径加载 - else { - in = classLoader().getResourceAsStream(confFilePath); - //System.out.println("loadFrom...class path done"); - } readToParamTable(in); } catch (Exception ex) { ex.printStackTrace(); diff --git a/src/main/java/org/csource/fastdfs/ClientGlobal.java b/src/main/java/org/csource/fastdfs/ClientGlobal.java index 9e4a8e9..b078667 100644 --- a/src/main/java/org/csource/fastdfs/ClientGlobal.java +++ b/src/main/java/org/csource/fastdfs/ClientGlobal.java @@ -12,8 +12,12 @@ import org.csource.common.IniFileReader; import org.csource.common.MyException; import java.io.IOException; +import java.io.InputStream; import java.net.InetSocketAddress; import java.net.Socket; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; /** * Global variables @@ -22,14 +26,37 @@ import java.net.Socket; * @version Version 1.11 */ public class ClientGlobal { - public static final int DEFAULT_CONNECT_TIMEOUT = 5; //second + + public static final String CONF_KEY_CONNECT_TIMEOUT = "connect_timeout"; + public static final String CONF_KEY_NETWORK_TIMEOUT = "network_timeout"; + public static final String CONF_KEY_CHARSET = "charset"; + public static final String CONF_KEY_HTTP_ANTI_STEAL_TOKEN = "http.anti_steal_token"; + public static final String CONF_KEY_HTTP_SECRET_KEY = "http.secret_key"; + public static final String CONF_KEY_HTTP_TRACKER_HTTP_PORT = "http.tracker_http_port"; + public static final String CONF_KEY_TRACKER_SERVER = "tracker_server"; + + public static final String PROP_KEY_CONNECT_TIMEOUT_IN_SECONDS = "fastdfs.connect_timeout_in_seconds"; + public static final String PROP_KEY_NETWORK_TIMEOUT_IN_SECONDS = "fastdfs.network_timeout_in_seconds"; + public static final String PROP_KEY_CHARSET = "fastdfs.charset"; + public static final String PROP_KEY_HTTP_ANTI_STEAL_TOKEN = "fastdfs.http_anti_steal_token"; + public static final String PROP_KEY_HTTP_SECRET_KEY = "fastdfs.http_secret_key"; + public static final String PROP_KEY_HTTP_TRACKER_HTTP_PORT = "fastdfs.http_tracker_http_port"; + public static final String PROP_KEY_TRACKER_SERVERS = "fastdfs.tracker_servers"; + + public static final int DEFAULT_CONNECT_TIMEOUT = 5; //second public static final int DEFAULT_NETWORK_TIMEOUT = 30; //second - public static int g_connect_timeout; //millisecond - public static int g_network_timeout; //millisecond - public static String g_charset; - public static int g_tracker_http_port; - public static boolean g_anti_steal_token; //if anti-steal token - public static String g_secret_key; //generage token secret key + public static final String DEFAULT_CHARSET = "UTF-8"; + public static final boolean DEFAULT_HTTP_ANTI_STEAL_TOKEN = false; + public static final String DEFAULT_HTTP_SECRET_KEY = "FastDFS1234567890"; + public static final int DEFAULT_HTTP_TRACKER_HTTP_PORT = 80; + + public static int g_connect_timeout = DEFAULT_CONNECT_TIMEOUT * 1000; //millisecond + public static int g_network_timeout = DEFAULT_NETWORK_TIMEOUT * 1000; //millisecond + public static String g_charset = DEFAULT_CHARSET; + public static boolean g_anti_steal_token = DEFAULT_HTTP_ANTI_STEAL_TOKEN; //if anti-steal token + public static String g_secret_key = DEFAULT_HTTP_SECRET_KEY; //generage token secret key + public static int g_tracker_http_port = DEFAULT_HTTP_TRACKER_HTTP_PORT; + public static TrackerGroup g_tracker_group; private ClientGlobal() { @@ -87,6 +114,87 @@ public class ClientGlobal { } } + /** + * load from properties file + * + * @param propsFilePath properties file path, eg: + * "fastdfs-client.properties" + * "config/fastdfs-client.properties" + * "/opt/fastdfs-client.properties" + * "C:\\Users\\James\\config\\fastdfs-client.properties" + * properties文件至少包含一个配置项 fastdfs.tracker_servers 例如: + * fastdfs.tracker_servers = 10.0.11.245:22122,10.0.11.246:22122 + * server的IP和端口用冒号':'分隔 + * server之间用逗号','分隔 + */ + public static void initByProperties(String propsFilePath) throws IOException, MyException { + Properties props = new Properties(); + InputStream in = IniFileReader.loadFromOsFileSystemOrClasspathAsStream(propsFilePath); + if (in != null) { + props.load(in); + } + initByProperties(props); + } + + public static void initByProperties(Properties props) throws IOException, MyException { + String trackerServersConf = props.getProperty(PROP_KEY_TRACKER_SERVERS); + if (trackerServersConf == null || trackerServersConf.trim().length() == 0) { + throw new MyException(String.format("configure item %s is required", PROP_KEY_TRACKER_SERVERS)); + } + initByTrackers(trackerServersConf.trim()); + + String connectTimeoutInSecondsConf = props.getProperty(PROP_KEY_CONNECT_TIMEOUT_IN_SECONDS); + String networkTimeoutInSecondsConf = props.getProperty(PROP_KEY_NETWORK_TIMEOUT_IN_SECONDS); + String charsetConf = props.getProperty(PROP_KEY_CHARSET); + String httpAntiStealTokenConf = props.getProperty(PROP_KEY_HTTP_ANTI_STEAL_TOKEN); + String httpSecretKeyConf = props.getProperty(PROP_KEY_HTTP_SECRET_KEY); + String httpTrackerHttpPortConf = props.getProperty(PROP_KEY_HTTP_TRACKER_HTTP_PORT); + if (connectTimeoutInSecondsConf != null && connectTimeoutInSecondsConf.trim().length() != 0) { + g_connect_timeout = Integer.parseInt(connectTimeoutInSecondsConf.trim()) * 1000; + } + if (networkTimeoutInSecondsConf != null && networkTimeoutInSecondsConf.trim().length() != 0) { + g_network_timeout = Integer.parseInt(networkTimeoutInSecondsConf.trim()) * 1000; + } + if (charsetConf != null && charsetConf.trim().length() != 0) { + g_charset = charsetConf.trim(); + } + if (httpAntiStealTokenConf != null && httpAntiStealTokenConf.trim().length() != 0) { + g_anti_steal_token = Boolean.parseBoolean(httpAntiStealTokenConf); + } + if (httpSecretKeyConf != null && httpSecretKeyConf.trim().length() != 0) { + g_secret_key = httpSecretKeyConf.trim(); + } + if (httpTrackerHttpPortConf != null && httpTrackerHttpPortConf.trim().length() != 0) { + g_tracker_http_port = Integer.parseInt(httpTrackerHttpPortConf); + } + } + + /** + * load from properties file + * + * @param trackerServers 例如:"10.0.11.245:22122,10.0.11.246:22122" + * server的IP和端口用冒号':'分隔 + * server之间用逗号','分隔 + */ + public static void initByTrackers(String trackerServers) throws IOException, MyException { + List list = new ArrayList(); + String spr1 = ","; + String spr2 = ":"; + String[] arr1 = trackerServers.trim().split(spr1); + for (String addrStr : arr1) { + String[] arr2 = addrStr.trim().split(spr2); + String host = arr2[0].trim(); + int port = Integer.parseInt(arr2[1].trim()); + list.add(new InetSocketAddress(host, port)); + } + InetSocketAddress[] trackerAddresses = list.toArray(new InetSocketAddress[list.size()]); + initByTrackers(trackerAddresses); + } + + public static void initByTrackers(InetSocketAddress[] trackerAddresses) throws IOException, MyException { + g_tracker_group = new TrackerGroup(trackerAddresses); + } + /** * construct Socket object * @@ -173,4 +281,25 @@ public class ClientGlobal { public static void setG_tracker_group(TrackerGroup tracker_group) { ClientGlobal.g_tracker_group = tracker_group; } + + public static String configInfo() { + String trackerServers = ""; + if (g_tracker_group != null) { + InetSocketAddress[] trackerAddresses = g_tracker_group.tracker_servers; + for (InetSocketAddress inetSocketAddress : trackerAddresses) { + if(trackerServers.length() > 0) trackerServers += ","; + trackerServers += inetSocketAddress.toString().substring(1); + } + } + return "{" + + "\n g_connect_timeout(ms) = " + g_connect_timeout + + "\n g_network_timeout(ms) = " + g_network_timeout + + "\n g_charset = " + g_charset + + "\n g_anti_steal_token = " + g_anti_steal_token + + "\n g_secret_key = " + g_secret_key + + "\n g_tracker_http_port = " + g_tracker_http_port + + "\n trackerServers = " + trackerServers + + "\n}"; + } + } diff --git a/src/main/resources/fastdfs-client.properties.sample b/src/main/resources/fastdfs-client.properties.sample new file mode 100644 index 0000000..5031a45 --- /dev/null +++ b/src/main/resources/fastdfs-client.properties.sample @@ -0,0 +1,13 @@ +## fastdfs-client.properties + +fastdfs.connect_timeout_in_seconds = 5 +fastdfs.network_timeout_in_seconds = 30 + +fastdfs.charset = UTF-8 + +fastdfs.http_anti_steal_token = false +fastdfs.http_secret_key = FastDFS1234567890 +fastdfs.http_tracker_http_port = 80 + +fastdfs.tracker_servers = 10.0.11.201:22122,10.0.11.202:22122,10.0.11.203:22122 + diff --git a/src/test/java/org/csource/fastdfs/ClientGlobalTests.java b/src/test/java/org/csource/fastdfs/ClientGlobalTests.java new file mode 100644 index 0000000..d8f0320 --- /dev/null +++ b/src/test/java/org/csource/fastdfs/ClientGlobalTests.java @@ -0,0 +1,26 @@ +package org.csource.fastdfs; + +import java.util.Properties; + +/** + * Created by James on 2017/5/19. + */ +public class ClientGlobalTests { + + public static void main(String[] args) throws Exception { + String trackerServers = "10.0.11.101:22122,10.0.11.102:22122"; + ClientGlobal.initByTrackers(trackerServers); + System.out.println("ClientGlobal.configInfo() : " + ClientGlobal.configInfo()); + + String propFilePath = "fastdfs-client.properties"; + ClientGlobal.initByProperties(propFilePath); + System.out.println("ClientGlobal.configInfo() : " + ClientGlobal.configInfo()); + + Properties props = new Properties(); + props.put(ClientGlobal.PROP_KEY_TRACKER_SERVERS, "10.0.11.101:22122,10.0.11.102:22122"); + ClientGlobal.initByProperties(props); + System.out.println("ClientGlobal.configInfo(): " + ClientGlobal.configInfo()); + + } + +} diff --git a/src/test/resources/fastdfs-client.properties b/src/test/resources/fastdfs-client.properties new file mode 100644 index 0000000..5031a45 --- /dev/null +++ b/src/test/resources/fastdfs-client.properties @@ -0,0 +1,13 @@ +## fastdfs-client.properties + +fastdfs.connect_timeout_in_seconds = 5 +fastdfs.network_timeout_in_seconds = 30 + +fastdfs.charset = UTF-8 + +fastdfs.http_anti_steal_token = false +fastdfs.http_secret_key = FastDFS1234567890 +fastdfs.http_tracker_http_port = 80 + +fastdfs.tracker_servers = 10.0.11.201:22122,10.0.11.202:22122,10.0.11.203:22122 +