我是靠谱客的博主 兴奋茉莉,最近开发中收集的这篇文章主要介绍读取redis配置文件,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述




import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;




/**
 * 读取redis配置文件
 * 
 * 
 * */
public class RedisConfig {

public static List<RedisCluster> redisGroupList = null;




//构造函数
public RedisConfig()
{

}




//获取所有clusterGroup组的键值对
public static List<RedisCluster> getRedisCluster(InputStream xmlInputStream) throws Exception{
//读取xml文件
Document document=readXmlFile(xmlInputStream);
//获得clusterGroup节点的key 和 list
if(redisGroupList == null)
{
redisGroupList=getRedisGroupList(document);
}

return redisGroupList;
}




//读取redisConfig配置文件
private static Document readXmlFile(InputStream xmlInputStream) throws Exception{
    //创建读入对象
    SAXReader reader = new SAXReader();
    //创建document实例
    Document doc=null;
    try {
         reader.setEncoding("UTF-8");
         doc = reader.read(xmlInputStream);
}
    catch (DocumentException e) {
    e.printStackTrace();
System.out.println("读取xml配置文件出现异常 ");
throw e;

}
    catch (Exception e) {
    e.printStackTrace();
System.out.println(e.getMessage());
throw e;
}
    finally{
    if(xmlInputStream!=null){
    try {
    xmlInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
    }
    }
return doc;


}


//读取xml节点,返回节点为redisGroup的Map
private  static List<RedisCluster> getRedisGroupList(Document document) throws Exception{
List<RedisCluster> redisList=new ArrayList<RedisCluster>();
try{
//获得根节点
Element root=document.getRootElement();
//获得根节点下所有子节点clusterGroup的list
List<Element> itemsList=root.selectNodes("./clusterGroup");
if(itemsList!=null && itemsList.size()==1)
{
//获得节点Items
Element items=itemsList.get(0);
//获取server列表
List itemList=items.elements();
//获得service节点的值
redisList = getItemList(itemList);
}
else{
System.out.println("clusterGroup node only one");
throw new Exception("clusterGroup node only one");

}
}
catch(Exception e){
e.printStackTrace();
throw e;
}
return redisList;
}




//获得所有Item下节点的redis服务节点
private static List<RedisCluster> getItemList(List itemList){

List<RedisCluster> redisClusterList = new ArrayList<RedisCluster>();
for(int i=0;i<itemList.size();i++){
//获得节点server
Element item=(Element)itemList.get(i);
String masterHost =  item.attribute("masterHost").getText().trim();
String slaveHost =  item.attribute("slaveHost").getText().trim();
String port = item.attribute("port").getText().trim();
String masterName = item.attribute("masterName").getText().trim();
String password=null;
if(item.attribute("password")!=null){
password = item.attribute("password").getText().trim();
}
RedisCluster redisCluster =new RedisCluster();
redisCluster.setMasterHost(masterHost);
redisCluster.setSlaveHost(slaveHost);
redisCluster.setPort(port);
redisCluster.setMasterName(masterName);
if(password != null){
   redisCluster.setPassword(password);
}
redisClusterList.add(redisCluster);
   }
   return redisClusterList;

}




}



XML

<?xml version="1.0" encoding="UTF-8"?>
<redisCluster>
<!--clusterGroup-->
<clusterGroup>
<server masterHost="192.168.1.101" slaveHost="192.168.1.101"
port="26379"
masterName="master"></server>
<server masterHost="192.168.1.101" slaveHost="192.168.1.101"
port="26479"
masterName="master"></server>
<server masterHost="192.168.1.101" slaveHost="192.168.1.101"
port="26579"
masterName="master"></server>
</clusterGroup>
</redisCluster>

调用:

//初始化redis集群
ClassPathResource resource = new ClassPathResource("conf/redisClusterConfig.xml");
InputStream xmlInputStream = resource.getInputStream(); 
if(xmlInputStream!=null){
RedisDistributedFactory.init(xmlInputStream);
}

最后

以上就是兴奋茉莉为你收集整理的读取redis配置文件的全部内容,希望文章能够帮你解决读取redis配置文件所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(49)

评论列表共有 0 条评论

立即
投稿
返回
顶部