作者 申海龙

修改 代码中的reids 写到配置文件中

package com.agent.util;
import com.framework.util.PropertiesLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.InputStream;
import java.util.Properties;
/**
* @Auther: shenhl
* @Date: 2019/3/6 15:22
*/
public class RedisUtils {
private static final Logger logger = LoggerFactory.getLogger(RedisUtils.class);
private static final String CONFIG_FILE = "/config.properties";
/**
* 配置文件properties获取value
* @param key
* @param propertyFile 注意classpath下文件以"/"开始
* @return
*/
public static String get(String key){
Properties properties = loadProperty("/redis.properties");
return (String) properties.get(key);
}
/**
* load properties文件
* @param propertyFile
* @return
*/
public static Properties loadProperty(String propertyFile){
InputStream in=PropertiesLoader.class.getResourceAsStream(propertyFile);
Properties properties = new Properties();
try{
properties.load(in);
}catch(Exception e){
logger.error("解析文件失败:文件名={}", propertyFile, e);
}
return properties;
}
public static String getConfig(String key) {
Properties properties = PropertiesLoader.loadProperty(CONFIG_FILE);
return properties.getProperty(key);
}
public static String get(String key,String propertyFile){
Properties properties = loadProperty(propertyFile);
return (String) properties.get(key);
}
}
package com.agent.xml;
import com.framework.util.PropertiesLoader;
import org.apache.commons.lang3.StringUtils;
import redis.clients.jedis.Jedis;
... ... @@ -18,7 +19,11 @@ import redis.clients.jedis.Jedis;
class XmlSendTask extends Thread {
// 创建 缓存服务器的地址ip
// private Jedis jedis = new Jedis("10.50.3.71", 6379);
private Jedis jedis = new Jedis("10.50.3.84", 6379);
// private Jedis jedis = new Jedis("10.50.3.84", 6379);
private Jedis jedis = new Jedis(PropertiesLoader.getRedis("host"),
Integer.valueOf(PropertiesLoader.getRedis("port")));
private String xml;
... ...
... ... @@ -21,6 +21,8 @@ public class PropertiesLoader {
private static final String CONFIG_FILE = "/config.properties";
private static final String REDIS_FILE = "/redis.properties";
/**
* 配置文件properties获取value
* @param key
... ... @@ -55,6 +57,11 @@ public class PropertiesLoader {
return properties.getProperty(key);
}
public static String getRedis(String key) {
Properties properties = PropertiesLoader.loadProperty(REDIS_FILE);
return properties.getProperty(key);
}
public static String get(String key,String propertyFile){
Properties properties = loadProperty(propertyFile);
... ...