ITPub博客

首页 > 数据库 > 数据库开发技术 > Java应用程序读取外部配置文件

Java应用程序读取外部配置文件

原创 数据库开发技术 作者:HelloTech技术派 时间:2018-09-11 18:00:00 0 删除 编辑

背景

  • 当我们在服务器上安装一些软件服务时,如 MySQL、Kafka、ES 等,在启动服务的时候,脚本会读取默认路径下的配置文件,如果配置文件没有放到默认的路径下,我们也可以通过参数的方式指定,通过这个配置文件,可以灵活方便的开始某些功能或调整某些参数,不用重新对源码进行修改、编译和发布了。

  • 所以,我们工作当中有时希望可以把配置文件放在程序外,这样就可以做到配置与业务分离,在项目中使用的 properties,这里就说一下如何用 properties 配置文件,默认加载 classpath 下的文件,如果通过参数指定了文件路径就可以使用外部配置了。

  • 最近在使用 Netty 实现一个 TCP Server,主要用来接收媒体数据,然后对其进行相应的处理;同时,在这个项目中也嵌入了 HTTP 服务,使用 Jetty 实现了 HTTP Servlet,用来实现一些 Web 的交互控制。在这个项目中,服务的端口和一些开关设置使用了配置文件。

举例

在介绍完背景后,来看看是怎么来实现这样一个应用程序,此程序仅使用了 Netty 和 Jetty ,没有使用 Spring 相关的框架。本篇主要介绍如何实现读取外部配置文件

依赖

  1. <dependency>

  2.    <groupId>commons-configuration</groupId>

  3.    <artifactId>commons-configuration</artifactId>

  4.    <version>1.10</version>

  5. </dependency>

实现

lombok, 通过注解引入 log

单例模式

  1. package com.xxx.config;

  2. import lombok.extern.log4j.Log4j2;

  3. import org.apache.commons.configuration.ConfigurationException;

  4. import org.apache.commons.configuration.PropertiesConfiguration;

  5. import org.apache.commons.configuration.SystemConfiguration;

  6. import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;

  7. import org.apache.commons.lang3.StringUtils;

  8. import java.io.File;

  9. @Log4j2

  10. public class ConfigStrategy {

  11.    private boolean autoReload;

  12.    private static final String BASE_PATH;

  13.    private static final String GLOBAL_CONFIG_PATH = "global.config.path";

  14.    private static final String fileName="conf.properties";

  15.    private static volatile PropertiesConfiguration prop =null;

  16.    static{

  17.        log.info("start init configuration strategy");

  18.        SystemConfiguration sysConfig = new SystemConfiguration();

  19.        String globalPath = sysConfig.getString(GLOBAL_CONFIG_PATH);

  20.        log.info("configuration globalPath {} ", globalPath);

  21.        // 默认加载classpath下面的文件

  22.        if(StringUtils.isBlank(globalPath)){

  23.            globalPath = Thread.currentThread().getContextClassLoader().getResource(fileName).getFile();

  24.        }

  25.        BASE_PATH=globalPath;

  26.    }

  27.    public ConfigStrategy(boolean autoReload) throws ConfigurationException {

  28.        this.autoReload = autoReload;

  29.        loadConfig();

  30.    }

  31.    public void loadConfig() throws ConfigurationException {

  32.        if (null == prop){

  33.            prop = new PropertiesConfiguration();

  34.        }

  35.        File file = new File(BASE_PATH);

  36.        prop.setFile(file);

  37.        prop.setAutoSave(false);

  38.        // 重载策略,5秒钟监视文件变化

  39.        if(autoReload){

  40.            prop.setReloadingStrategy(new FileChangedReloadingStrategy());

  41.        }

  42.        prop.load();

  43.    }

  44.    /**

  45.     * 获取字符串类型的配置项

  46.     *

  47.     * @param key

  48.     * @return value

  49.     */

  50.    public  String getProperty(String key) {

  51.        return prop.getString(key);

  52.    }

  53.    /**

  54.     * 获取整数类型的配置项

  55.     *

  56.     * @param key

  57.     * @return value

  58.     */

  59.    public  Integer getInteger(String key) {

  60.        String value = getProperty(key);

  61.        return Integer.valueOf(value);

  62.    }

  63.    /**

  64.     * 获取布尔类型的配置项

  65.     *

  66.     * @param key

  67.     * @return value

  68.     */

  69.    public  Boolean getBoolean(String key) {

  70.        String value = getProperty(key);

  71.        return Boolean.valueOf(value);

  72.    }

  73.    /**

  74.     * 获取Long类型的配置项

  75.     *

  76.     * @param key

  77.     * @return

  78.     */

  79.    public  Long getLong(String key) {

  80.        String value = getProperty(key);

  81.        return Long.valueOf(value);

  82.    }

  83.    private static class SingletonHelp {

  84.        static ConfigStrategy instance;

  85.        static {

  86.            try {

  87.                instance = new ConfigStrategy(true);

  88.            } catch (ConfigurationException e) {

  89.                log.error("configuration strategy init error ", e);

  90.            }

  91.        }

  92.    }

  93.    public static ConfigStrategy build(){

  94.        return SingletonHelp.instance;

  95.    }

  96. }

项目启动命令

-Dglobal.config.path 指定配置文件的位置

注意 -Dglobal.config.path=./conf.properties 的顺序,在放到 xxxx.jar 前,否则读取不到配置文件。

  1. java -jar -Dglobal.config.path=./conf.properties xxxx.jar


原文作者:HelloTech技术派;如有侵权,请联系管理员删除。

请登录后发表评论 登录
全部评论

注册时间:2017-12-05

  • 博文量
    181
  • 访问量
    73736