`
247687009
  • 浏览: 173219 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

通过修改源码让FastDFS能正确的读取到classpath:配置文件

阅读更多
说句难听的话,FastDFS的java客户端,站在java程序员的角度来讲是相当的难用啊。
试着将FastDFS的javaClient整合进电商平台之中
FastDFS客户端初始化方式有两种一种是ClientGlobal.init(String)传入配置文件的路径,另外一种是通过ClientGlobal.set的方式,不过实在是不爽这种set方式啊,附上源码就知道了。

	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 TrackerGroup g_tracker_group;
        //set
        	public static int getG_connect_timeout()
	{
		return g_connect_timeout;
	}
	
	public static void setG_connect_timeout(int connect_timeout)
	{
		ClientGlobal.g_connect_timeout = connect_timeout;
	}
	
	public static int getG_network_timeout()
	{
		return g_network_timeout;
	}
	
	public static void setG_network_timeout(int network_timeout)
	{
		ClientGlobal.g_network_timeout = network_timeout;
	}
	
	public static String getG_charset()
	{
		return g_charset;
	}
	

完全就是C程序的风格啊。作为一枚称职的java程序猿,实在是受不了这种方式。那么就用配置文件的方式了吧,来吧,试一试
//先来试试相对路径
ClientGlobal.init("/fastdfs.conf");
//运行,配置文件未找到 
//那么在来试试classpath
ClientGlobal.init("classpath:fastdfs.conf");
//运行配置文件未找到。
//看来要使用绝招了,直接给系统的绝对路径
ClientGlobal.init("D:/seafood_project/seafood-core/src/main/resources/fastdfs.conf");
//这下子总算是可以了。

但是又来问题了,项目部署的时候不可能这样子配置啊,好的选择肯定还是的从classpath下面找才是。没办法又看源码吧,直接上读取配置文件的源码,你就明白了为啥读取不到了

public class IniFileReader
{
	//居然在用hashTable。。。。。
        private Hashtable paramTable;
	private String conf_filename;
	
/**
* @param conf_filename config filename
*/
	public IniFileReader(String conf_filename) throws FileNotFoundException, IOException
	{
		this.conf_filename = conf_filename;
                //看来这里是我们需要的
		loadFromFile(conf_filename);
	}
        //看到loadFromFile的源码的时候,根本忍不住要改源码啊
        private void loadFromFile(String conf_filename) throws FileNotFoundException, IOException
	{
		FileReader fReader;
		BufferedReader buffReader;
		String line;
		String[] parts;
		String name;
		String value;
		Object obj;
		ArrayList valueList;
		
	  fReader = new FileReader(conf_filename);
	  buffReader = new BufferedReader(fReader);
	  this.paramTable = new Hashtable();
	  
	  try
	  {
	  	while ((line=buffReader.readLine()) != null)
	  	{
	  		line = line.trim();
	  		if (line.length() == 0 || line.charAt(0) == '#')
	  		{
	  			continue;
	  		}
	  		
	  		parts = line.split("=", 2);
	  		if (parts.length != 2)
	  		{
	  			continue;
	  		}
	  	
	  		name = parts[0].trim();
	  		value = parts[1].trim();
	  		
	  		obj = this.paramTable.get(name);
	  		if (obj == null)
	  		{
	  			this.paramTable.put(name, value);
	  		}
	  		else if (obj instanceof String)
	  		{
	  			valueList = new ArrayList();
	  			valueList.add(obj);
	  			valueList.add(value);
	  			this.paramTable.put(name, valueList);
	  		}
	  		else
	  		{
	  			valueList = (ArrayList)obj;
	  			valueList.add(value);
	  		}
	  	}
	  }
	  finally
	  {
	  	fReader.close();
	  }
  } 


看到这儿应该明白为啥读不了了吧!泪奔啊!附上改写的源代码
public class IniFileReader {
        private PropertiesLoader loader;
        private String conf_filename;

        /**
         * 
        * <p>Title: </p>
        * <p>Description: </p>
        * @param conf_filename
        * @throws FileNotFoundException
        * @throws IOException
         */
        public IniFileReader(String conf_filename) throws FileNotFoundException, IOException {
                this.conf_filename = conf_filename;
                loadFromFile(conf_filename);
        }

        /**
         * 
        * @Description: TODO(这里用一句话描述这个方法的作用)
        * @author LiuYi
        * @date 2014年6月5日 上午10:11:14
        *  @return  String
         */
        public String getConfFilename() {
                return this.conf_filename;
        }

        /**
         * 
        * @Description: TODO(这里用一句话描述这个方法的作用)
        * @author LiuYi
        * @date 2014年6月6日 上午10:11:11
        *  @param name
        *  @return  String
         */
        public String getStrValue(String name) {
                return this.loader.getProperty(name);
        }

        /**
         * 
        * @Description: TODO(这里用一句话描述这个方法的作用)
        * @author LiuYi
        * @date 2014年6月5日 上午10:11:01
        *  @param name
        *  @param default_value
        *  @return  int
         */
        public int getIntValue(String name, int default_value) {
                String szValue = this.loader.getProperty(name);
                if (szValue == null || "".equals(szValue)) {
                        return default_value;
                }
                return Integer.parseInt(szValue);
        }

        /**
         * 
        * @Description: TODO(这里用一句话描述这个方法的作用)
        * @author LiuYi
        * @date 2014年6月5日 上午10:10:53
        *  @param name
        *  @param default_value
        *  @return  boolean
         */
        public boolean getBoolValue(String name, boolean default_value) {
                String szValue = this.loader.getProperty(name);
                if (szValue == null) {
                        return default_value;
                }
                return szValue.equalsIgnoreCase("yes") || szValue.equalsIgnoreCase("on")
                                || szValue.equalsIgnoreCase("true") || szValue.equals("1");
        }

        /**
         * 
        * @Description: TODO()
        * @author LiuYi
        * @date 2014年6月5日 上午10:10:35
        *  @param name
        *  @return  String[]
         */
        public String[] getValues(String name) {
                List<String> values = new ArrayList<String>();
                String val = this.loader.getProperty(name);
                if (val.contains(",")) {
                        for (String v : val.split(",")) {
                                values.add(v);
                        }
                } else {
                        values.add(val);
                }
                return values.toArray(new String[values.size()]);
        }
        /**
         * 
        * @Description: TODO(这里用一句话描述这个方法的作用)
        * @author LiuYi
        * @date 2014年6月5日 上午10:11:54
        *  @param resourcesPaths
        *  @throws FileNotFoundException
        *  @throws IOException  void
         */
        private void loadFromFile(String... resourcesPaths) throws FileNotFoundException, IOException {
                this.loader = new PropertiesLoader(resourcesPaths);
        }
}

PropertiesLoader依赖了Spring的io包附上源码
/**
 * Properties文件载入工具类. 可载入多个properties文件, 相同的属性在最后载入的文件中的值将会覆盖之前的值,但以System的Property优先.
 * @author LiuYi
 * @version 2014-05-21
 */
public class PropertiesLoader {
	private static ResourceLoader resourceLoader = new DefaultResourceLoader();
	private final Properties properties;
	public PropertiesLoader(String... resourcesPaths) {
		properties = loadProperties(resourcesPaths);
	}
	public Properties getProperties() {
		return properties;
	}
	/**
	 * 取出Property,但以System的Property优先,取不到返回空字符串.
	 */
	private String getValue(String key) {
		String systemProperty = System.getProperty(key);
		if (systemProperty != null) {
			return systemProperty;
		}
		if (properties.containsKey(key)) {
	        return properties.getProperty(key);
	    }
	    return "";
	}
	/**
	 * 取出String类型的Property,但以System的Property优先,如果都为Null则抛出异常.
	 */
	public String getProperty(String key) {
		String value = getValue(key);
		if (value == null) {
			throw new NoSuchElementException();
		}
		return value;
	}

	/**
	 * 取出String类型的Property,但以System的Property优先.如果都为Null则返回Default值.
	 */
	public String getProperty(String key, String defaultValue) {
		String value = getValue(key);
		return value != null ? value : defaultValue;
	}

	/**
	 * 取出Integer类型的Property,但以System的Property优先.如果都为Null或内容错误则抛出异常.
	 */
	public Integer getInteger(String key) {
		String value = getValue(key);
		if (value == null) {
			return null;
		}
		return Integer.valueOf(value);
	}

	/**
	 * 取出Integer类型的Property,但以System的Property优先.如果都为Null则返回Default值,如果内容错误则抛出异常
	 */
	public Integer getInteger(String key, Integer defaultValue) {
		String value = getValue(key);
		return value != null ? Integer.valueOf(value) : defaultValue;
	}

	/**
	 * 取出Double类型的Property,但以System的Property优先.如果都为Null或内容错误则抛出异常.
	 */
	public Double getDouble(String key) {
		String value = getValue(key);
		if (value == null) {
			throw new NoSuchElementException();
		}
		return Double.valueOf(value);
	}

	/**
	 * 取出Double类型的Property,但以System的Property优先.如果都为Null则返回Default值,如果内容错误则抛出异常
	 */
	public Double getDouble(String key, Integer defaultValue) {
		String value = getValue(key);
		return value != null ? Double.valueOf(value) : defaultValue;
	}

	/**
	 * 取出Boolean类型的Property,但以System的Property优先.如果都为Null抛出异常,如果内容不是true/false则返回false.
	 */
	public Boolean getBoolean(String key) {
		String value = getValue(key);
		if (value == null) {
			throw new NoSuchElementException();
		}
		return Boolean.valueOf(value);
	}

	/**
	 * 取出Boolean类型的Property,但以System的Property优先.如果都为Null则返回Default值,如果内容不为true/false则返回false.
	 */
	public Boolean getBoolean(String key, boolean defaultValue) {
		String value = getValue(key);
		return value != null ? Boolean.valueOf(value) : defaultValue;
	}

	/**
	 * 载入多个文件, 文件路径使用Spring Resource格式.
	 */
	private Properties loadProperties(String... resourcesPaths) {
		Properties props = new Properties();
		for (String location : resourcesPaths) {
//			logger.debug("Loading properties file from:" + location);
			InputStream is = null;
			try {
				Resource resource = resourceLoader.getResource(location);
				is = resource.getInputStream();
				props.load(is);
			} catch (IOException ex) {
			} finally {
			        try {
                                        if(is!=null)
                                        is.close();
                                } catch (IOException e) {
                                        e.printStackTrace();
                                }
			}
		}
		return props;
	}
}
分享到:
评论

相关推荐

    FastDFS需要的配置需要软件和配置说明

    3. **配置FastDFS**:修改`/etc/fdfs`目录下的`tracker.conf`和`storage.conf`文件。`tracker.conf`是追踪服务器的配置,包含服务器的IP、端口等信息;`storage.conf`则是存储服务器的配置,包括存储路径、组名、...

    FastDFSClient C#源码

    通过学习和理解FastDFSClient C#源码,开发者不仅可以深入理解FastDFS的工作原理,还能掌握如何在.NET环境中高效地使用分布式文件系统,为大型网站、企业应用等提供稳定可靠的文件存储解决方案。同时,源码分析有助...

    FastDFS Storage端配置文件详解及启动注意事项

    3. **配置文件正确性**:检查`storage.conf`中所有配置项无误,尤其是服务器IP和端口。 4. **启动顺序**:先启动Tracker,再启动Storage,以保证Tracker能够发现Storage。 5. **监控与调试**:启动后,观察日志文件...

    FastDFS集群配置文件

    集群的搭建和配置涉及到多个文件,包括`mod_fastdfs.conf`、`keepalived.conf`和`nginx.conf`。 `mod_fastdfs.conf`是FastDFS的模块配置文件,它定义了FastDFS与Nginx之间的交互方式。其中,`group1--group2`表示...

    FastDFS所有安装配置文件

    1. **client.conf**:这是FastDFS客户端的配置文件,主要用于设置客户端连接到Tracker Server的参数,如服务器地址、端口、超时时间、重试次数等。客户端通过这个配置文件与FastDFS系统进行交互,上传或下载文件。 ...

    nginx+fastDFS(单机版本,系统:CentOS)

    根据FastDFS生成的文件路径,通过API获取文件流并保存到本地,或者直接返回文件流供用户下载。 **五、监控与优化** 在实际使用过程中,需要注意监控FastDFS和Nginx的运行状态,及时处理异常。可以通过日志分析、...

    FastDFS集群与Http访问部署详解配置文件.rar

    2. **负载均衡**:Nginx可以通过配置实现负载均衡,将请求分发到不同的Storage节点,平衡服务器压力。 3. **故障转移**:当某台服务器故障时,FastDFS集群能够自动进行数据迁移,保证服务的连续性。 4. **监控与...

    FastDFS_配置文件详解

    《FastDFS配置文件详解》 FastDFS是一款开源的高性能、轻量级的分布式文件系统,主要解决大容量存储和负载均衡的问题。配置文件是FastDFS系统运行的核心,它定义了系统的运行方式和行为。本文将深入解析FastDFS的...

    fastdfs的安装手册说明+配置文件+fastdfs安装包+nginx安装包

    1. 通过FastDFS客户端上传文件,确保文件能正确存储在Storage上。 2. 访问Nginx服务器,尝试通过FastDFS生成的URL下载文件,验证Nginx与FastDFS的集成。 六、维护与优化 1. 监控FastDFS服务状态,定期检查日志文件...

    fastDFS源码

    Volume的大小可以通过配置文件动态调整,便于扩展存储空间。 3. File ID(FID):FastDFS为每个文件生成一个唯一的FID,由Volume ID、Group ID和文件名三部分组成,便于文件的定位和访问。 四、FastDFS部署与配置 ...

    Redhat下Fastdfs安装配置及nginx模块安装配置文档.pdf

    如果遇到找不到libevent库的错误,可以通过`ldd`命令检查依赖库,然后在`/etc/ld.so.conf.d/`下创建配置文件,指定libevent库的路径,并运行`ldconfig`命令更新链接器配置。 5. 配置Storage Server: 修改`%...

    fastdfs-1.2.zip

    标题中的"fastdfs-1.2.zip"表明这是一个FastDFS的特定版本——1.2版的压缩包,通常包含了该版本的源码、编译好的二进制文件、配置文件以及相关的文档资料。这个压缩包包含了两个主要的文件:`fastdfs-1.2.jar`和`...

    FastDFS配置文件详解

    理解并正确配置这三个文件对于成功部署和运行FastDFS至关重要。每个参数都需要根据实际环境进行调整,例如,服务器的硬件资源、网络状况以及应用需求都会影响这些参数的选择。在实际应用中,还需要关注其他方面,如...

    fastdfs-java源码

    开发者只需在`pom.xml`文件中添加对应的依赖配置,即可轻松引入FastDFS客户端库,简化项目的构建过程。 总结,FastDFS Java源码的解析涉及了连接管理、请求响应处理、协议解析、文件操作等多个层面,深入理解这些...

    FastDFS错误

    3. **更改端口号**:在Storage节点的配置文件中修改端口号。 4. **重启Storage服务**:配置修改完成后,重启Storage服务。 ##### 2.5 删除Data目录并重新启动 **操作步骤**: 1. **停止所有FastDFS服务**:使用...

    fastDFS整合nginx主要配置文件/etc/fdfs

    在IT领域,尤其是在服务器部署和文件分发场景中,FastDFS和Nginx的整合是一个常见的解决方案。FastDFS是一款开源的高性能、轻量级的分布式...总的来说,FastDFS和Nginx的整合能有效提高文件分发的效率和系统的稳定性。

    虚拟机fastdfs配置静态ip

    为了确保虚拟机能够正常连接到外部网络,并且方便管理和配置FastDFS,建议采用桥接模式(Bridged Networking)。这样虚拟机将会获得一个与主机同网段的IP地址,便于后续操作。 ##### 2.3 安装Linux系统 下载CentOS ...

    FastDFSClient配置文件

    fastDFS配置文件,主要修改其中的Service就可以了,其他可默认。

    fastdfs在ubuntu上的部署配置

    在本文中,我们将详细探讨如何在Ubuntu系统上部署配置FastDFS,这是一个开源的分布式文件系统。FastDFS设计用于解决大容量存储和负载...在整个过程中,注意文件路径、端口和配置文件的正确性,以确保系统能正常运行。

Global site tag (gtag.js) - Google Analytics