作者 申海龙

将代码中的reids 写到配置文件中

  1 +package com.agent.util;
  2 +
  3 +import com.framework.util.PropertiesLoader;
  4 +import org.slf4j.Logger;
  5 +import org.slf4j.LoggerFactory;
  6 +
  7 +import java.io.InputStream;
  8 +import java.util.Properties;
  9 +
  10 +/**
  11 + * @Auther: shenhl
  12 + * @Date: 2019/3/6 15:22
  13 + */
  14 +public class RedisUtils {
  15 +
  16 +
  17 + private static final Logger logger = LoggerFactory.getLogger(RedisUtils.class);
  18 +
  19 + private static final String CONFIG_FILE = "/config.properties";
  20 +
  21 + /**
  22 + * 配置文件properties获取value
  23 + * @param key
  24 + * @param propertyFile 注意classpath下文件以"/"开始
  25 + * @return
  26 + */
  27 + public static String get(String key){
  28 + Properties properties = loadProperty("/redis.properties");
  29 + return (String) properties.get(key);
  30 + }
  31 +
  32 +
  33 + /**
  34 + * load properties文件
  35 + * @param propertyFile
  36 + * @return
  37 + */
  38 + public static Properties loadProperty(String propertyFile){
  39 + InputStream in=PropertiesLoader.class.getResourceAsStream(propertyFile);
  40 + Properties properties = new Properties();
  41 + try{
  42 + properties.load(in);
  43 + }catch(Exception e){
  44 + logger.error("解析文件失败:文件名={}", propertyFile, e);
  45 + }
  46 + return properties;
  47 + }
  48 +
  49 +
  50 + public static String getConfig(String key) {
  51 + Properties properties = PropertiesLoader.loadProperty(CONFIG_FILE);
  52 + return properties.getProperty(key);
  53 + }
  54 +
  55 +
  56 + public static String get(String key,String propertyFile){
  57 + Properties properties = loadProperty(propertyFile);
  58 + return (String) properties.get(key);
  59 + }
  60 +
  61 +
  62 +
  63 +}
  1 +host = 10.50.3.84
  2 +port = 6379