全局配置实践"/>
scala项目的全局配置实践
- 定义一个 object 放一些常量名,相当于各种属性的key值
object MktEnvironmentContants {// 系统环境变量val KEY_UAP_ZK_SERVER = "uap.zk.server"val KEY_UAP_SERVICE_CONSOLE_PORT = "uap.service.console.port"val KEY_UAP_SERVICE_ANALYZE_PORT = "uap.service.analyze.port"val KEY_UAP_SERVICE_PREDICT_PORT = "uap.service.predict.port"}
- 通过启动命令传递参数并通过 System.getProperty 获取
-Dspring.profiles.active=dev
- 根据启动命令的类型加载不同的配置文件,并将加载到的内存放入系统配置里面。整个过程中都是java内置的代码
object PropertiesUtil extends RichLogging {/*** 加载properties文件,以classpath为根目录** @param fileName 文件名* @return*/def loadProperties(fileName: String): Properties = {val inputStream: InputStream = Thread.currentThread().getContextClassLoader.getResourceAsStream(fileName)val properties = new Properties()properties.load(inputStream)properties}/*** 加载属性到系统属性** @param fileName 文件名*/def loadProperteisToSystem(fileName: String): Unit = {val envProperties = PropertiesUtil.loadProperties(fileName)val envNames = envProperties.propertyNames()while (envNames.hasMoreElements) {val key = envNames.nextElement.asInstanceOf[String]System.getProperties.setProperty(key, envProperties.getProperty(key))}}/*** 显示系统属性*/def showSystemProperties(): Unit = {val properties = System.getPropertiesval ele = properties.propertyNameswhile (ele.hasMoreElements) {val key = ele.nextElement.asInstanceOf[String]logger.info(s"System Properties, key: $key, value: ${properties.getProperty(key)}")}}
}
- 使用常量定义的key值去系统中取配置
System.getProperty(MktEnvironmentContants.KEY_UAP_PROFILE_ACTIVE)
更多推荐
scala项目的全局配置实践
发布评论