Skip to content

Commit ad4e330

Browse files
author
veryben
committed
增加properties配置支持并添加README.md使用说明
1 parent 548d2fb commit ad4e330

File tree

7 files changed

+294
-23
lines changed

7 files changed

+294
-23
lines changed

README.md

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ mvn install:install-file -DgroupId=org.csource -DartifactId=fastdfs-client-java
3030
</dependency>
3131
```
3232

33-
## 配置文件、所在目录、加载优先顺序
33+
## .conf 配置文件、所在目录、加载优先顺序
3434

35-
配置文件名fdfs_client.conf(或使用其它文件名xxx.conf)
35+
配置文件名fdfs_client.conf(或使用其它文件名xxx_yyy.conf)
3636

3737
文件所在位置可以是项目classpath(或OS文件系统目录比如/opt/):
3838
/opt/fdfs_client.conf
@@ -46,7 +46,7 @@ mvn install:install-file -DgroupId=org.csource -DartifactId=fastdfs-client-java
4646
connect_timeout = 2
4747
network_timeout = 30
4848
charset = UTF-8
49-
http.tracker_http_port = 8080
49+
http.tracker_http_port = 80
5050
http.anti_steal_token = no
5151
http.secret_key = FastDFS1234567890
5252
@@ -55,5 +55,73 @@ tracker_server = 10.0.11.248:22122
5555
tracker_server = 10.0.11.249:22122
5656
```
5757

58-
注:tracker_server指向您自己IP地址和端口,1-n个
58+
注1:tracker_server指向您自己IP地址和端口,1-n个
59+
注2:除了tracker_server,其它配置项都是可选的
5960

61+
62+
## .properties 配置文件、所在目录、加载优先顺序
63+
64+
配置文件名 fastdfs-client.properties(或使用其它文件名 xxx-yyy.properties)
65+
66+
文件所在位置可以是项目classpath(或OS文件系统目录比如/opt/):
67+
/opt/fastdfs-client.properties
68+
C:\Users\James\config\fastdfs-client.properties
69+
70+
优先按OS文件系统路径读取,没有找到才查找项目classpath,尤其针对linux环境下的相对路径比如:
71+
fastdfs-client.properties
72+
config/fastdfs-client.properties
73+
74+
```
75+
fastdfs.connect_timeout_in_seconds = 5
76+
fastdfs.network_timeout_in_seconds = 30
77+
fastdfs.charset = UTF-8
78+
fastdfs.http_anti_steal_token = false
79+
fastdfs.http_secret_key = FastDFS1234567890
80+
fastdfs.http_tracker_http_port = 80
81+
82+
fastdfs.tracker_servers = 10.0.11.201:22122,10.0.11.202:22122,10.0.11.203:22122
83+
```
84+
85+
注1:properties 配置文件中属性名跟 conf 配置文件不尽相同,并且统一加前缀"fastdfs.",便于整合到用户项目配置文件
86+
注2:fastdfs.tracker_servers 配置项不能重复属性名,多个 tracker_server 用逗号","隔开
87+
注3:除了fastdfs.tracker_servers,其它配置项都是可选的
88+
89+
90+
## 加载配置示例
91+
92+
加载原 conf 格式文件配置:
93+
ClientGlobal.init("fdfs_client.conf");
94+
ClientGlobal.init("config/fdfs_client.conf");
95+
ClientGlobal.init("/opt/fdfs_client.conf");
96+
ClientGlobal.init("C:\\Users\\James\\config\\fdfs_client.conf");
97+
98+
加载 properties 格式文件配置:
99+
ClientGlobal.initByProperties("fastdfs-client.properties");
100+
ClientGlobal.initByProperties("config/fastdfs-client.properties");
101+
ClientGlobal.initByProperties("/opt/fastdfs-client.properties");
102+
ClientGlobal.initByProperties("C:\\Users\\James\\config\\fastdfs-client.properties");
103+
104+
加载 Properties 对象配置:
105+
Properties props = new Properties();
106+
props.put(ClientGlobal.PROP_KEY_TRACKER_SERVERS, "10.0.11.101:22122,10.0.11.102:22122");
107+
ClientGlobal.initByProperties(props);
108+
109+
加载 trackerServers 字符串配置:
110+
String trackerServers = "10.0.11.101:22122,10.0.11.102:22122";
111+
ClientGlobal.initByTrackers(trackerServers);
112+
113+
114+
## 检查加载配置结果:
115+
116+
System.out.println("ClientGlobal.configInfo(): " + ClientGlobal.configInfo());
117+
```
118+
ClientGlobal.configInfo(): {
119+
g_connect_timeout(ms) = 5000
120+
g_network_timeout(ms) = 30000
121+
g_charset = UTF-8
122+
g_anti_steal_token = false
123+
g_secret_key = FastDFS1234567890
124+
g_tracker_http_port = 80
125+
trackerServers = 10.0.11.101:22122,10.0.11.102:22122
126+
}
127+
```

fastdfs-client.properties

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## fastdfs-client.properties
2+
3+
fastdfs.connect_timeout_in_seconds = 5
4+
fastdfs.network_timeout_in_seconds = 30
5+
6+
fastdfs.charset = UTF-8
7+
8+
fastdfs.http_anti_steal_token = false
9+
fastdfs.http_secret_key = FastDFS1234567890
10+
fastdfs.http_tracker_http_port = 80
11+
12+
fastdfs.tracker_servers = 10.0.11.201:22122,10.0.11.202:22122,10.0.11.203:22122
13+

src/main/java/org/csource/common/IniFileReader.java

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,33 @@ public IniFileReader(String conf_filename) throws IOException {
3030
loadFromFile(conf_filename);
3131
}
3232

33-
private static ClassLoader classLoader() {
33+
public static ClassLoader classLoader() {
3434
ClassLoader loader = Thread.currentThread().getContextClassLoader();
3535
if (loader == null) {
3636
loader = ClassLoader.getSystemClassLoader();
3737
}
3838
return loader;
3939
}
4040

41+
public static InputStream loadFromOsFileSystemOrClasspathAsStream(String filePath) {
42+
InputStream in = null;
43+
try {
44+
// 优先从文件系统路径加载
45+
if (new File(filePath).exists()) {
46+
in = new FileInputStream(filePath);
47+
//System.out.println("loadFrom...file path done");
48+
}
49+
// 从类路径加载
50+
else {
51+
in = classLoader().getResourceAsStream(filePath);
52+
//System.out.println("loadFrom...class path done");
53+
}
54+
} catch (Exception ex) {
55+
ex.printStackTrace();
56+
}
57+
return in;
58+
}
59+
4160
/**
4261
* get the config filename
4362
*
@@ -128,18 +147,8 @@ public String[] getValues(String name) {
128147
}
129148

130149
private void loadFromFile(String confFilePath) throws IOException {
131-
InputStream in = null;
150+
InputStream in = loadFromOsFileSystemOrClasspathAsStream(confFilePath);
132151
try {
133-
// 优先从文件系统路径加载
134-
if (new File(confFilePath).exists()) {
135-
in = new FileInputStream(confFilePath);
136-
//System.out.println("loadFrom...file path done");
137-
}
138-
// 从类路径加载
139-
else {
140-
in = classLoader().getResourceAsStream(confFilePath);
141-
//System.out.println("loadFrom...class path done");
142-
}
143152
readToParamTable(in);
144153
} catch (Exception ex) {
145154
ex.printStackTrace();

src/main/java/org/csource/fastdfs/ClientGlobal.java

Lines changed: 136 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@
1212
import org.csource.common.MyException;
1313

1414
import java.io.IOException;
15+
import java.io.InputStream;
1516
import java.net.InetSocketAddress;
1617
import java.net.Socket;
18+
import java.util.ArrayList;
19+
import java.util.List;
20+
import java.util.Properties;
1721

1822
/**
1923
* Global variables
@@ -22,14 +26,37 @@
2226
* @version Version 1.11
2327
*/
2428
public class ClientGlobal {
25-
public static final int DEFAULT_CONNECT_TIMEOUT = 5; //second
29+
30+
public static final String CONF_KEY_CONNECT_TIMEOUT = "connect_timeout";
31+
public static final String CONF_KEY_NETWORK_TIMEOUT = "network_timeout";
32+
public static final String CONF_KEY_CHARSET = "charset";
33+
public static final String CONF_KEY_HTTP_ANTI_STEAL_TOKEN = "http.anti_steal_token";
34+
public static final String CONF_KEY_HTTP_SECRET_KEY = "http.secret_key";
35+
public static final String CONF_KEY_HTTP_TRACKER_HTTP_PORT = "http.tracker_http_port";
36+
public static final String CONF_KEY_TRACKER_SERVER = "tracker_server";
37+
38+
public static final String PROP_KEY_CONNECT_TIMEOUT_IN_SECONDS = "fastdfs.connect_timeout_in_seconds";
39+
public static final String PROP_KEY_NETWORK_TIMEOUT_IN_SECONDS = "fastdfs.network_timeout_in_seconds";
40+
public static final String PROP_KEY_CHARSET = "fastdfs.charset";
41+
public static final String PROP_KEY_HTTP_ANTI_STEAL_TOKEN = "fastdfs.http_anti_steal_token";
42+
public static final String PROP_KEY_HTTP_SECRET_KEY = "fastdfs.http_secret_key";
43+
public static final String PROP_KEY_HTTP_TRACKER_HTTP_PORT = "fastdfs.http_tracker_http_port";
44+
public static final String PROP_KEY_TRACKER_SERVERS = "fastdfs.tracker_servers";
45+
46+
public static final int DEFAULT_CONNECT_TIMEOUT = 5; //second
2647
public static final int DEFAULT_NETWORK_TIMEOUT = 30; //second
27-
public static int g_connect_timeout; //millisecond
28-
public static int g_network_timeout; //millisecond
29-
public static String g_charset;
30-
public static int g_tracker_http_port;
31-
public static boolean g_anti_steal_token; //if anti-steal token
32-
public static String g_secret_key; //generage token secret key
48+
public static final String DEFAULT_CHARSET = "UTF-8";
49+
public static final boolean DEFAULT_HTTP_ANTI_STEAL_TOKEN = false;
50+
public static final String DEFAULT_HTTP_SECRET_KEY = "FastDFS1234567890";
51+
public static final int DEFAULT_HTTP_TRACKER_HTTP_PORT = 80;
52+
53+
public static int g_connect_timeout = DEFAULT_CONNECT_TIMEOUT * 1000; //millisecond
54+
public static int g_network_timeout = DEFAULT_NETWORK_TIMEOUT * 1000; //millisecond
55+
public static String g_charset = DEFAULT_CHARSET;
56+
public static boolean g_anti_steal_token = DEFAULT_HTTP_ANTI_STEAL_TOKEN; //if anti-steal token
57+
public static String g_secret_key = DEFAULT_HTTP_SECRET_KEY; //generage token secret key
58+
public static int g_tracker_http_port = DEFAULT_HTTP_TRACKER_HTTP_PORT;
59+
3360
public static TrackerGroup g_tracker_group;
3461

3562
private ClientGlobal() {
@@ -87,6 +114,87 @@ public static void init(String conf_filename) throws IOException, MyException {
87114
}
88115
}
89116

117+
/**
118+
* load from properties file
119+
*
120+
* @param propsFilePath properties file path, eg:
121+
* "fastdfs-client.properties"
122+
* "config/fastdfs-client.properties"
123+
* "/opt/fastdfs-client.properties"
124+
* "C:\\Users\\James\\config\\fastdfs-client.properties"
125+
* properties文件至少包含一个配置项 fastdfs.tracker_servers 例如:
126+
* fastdfs.tracker_servers = 10.0.11.245:22122,10.0.11.246:22122
127+
* server的IP和端口用冒号':'分隔
128+
* server之间用逗号','分隔
129+
*/
130+
public static void initByProperties(String propsFilePath) throws IOException, MyException {
131+
Properties props = new Properties();
132+
InputStream in = IniFileReader.loadFromOsFileSystemOrClasspathAsStream(propsFilePath);
133+
if (in != null) {
134+
props.load(in);
135+
}
136+
initByProperties(props);
137+
}
138+
139+
public static void initByProperties(Properties props) throws IOException, MyException {
140+
String trackerServersConf = props.getProperty(PROP_KEY_TRACKER_SERVERS);
141+
if (trackerServersConf == null || trackerServersConf.trim().length() == 0) {
142+
throw new MyException(String.format("configure item %s is required", PROP_KEY_TRACKER_SERVERS));
143+
}
144+
initByTrackers(trackerServersConf.trim());
145+
146+
String connectTimeoutInSecondsConf = props.getProperty(PROP_KEY_CONNECT_TIMEOUT_IN_SECONDS);
147+
String networkTimeoutInSecondsConf = props.getProperty(PROP_KEY_NETWORK_TIMEOUT_IN_SECONDS);
148+
String charsetConf = props.getProperty(PROP_KEY_CHARSET);
149+
String httpAntiStealTokenConf = props.getProperty(PROP_KEY_HTTP_ANTI_STEAL_TOKEN);
150+
String httpSecretKeyConf = props.getProperty(PROP_KEY_HTTP_SECRET_KEY);
151+
String httpTrackerHttpPortConf = props.getProperty(PROP_KEY_HTTP_TRACKER_HTTP_PORT);
152+
if (connectTimeoutInSecondsConf != null && connectTimeoutInSecondsConf.trim().length() != 0) {
153+
g_connect_timeout = Integer.parseInt(connectTimeoutInSecondsConf.trim()) * 1000;
154+
}
155+
if (networkTimeoutInSecondsConf != null && networkTimeoutInSecondsConf.trim().length() != 0) {
156+
g_network_timeout = Integer.parseInt(networkTimeoutInSecondsConf.trim()) * 1000;
157+
}
158+
if (charsetConf != null && charsetConf.trim().length() != 0) {
159+
g_charset = charsetConf.trim();
160+
}
161+
if (httpAntiStealTokenConf != null && httpAntiStealTokenConf.trim().length() != 0) {
162+
g_anti_steal_token = Boolean.parseBoolean(httpAntiStealTokenConf);
163+
}
164+
if (httpSecretKeyConf != null && httpSecretKeyConf.trim().length() != 0) {
165+
g_secret_key = httpSecretKeyConf.trim();
166+
}
167+
if (httpTrackerHttpPortConf != null && httpTrackerHttpPortConf.trim().length() != 0) {
168+
g_tracker_http_port = Integer.parseInt(httpTrackerHttpPortConf);
169+
}
170+
}
171+
172+
/**
173+
* load from properties file
174+
*
175+
* @param trackerServers 例如:"10.0.11.245:22122,10.0.11.246:22122"
176+
* server的IP和端口用冒号':'分隔
177+
* server之间用逗号','分隔
178+
*/
179+
public static void initByTrackers(String trackerServers) throws IOException, MyException {
180+
List<InetSocketAddress> list = new ArrayList();
181+
String spr1 = ",";
182+
String spr2 = ":";
183+
String[] arr1 = trackerServers.trim().split(spr1);
184+
for (String addrStr : arr1) {
185+
String[] arr2 = addrStr.trim().split(spr2);
186+
String host = arr2[0].trim();
187+
int port = Integer.parseInt(arr2[1].trim());
188+
list.add(new InetSocketAddress(host, port));
189+
}
190+
InetSocketAddress[] trackerAddresses = list.toArray(new InetSocketAddress[list.size()]);
191+
initByTrackers(trackerAddresses);
192+
}
193+
194+
public static void initByTrackers(InetSocketAddress[] trackerAddresses) throws IOException, MyException {
195+
g_tracker_group = new TrackerGroup(trackerAddresses);
196+
}
197+
90198
/**
91199
* construct Socket object
92200
*
@@ -173,4 +281,25 @@ public static TrackerGroup getG_tracker_group() {
173281
public static void setG_tracker_group(TrackerGroup tracker_group) {
174282
ClientGlobal.g_tracker_group = tracker_group;
175283
}
284+
285+
public static String configInfo() {
286+
String trackerServers = "";
287+
if (g_tracker_group != null) {
288+
InetSocketAddress[] trackerAddresses = g_tracker_group.tracker_servers;
289+
for (InetSocketAddress inetSocketAddress : trackerAddresses) {
290+
if(trackerServers.length() > 0) trackerServers += ",";
291+
trackerServers += inetSocketAddress.toString().substring(1);
292+
}
293+
}
294+
return "{"
295+
+ "\n g_connect_timeout(ms) = " + g_connect_timeout
296+
+ "\n g_network_timeout(ms) = " + g_network_timeout
297+
+ "\n g_charset = " + g_charset
298+
+ "\n g_anti_steal_token = " + g_anti_steal_token
299+
+ "\n g_secret_key = " + g_secret_key
300+
+ "\n g_tracker_http_port = " + g_tracker_http_port
301+
+ "\n trackerServers = " + trackerServers
302+
+ "\n}";
303+
}
304+
176305
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## fastdfs-client.properties
2+
3+
fastdfs.connect_timeout_in_seconds = 5
4+
fastdfs.network_timeout_in_seconds = 30
5+
6+
fastdfs.charset = UTF-8
7+
8+
fastdfs.http_anti_steal_token = false
9+
fastdfs.http_secret_key = FastDFS1234567890
10+
fastdfs.http_tracker_http_port = 80
11+
12+
fastdfs.tracker_servers = 10.0.11.201:22122,10.0.11.202:22122,10.0.11.203:22122
13+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.csource.fastdfs;
2+
3+
import java.util.Properties;
4+
5+
/**
6+
* Created by James on 2017/5/19.
7+
*/
8+
public class ClientGlobalTests {
9+
10+
public static void main(String[] args) throws Exception {
11+
String trackerServers = "10.0.11.101:22122,10.0.11.102:22122";
12+
ClientGlobal.initByTrackers(trackerServers);
13+
System.out.println("ClientGlobal.configInfo() : " + ClientGlobal.configInfo());
14+
15+
String propFilePath = "fastdfs-client.properties";
16+
ClientGlobal.initByProperties(propFilePath);
17+
System.out.println("ClientGlobal.configInfo() : " + ClientGlobal.configInfo());
18+
19+
Properties props = new Properties();
20+
props.put(ClientGlobal.PROP_KEY_TRACKER_SERVERS, "10.0.11.101:22122,10.0.11.102:22122");
21+
ClientGlobal.initByProperties(props);
22+
System.out.println("ClientGlobal.configInfo(): " + ClientGlobal.configInfo());
23+
24+
}
25+
26+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## fastdfs-client.properties
2+
3+
fastdfs.connect_timeout_in_seconds = 5
4+
fastdfs.network_timeout_in_seconds = 30
5+
6+
fastdfs.charset = UTF-8
7+
8+
fastdfs.http_anti_steal_token = false
9+
fastdfs.http_secret_key = FastDFS1234567890
10+
fastdfs.http_tracker_http_port = 80
11+
12+
fastdfs.tracker_servers = 10.0.11.201:22122,10.0.11.202:22122,10.0.11.203:22122
13+

0 commit comments

Comments
 (0)