в Solr SolrResourceLoader#locateSolrHome
сначала пытается JNDI, а затем свойства системы, чтобы найти его параметры. Поскольку свойства -D
распределяются между всеми приложениями в одном экземпляре Tomcat, они не могут использоваться для настройки нескольких экземпляров Solr.
Если по какой-то причине один не может use JNDI (как <Environment name="solr/home" ...>
in XML files), или один экземпляр с несколькими ядрами, то можно было бы обернуть Solr WAR в собственное приложение и использовать контекст сервлета слушателя (временно) изменить свойства системы в то время как начиная.
Это, безусловно, взломанный и полагается на Tomcat, не запуская приложения параллельно, а на Solr только чтение свойств системы при запуске. Я тестировал это, чтобы работать хорошо, но все же это, вероятно, подходит только для тестирования.
Опять же, это в первую очередь нуждается в обертывании Solr в свое приложение. Затем создайте папку /some/config
с подпапкой для каждого экземпляра, соответствующей ее названию контекста. В каждой подпапке создать custom.properties
файл:
# This file /some/config/[servlet-context-name]/custom.properties is used
# if Tomcat is started with:
# -Dcustom.config.dir=/some/config
# ...and then (temporarily) overwrites any system properties specified below:
solr.solr.home=/some/other/folder/conf
solr.data.dir=/some/other/folder/data
Чтобы объединить свойства системы, основанные на некоторых ключевых значений в файле свойств:
/**
* Tries to find the given file and merges its properties into the existing
* system properties.
*
* @param configFile
* full path of a property file
* @return {@code true} if the file was found and merged; {@code false}
* otherwise
*/
private boolean mergeSystemProperties(final String configFile) {
try (final FileInputStream is = new FileInputStream(configFile)) {
final Properties custom = new Properties();
custom.load(is);
for (final Map.Entry<Object, Object> prop : custom.entrySet()) {
LOG.info("Setting {}={}", prop.getKey(), prop.getValue());
System.setProperty((String)prop.getKey(), (String)prop.getValue());
}
return true;
} catch (final FileNotFoundException e) {
LOG.info("Could not find custom properties: {}", configFile);
} catch (final IOException e) {
LOG.error("Failed to read custom properties: " + configFile, e);
}
return false;
}
Это может быть использовано в качестве слушателя:
public class TestConfigContextListener implements ServletContextListener {
private static final Logger LOG = ...
private static final String PROP_DIR = "custom.config.dir";
private static final String FILE_NAME = "custom.properties";
@Override
public void contextInitialized(final ServletContextEvent event) {
final String configDir = System.getProperty(PROP_DIR);
if (configDir == null) {
LOG.info("No value for -D{}; not reading custom config", PROP_DIR);
} else {
LOG.info("Custom config dir: -D{}={}", PROP_DIR, configDir);
final ServletContext context = event.getServletContext();
// Either "" for the root, or "/some-path" otherwise
final String contextPath = context.getContextPath();
if (!contextPath.isEmpty()) {
if (mergeSystemProperties(configDir + File.separator
+ contextPath.substring(1, contextPath.length())
+ File.separator + FILE_NAME)) {
// We found the configuration in a subfolder matching the
// specific contextPath; done.
return;
}
}
// Root context, or no configuration in a subfolder matching the
// specific contextPath; try to load from configDir itself:
mergeSystemProperties(configDir + File.separator + FILE_NAME);
}
}
...
}
... с в web.xml
:
<!--
Tries to read a property file to set/override system properties just before
Solr is initialized, sort of allowing to run multiple instances with
different settings, IF THEY ARE NOT STARTED SIMULTANEOUSLY.
-->
<listener>
<listener-class>net.example.TestConfigContextListener</listener-class>
</listener>
Заключительные примечания: хотя можно было использовать <env-entry>
в web.xml
для «поддельных» записей JNDI, используя ServletContext#setInitParameter
, чтобы изменить те, которые прослушиватели контекста не имеют эффекта. Кроме того, контекст JNDI java:comp/env
доступен только для чтения и поэтому не может быть изменен из кода.Поэтому действительно нужно вернуться к (временному) настройке свойств системы.
Я так не думаю, что solr - это webapp, вам нужно запустить их на разных портах, возможно. Вы могли бы показать свой web.xml в tomcat? – Mysterion
@Mysterion В моем веб-файле Tomcat нет никакого отношения к обеим версиям Solr. – SaidbakR