Java项目环境变量管理

两种方式概述

不同模块之间环境变量管理的2种方式:

  1. linux系统环境变量
  2. 配置文件

方法1:Linux系统环境变量

在系统启动之后,执行如下命令:

. Profile.system

. Profile.system文件如下:

#!/bin/sh
#以下为所有的环境变量
PRODUCT_NAME="AAA"
PRODUCT_COMMON="common"
PRODUCT_VERSION=@VERSION@
PRODUCT_TOP=@PRODUCT_TOP@
LOCAL_ADDRESS=@HOST_IP@
IGNITE_ADDRESS=$LOCAL_ADDRESS
THIRD_SOFTWARE=@THIRD_SOFTWARE@
 
export PRODUCT_NAME …… #上述所有环境变量

# 去各个模块下执行各个模块自己特有的环境变量
if [ -f ${PRODUCT_TOP}/conf/${PRODUCT_COMMON}/profile.module1 ]; then
    . ${PORTAL_TOP}/conf/${HPC_COMMON}/profile.module1
fi
 
if [ -f ${PRODUCT_TOP}/conf/sso/profile.module2 ]; then
    . ${PRODUCT_TOP}/conf/sso/profile.module2
fi

# 引用其他软件
if [ "third_software" = "$THIRD_SOFTWARE" ]; then
    . /etc/third.profile
fi

上述所有执行了. profile操作而引入的环境变量可以被系统中所有模块共享使用。

方法2:配置文件管理

优点:各个模块自己加载自己的环境变量
缺点:配置文件较多

前提:
首先所有的模块会共同依赖于一个公共的jar包:common.jar

这个jar包中存在一个管理环境变量的类:

public class ExternalConfig {
    /**
     * 对外提供单例
     */
    public static final ExternalConfig INSTANCE = new ExternalConfig();
 
    /**
     * The constant ADMIN_NAME. 这种类型的环境变量使用方式:ExternalConfig.ADMIN_NAME
     * 这种类型是做区分:所有模块都要用到的通用常量用这种形式去承载。
     */
    public static final String ADMIN_NAME = System.getProperty("user.name");
 
    /**
     * active
     */
    public static final String ACTIVE_JOB_CACHE = "active";
 
        
          /**
            * The constant SECURITY_MANAGEMENT_CONFIG.
            * 配置文件路径,调用方式:ExternalConfig.……
            *  哪个模块用到了就由哪个模块负责完成初始化,属于该模块的环境变量
            */
          public static String SECURITY_MANAGEMENT_CONFIG;
 
    /**
     * The configuration information in SecurityConfigManagementConf
     * 三员管理功能开关,取值为on/off.
     * 这种类型的是需要以:External.INSTANCE.……去调用的
           * 哪个模块用到了就由哪个模块负责完成初始化
     */
    public String enableSecurityManagement;
 
    // 许许多多的上述三种环境变量
……
 
    private ExternalConfig() {
    }
 
        // 其他方法
        // 定义各种static方法用以完成上述常量的初始化
        // getInstance方法获取单例
 
}

Module1的环境变量初始化代码:

public class Module1EnvironmentPostProcessor implements EnvironmentPostProcessor {
   
    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        InputStream inputStream = null;
        try {
                           // 直接进行初始化
    // module1需要使用到的环境变量初始化
            ExternalConfig.PARAMETER1 = false;
  // 读取系统环境变量,这种是与方法1的结合,或许系统公共的环境变量。
            String fileTop = environment.getProperty("filepath");
            ExternalConfig.PARAMETER2 = fileTop;
 
// 无参数的环境变量初始化方法
            initConfig();
 
                          // 读取module1的配置文件,进行初始化
            inputStream = new FileInputStream(FileUtils.getFile(ExternalConfig.INSTANCE.MODULE1_CONFIG_PATH));
           Properties properties = new Properties();
            properties.load(inputStream);
                          // 在该方法中将配置文件中的环境变量注入ExternalConfig找中
            initCommonEnvironment(environment, properties);
 
        } catch (IOException e) {
           throw new IllegalStateException(e);
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    LOG.error("method postProcessEnvironment IOException ", e);
                }
            }
        }
    }
 
    private void initConfig() {
        ExternalConfig.INSTANCE.FILEA_PATH = FileUtil.fileUrl(ExternalConfig.INSTANCE.PARAMETER2,
            "config.properties");
        ExternalConfig.INSTANCE.FILEB_PATH = FileUtil.fileUrl(ExternalConfig.INSTANCE.PARAMETER2,
            "data_dir.yml");
                 // ……
    }
 
    private void initCommonEnvironment(ConfigurableEnvironment environment, Properties properties) {
                 // 读取配置文件完成初始化
        ExternalConfig.INSTANCE.PORT = getInputNumber(properties.getProperty("product.port"),
            Constant.DEFAULT_PORT);
        ExternalConfig.INSTANCE.B_PORT = ExternalConfig.INSTANCE.PORT + 2;
        ExternalConfig.INSTANCE.IP = properties.getProperty("product.ip");
    }
}

配置文件样例:

#
# SSH port
#
ssh.port=@SSH_PORT@
#
# Default timeout for command execution (milliseconds)
#
command.timeout=30000
#
# IP address of product
#
product.ip=@PRODUCT_IP@
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值