From e0242a440ec62531c23021c4b8b8a614d5420c7a Mon Sep 17 00:00:00 2001 From: kuangyongqiang Date: Fri, 4 Sep 2020 10:01:54 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=80=E5=8D=95=E5=A2=9E=E5=8A=A0=E5=86=85?= =?UTF-8?q?=E7=BD=91=E7=BD=91=E6=98=A0=E5=B0=84=E6=94=AF=E6=8C=81=E3=80=82?= =?UTF-8?q?=E5=9C=A8=E6=9F=90=E4=BA=9B=E6=83=85=E5=86=B5=E4=B8=8B=EF=BC=8C?= =?UTF-8?q?fdfs=E9=85=8D=E7=BD=AE=E5=9C=A8=E5=86=85=E7=BD=91=EF=BC=8C?= =?UTF-8?q?=E4=BD=86=E6=98=AF=E9=9C=80=E8=A6=81=E9=80=9A=E8=BF=87=E5=A4=96?= =?UTF-8?q?=E7=BD=91=E6=98=A0=E5=B0=84=E6=9D=A5=E6=93=8D=E4=BD=9CFdfs?= =?UTF-8?q?=EF=BC=8C=E5=8F=AF=E9=80=9A=E8=BF=87=E9=85=8D=E7=BD=AE=E6=9D=A5?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E5=A4=84=E7=90=86=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/csource/common/ConfigureMapper.java | 60 +++++++++++++++++++ .../org/csource/fastdfs/TrackerClient.java | 8 +++ .../fdfs_ip_mapper.properties.sample | 2 + 3 files changed, 70 insertions(+) create mode 100644 src/main/java/org/csource/common/ConfigureMapper.java create mode 100644 src/main/resources/fdfs_ip_mapper.properties.sample diff --git a/src/main/java/org/csource/common/ConfigureMapper.java b/src/main/java/org/csource/common/ConfigureMapper.java new file mode 100644 index 0000000..1a2683e --- /dev/null +++ b/src/main/java/org/csource/common/ConfigureMapper.java @@ -0,0 +1,60 @@ +package org.csource.common; + +import java.util.Properties; + +/** + * Description: 在特殊的内外网环境下,如果fdfs配置在内网,但是想通过外网操作fdfs上传文件,可以使用本配置方法来做IP映射。 + * 前提:外网映射的IP端口可达。 + * 配置格式:内网ip=外网IP:外网端口 + * + * @author kyq + * @version 1.0 + * @Date 2020/9/4 9:41 + */ +public class ConfigureMapper { + private static final String SYS_CONFIG_FILE = "fdfs_ip_mapper.properties"; + private static Properties sysProperties = new Properties(); + + static { + try { + sysProperties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("fdfs_ip_mapper.properties")); + } catch (Exception var1) { + System.out.println("fdfs_ip_mapper.properties not found!"); + } + + } + + public static String getProperty(String strKey) { + String strValue = ""; + + try { + strValue = sysProperties.getProperty(strKey); + } catch (Exception var3) { + System.out.println("Property <" + strKey + "> not found!"); + } + + return replaceNull(strValue); + } + + public static String replaceNull(Object obj) { + return isEmpty(obj) ? "" : obj.toString(); + } + + public static boolean isEmpty(Object... objs) { + if (objs == null) { + return true; + } else { + Object[] var4 = objs; + int var3 = objs.length; + + for(int var2 = 0; var2 < var3; ++var2) { + Object obj = var4[var2]; + if (obj != null && !"".equals(obj)) { + return false; + } + } + + return true; + } + } +} diff --git a/src/main/java/org/csource/fastdfs/TrackerClient.java b/src/main/java/org/csource/fastdfs/TrackerClient.java index 22c4bba..0d5a765 100644 --- a/src/main/java/org/csource/fastdfs/TrackerClient.java +++ b/src/main/java/org/csource/fastdfs/TrackerClient.java @@ -8,6 +8,7 @@ package org.csource.fastdfs; +import org.csource.common.ConfigureMapper; import org.csource.common.MyException; import org.csource.fastdfs.pool.Connection; @@ -248,6 +249,13 @@ public StorageServer[] getStoreStorages(TrackerServer trackerServer, String grou port = (int) ProtoCommon.buff2long(pkgInfo.body, offset); offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + String mapper = ConfigureMapper.getProperty(ip_addr); + if (!ConfigureMapper.isEmpty(new Object[]{mapper})) { + String[] mappingIp = mapper.split(":"); + ip_addr = mappingIp[0]; + port = Integer.parseInt(mappingIp[1]); + } + results[i] = new StorageServer(ip_addr, port, store_path); } diff --git a/src/main/resources/fdfs_ip_mapper.properties.sample b/src/main/resources/fdfs_ip_mapper.properties.sample new file mode 100644 index 0000000..195a375 --- /dev/null +++ b/src/main/resources/fdfs_ip_mapper.properties.sample @@ -0,0 +1,2 @@ +# Config intranet storage ip to the mapped internet ip and port +#10.0.11.244=113.204.217.100:22124 \ No newline at end of file