关于Disconf配置文件下载位置的问题

来源:互联网 发布:linux curl post 请求 编辑:程序博客网 时间:2024/06/05 04:29

  前面几篇博客介绍了Disconf安装过程,以及Disconf实现的两种方式,本篇博客就记录一下源码学习过程中,有关配置文件下载位置的问题。因为项目中使用时出现读取Disconf下载的配置文件混乱问题,想找出问题的原因,所以这几天学习了一下源码,想知道配置文件到底是下载到哪里,又是从哪里读取的。


  Disconf既可以管理配置文件,也可以管理配置项,这篇博客只说明配置文件的下载位置和读取位置。涉及到配置文件路径问题的属性有三个,第一个是disconf.user_define_download_dir,第二个是disconf.enable_local_download_dir_in_class_path,第三个是targetDirPath。


disconf.user_define_download_dir:这个属性是用户自定义的下载位置,这个路径是以用项目名称命名的文件夹为根目录,比如我的项目名称为disconf-1,那么这个属性就是以disconf-1这个文件夹为根路径。


disconf.enable_local_download_dir_in_class_path:这个属性是控制是否下载到classpath下,如果为true的话,则下载到classpath下。该属性的默认值就是true,官网也是强烈建议设置为true。


targetDirPath:这个属性是DisconfFile这个Annotation的一个属性,它默认值是空。如果设置它以"/"开头的话,则是以系统全路径为根路径,否则是以classpath为根路径,默认是以classpath为根路径。


  对于配置文件下载路径问题,官网上是这么说的:


下面就介绍一下我测试的结果,测试中是下载redis.properties配置文件:


注解式实现方式


1、targetDirPath设置为"disconf";disconf.user_define_download_dir注释掉,不进行设置;disconf.enable_local_download_dir_in_class_path为true或者false。


  这种情况下,从下面的图片可以看到,classpath的根路径下是没有redis.properties文件的,但是多了一个disconf文件夹,这个文件夹下是有redis.properties文件。其实只要targetDirPath设置了值,就不会下载到classpath根路径下了,只会下载到targetDirPath设置的路径。




2、targetDirPath设置为"disconf";disconf.user_define_download_dir设置为"./config";disconf.enable_local_download_dir_in_class_path为true或者false。


  这样的话,既会下载到targetDirPath设置的路径,也会在disconf-1文件夹下创建一个config文件夹,redis.properties文件也会下载到这个路径。



3、targetDirPath设置为空;disconf.user_define_download_dir注释掉,不进行设置;disconf.enable_local_download_dir_in_class_path为true。


这样设置的话,只会在classpath根路径下下载redis.properties文件。



4、targetDirPath设置为空;disconf.user_define_download_dir设置为"./config";disconf.enable_local_download_dir_in_class_path为false。


这样设置只会在disconf-1/config路径下下载配置文件。



XML式实现方式


  采用XML式实现方式,不会使用DisconfFile的annotation,所以没办法设置targetDirPath这个属性,只能设置disconf.user_define_download_dir和disconf.enable_local_download_dir_in_class_path。经过测试,当disconf.enable_local_download_dir_in_class_path属性设置为false时,不管其他属性设置为什么值,都不管用,因为项目会直接报错无法启动,所以下载不成功。当disconf.enable_local_download_dir_in_class_path设置为true时,会下载到classpath下,经过Spring的配置文件也会在classpath下读取。Spring配置文件在启动时,只能在classpath下读取下载的配置文件,所以只要下载不到classpath下面,就读取不到。


总结


  这是我在学习源码的过程中测试的过程,发现跟官网上解释的配置文件下载的位置有点出入。而且令我感到疑惑的是,disconf-client会默认设置一个./disconf/download目录,不管上面说到的三种属性如何设置,这个目录下总会下载配置文件,所以就对配置文件从哪里加载有些疑惑。研究的不够深入,还需要继续学习,如果博客中有什么不对的,还请过路的大牛指点,不胜感激!


补充:

  再学习源码才发现,原来./disconf/download目录是2.6.36版本默认进去的,也就是说当disconf.user_define_download_dir没有配置值时,disconf-client会默认./disconf/download目录,如果配置了值的话,会使用用户配置的目录。

0 0
原创粉丝点击