1. 程式人生 > >solr分布式索引【實戰一、分片配置讀取:工具類configUtil.java,讀取配置代碼片段,配置實例】

solr分布式索引【實戰一、分片配置讀取:工具類configUtil.java,讀取配置代碼片段,配置實例】

hset fileinput string lose eas hash hashset mod bject

 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,讀取配置代碼片段,配置實例】