Merge pull request #108 from sunqiangwei1988/master

Added: 增加IPv6支持
This commit is contained in:
YuQing
2023-11-25 09:29:30 +08:00
committed by GitHub
5 changed files with 78 additions and 12 deletions
+9
View File
@@ -5,6 +5,13 @@ http.tracker_http_port = 8080
http.anti_steal_token = no
http.secret_key = FastDFS1234567890
#tracker_server
# IPv4:
# for example: 192.168.2.100,122.244.141.46:22122
#
# IPv6:
# for example: [2409:8a20:42d:2f40:587a:4c47:72c0:ad8e]:22122
#
tracker_server = 10.0.11.247:22122
tracker_server = 10.0.11.248:22122
tracker_server = 10.0.11.249:22122
@@ -13,3 +20,5 @@ connection_pool.enabled = true
connection_pool.max_count_per_entry = 500
connection_pool.max_idle_time = 3600
connection_pool.max_wait_time_in_ms = 1000
server_ipv6.enabled = false
@@ -13,6 +13,7 @@ import org.csource.common.MyException;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.ArrayList;
@@ -48,6 +49,8 @@ public class ClientGlobal {
public static final String PROP_KEY_CONNECTION_POOL_MAX_IDLE_TIME = "fastdfs.connection_pool.max_idle_time";
public static final String PROP_KEY_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS = "fastdfs.connection_pool.max_wait_time_in_ms";
public static final String PROP_KEY_SERVER_IPV6_ENABLED = "fastdfs.server_ipv6.enabled";
public static final int DEFAULT_CONNECT_TIMEOUT = 5; //second
public static final int DEFAULT_NETWORK_TIMEOUT = 30; //second
public static final String DEFAULT_CHARSET = "UTF-8";
@@ -113,12 +116,19 @@ public class ClientGlobal {
InetSocketAddress[] tracker_servers = new InetSocketAddress[szTrackerServers.length];
for (int i = 0; i < szTrackerServers.length; i++) {
parts = szTrackerServers[i].split("\\:", 2);
if(szTrackerServers[i].contains("[")){
parts = new String[2];
parts[0] = szTrackerServers[i].substring(1, szTrackerServers[i].indexOf("]"));
parts[1] = szTrackerServers[i].substring(szTrackerServers[i].lastIndexOf(":") + 1);
}else {
parts = szTrackerServers[i].split("\\:", 2);
}
if (parts.length != 2) {
throw new MyException("the value of item \"tracker_server\" is invalid, the correct format is host:port");
}
tracker_servers[i] = new InetSocketAddress(parts[0].trim(), Integer.parseInt(parts[1].trim()));
tracker_servers[i] = new InetSocketAddress(InetAddress.getByName(parts[0].trim()), Integer.parseInt(parts[1].trim()));
}
g_tracker_group = new TrackerGroup(tracker_servers);
@@ -138,6 +148,9 @@ public class ClientGlobal {
if (g_connection_pool_max_wait_time_in_ms < 0) {
g_connection_pool_max_wait_time_in_ms = DEFAULT_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS;
}
if(iniReader.getBoolValue("server_ipv6.enabled",false)){
ProtoCommon.useIPv6();
}
}
/**
@@ -179,6 +192,7 @@ public class ClientGlobal {
String poolMaxCountPerEntry = props.getProperty(PROP_KEY_CONNECTION_POOL_MAX_COUNT_PER_ENTRY);
String poolMaxIdleTime = props.getProperty(PROP_KEY_CONNECTION_POOL_MAX_IDLE_TIME);
String poolMaxWaitTimeInMS = props.getProperty(PROP_KEY_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS);
String serverIPv6Enabled = props.getProperty(PROP_KEY_SERVER_IPV6_ENABLED);
if (connectTimeoutInSecondsConf != null && connectTimeoutInSecondsConf.trim().length() != 0) {
g_connect_timeout = Integer.parseInt(connectTimeoutInSecondsConf.trim()) * 1000;
}
@@ -209,6 +223,11 @@ public class ClientGlobal {
if (poolMaxWaitTimeInMS != null && poolMaxWaitTimeInMS.trim().length() != 0) {
g_connection_pool_max_wait_time_in_ms = Integer.parseInt(poolMaxWaitTimeInMS);
}
if (serverIPv6Enabled != null && serverIPv6Enabled.trim().length() != 0) {
if(Boolean.parseBoolean(poolEnabled)){
ProtoCommon.useIPv6();
}
}
}
/**
@@ -224,10 +243,16 @@ public class ClientGlobal {
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));
if(addrStr.contains("[")){
String host = addrStr.substring(1, addrStr.indexOf("]"));
int port = Integer.parseInt(addrStr.substring(addrStr.lastIndexOf(":") + 1));
list.add(new InetSocketAddress(InetAddress.getByName(host), port));
}else {
String[] arr2 = addrStr.trim().split(spr2);
String host = arr2[0].trim();
int port = Integer.parseInt(arr2[1].trim());
list.add(new InetSocketAddress(InetAddress.getByName(host), port));
}
}
InetSocketAddress[] trackerAddresses = list.toArray(new InetSocketAddress[list.size()]);
initByTrackers(trackerAddresses);
@@ -247,7 +272,7 @@ public class ClientGlobal {
public static Socket getSocket(String ip_addr, int port) throws IOException {
Socket sock = new Socket();
sock.setSoTimeout(ClientGlobal.g_network_timeout);
sock.connect(new InetSocketAddress(ip_addr, port), ClientGlobal.g_connect_timeout);
sock.connect(new InetSocketAddress(InetAddress.getByName(ip_addr), port), ClientGlobal.g_connect_timeout);
return sock;
}
@@ -72,15 +72,17 @@ public class ProtoCommon {
public static final int FDFS_PROTO_PKG_LEN_SIZE = 8;
public static final int FDFS_PROTO_CMD_SIZE = 1;
public static final int FDFS_GROUP_NAME_MAX_LEN = 16;
public static final int FDFS_IPADDR_SIZE = 16;
public static final int FDFS_IPADDR_V4_SIZE = 16;
public static final int FDFS_IPADDR_V6_SIZE = 46;
public static int FDFS_IPADDR_SIZE = FDFS_IPADDR_V4_SIZE;
public static final int FDFS_DOMAIN_NAME_MAX_SIZE = 128;
public static final int FDFS_VERSION_SIZE = 6;
public static final int FDFS_STORAGE_ID_MAX_SIZE = 16;
public static final String FDFS_RECORD_SEPERATOR = "\u0001";
public static final String FDFS_FIELD_SEPERATOR = "\u0002";
public static final int TRACKER_QUERY_STORAGE_FETCH_BODY_LEN = FDFS_GROUP_NAME_MAX_LEN
public static int TRACKER_QUERY_STORAGE_FETCH_BODY_LEN = FDFS_GROUP_NAME_MAX_LEN
+ FDFS_IPADDR_SIZE - 1 + FDFS_PROTO_PKG_LEN_SIZE;
public static final int TRACKER_QUERY_STORAGE_STORE_BODY_LEN = FDFS_GROUP_NAME_MAX_LEN
public static int TRACKER_QUERY_STORAGE_STORE_BODY_LEN = FDFS_GROUP_NAME_MAX_LEN
+ FDFS_IPADDR_SIZE + FDFS_PROTO_PKG_LEN_SIZE;
public static final byte FDFS_FILE_EXT_NAME_MAX_LEN = 6;
public static final byte FDFS_FILE_PREFIX_MAX_LEN = 16;
@@ -502,4 +504,16 @@ public class ProtoCommon {
this.body_len = body_len;
}
}
/**
* 设置系统使用IPv6地址进行通信
*/
public static void useIPv6(){
FDFS_IPADDR_SIZE = FDFS_IPADDR_V6_SIZE;
TRACKER_QUERY_STORAGE_FETCH_BODY_LEN = FDFS_GROUP_NAME_MAX_LEN
+ FDFS_IPADDR_SIZE - 1 + FDFS_PROTO_PKG_LEN_SIZE;
TRACKER_QUERY_STORAGE_STORE_BODY_LEN = FDFS_GROUP_NAME_MAX_LEN
+ FDFS_IPADDR_SIZE + FDFS_PROTO_PKG_LEN_SIZE;
}
}
@@ -9,6 +9,13 @@ fastdfs.http_anti_steal_token = false
fastdfs.http_secret_key = FastDFS1234567890
fastdfs.http_tracker_http_port = 80
#tracker_server
# IPv4:
# for example: 192.168.2.100,122.244.141.46:22122
#
# IPv6:
# for example: [2409:8a20:42d:2f40:587a:4c47:72c0:ad8e]:22122
#
fastdfs.tracker_servers = 185.245.40.70:22122
## Whether to open the connection pool, if not, create a new connection every time
@@ -21,4 +28,6 @@ fastdfs.connection_pool.max_count_per_entry = 500
fastdfs.connection_pool.max_idle_time = 3600
## Maximum waiting time when the maximum number of connections is reached, unit: millisecond, default value is 1000
fastdfs.connection_pool.max_wait_time_in_ms = 1000
fastdfs.connection_pool.max_wait_time_in_ms = 1000
server_ipv6.enabled = false
+10 -1
View File
@@ -5,10 +5,19 @@ http.tracker_http_port = 8080
http.anti_steal_token = no
http.secret_key = FastDFS1234567890
#tracker_server
# IPv4:
# for example: 192.168.2.100,122.244.141.46:22122
#
# IPv6:
# for example: [2409:8a20:42d:2f40:587a:4c47:72c0:ad8e]:22122
#
tracker_server = 10.0.11.243:22122
tracker_server = 10.0.11.244:22122
connection_pool.enabled = true
connection_pool.max_count_per_entry = 500
connection_pool.max_idle_time = 3600
connection_pool.max_wait_time_in_ms = 1000
connection_pool.max_wait_time_in_ms = 1000
server_ipv6.enabled = false