solr分布式索引实战分片配置读取:工具类configUtil.java,读取配置代码片段,配置实例
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了solr分布式索引实战分片配置读取:工具类configUtil.java,读取配置代码片段,配置实例相关的知识,希望对你有一定的参考价值。
1 private static Properties prop = new Properties(); 2 3 private static String confFilePath = "conf" + File.separator + "config.properties";// 配置文件目录 4 static { 5 // 加载properties 6 InputStream is = null; 7 InputStreamReader isr = null; 8 try { 9 File f = new File(confFilePath); 10 if (f.exists()) { 11 is = new BufferedInputStream(new FileInputStream(f)); 12 isr = new InputStreamReader(is, "utf-8"); 13 prop.load(isr); 14 is.close(); 15 is = null; 16 isr.close(); 17 isr = null; 18 } else { 19 /** 修改为资源文件读取方式,方便以后工程打包 */ 20 is = ConfigUtil.class.getClassLoader().getResourceAsStream("config.properties"); 21 if (is != null) { 22 isr = new InputStreamReader(is, "utf-8"); 23 prop.load(isr); 24 is.close(); 25 is = null; 26 isr.close(); 27 isr = null; 28 } else { 29 System.err.println("严重警告:项目的配置文件“config.properties”缺失了,该文件应在src目录下或者工程目录的conf文件夹中," 30 + "继续运行将可能因数据库等配置参数缺失而导致重大异常!"); 31 32 // System.exit(-1); 33 } 34 } 35 } catch (Exception e) { 36 e.printStackTrace(); 37 } finally { 38 if (is != null) { 39 try { 40 is.close(); 41 } catch (IOException e) { 42 e.printStackTrace(); 43 } 44 is = null; 45 } 46 } 47 48 }
读取配置代码片段:
/**获取所有的分片的cloud配置序号 * @return */ public static Set<String> getAllCloudSplitModeConfigIndex(){ Set<String> result = new HashSet<String>(); Properties props = ConfigUtil.getProperties(); Set<Object> allKeys = props.keySet();
//遍历配置文件,读取所有键值对。 for(Object s:allKeys){ String key = (String)s; if(key.startsWith("solr_mode_")){ String value = StringUtil.trim(ConfigUtil.getProp(key)); //如果是分布式模式
if("cloud".equalsIgnoreCase(value)){ String configName = key.replaceFirst("solr_mode_", ""); //如果是按照时间分片
if("true".equalsIgnoreCase(ConfigUtil.getProp("split_mode_" + configName))){ result.add(configName); } } } } return result; }
配置文件实例:
######## solr ######
defaultConfigName = 1
#####solr1########
solr_url_1 = http://***********:8080/solr (单机)
zkHost_1 = 192.168.*.*:2181,192.168.*.*:2181,192.168.*.*:2181(zookeeper地址)
solr_mode_1 = cloud (分布式模式)
coreName_1 = collection1 (内核名称)
zkClientTimeout_1 = 200000
zkConnectTimeout_1 = 10000
connectionTimeout_1 = 100000
idField_1 = guidSolr (uniqueKey)
split_mode_1 = true (分片模式开启)
splitTimeField_1 = publishTime (分片字段)
#####solr2#######
solr_url_2 = http://192.168.*.*:8080/solr(单机)
zkHost_2 = 192.168.*.*:2181,192.168.*.*:2181,192.168.*.*:2181
solr_mode_2 = cloud
coreName_2 = collection2
zkClientTimeout_2 = 200000
zkConnectTimeout_2 = 10000
connectionTimeout_2 = 100000
idField_2 = guidSolr
split_mode_2 = false(分片模式关闭)
以上是关于solr分布式索引实战分片配置读取:工具类configUtil.java,读取配置代码片段,配置实例的主要内容,如果未能解决你的问题,请参考以下文章