在 Solr 4.7 中添加了一个类 MiniSolrCloudCluster,它实际上在本地“部署”(如果您只想要 ram 或在临时目录上)一个完整的 solr 集群,包括 zookeeper、分片和所有东西,用于您的测试。
您可以在这里找到 jira:https://issues.apache.org/jira/browse/SOLR-5865
我已经成功使用它对 solr 分布式组件进行了测试,以 Solr 测试为例,如下:
私有静态 MiniSolrCloudCluster miniCluster;
私有静态 CloudSolrServer cloudSolrServer;
@课前
公共静态无效设置()抛出异常{
miniCluster = new MiniSolrCloudCluster(2, null, new File("src/main/solr/solr.xml"), null, null);
uploadConfigToZk("src/main/solr/content/conf/", "content");
// 覆盖 solrconfig 中的设置包括
System.setProperty("solr.tests.maxBufferedDocs", "100000");
System.setProperty("solr.tests.maxIndexingThreads", "-1");
System.setProperty("solr.tests.ramBufferSizeMB", "100");
// 使用非测试类,因此不需要 RandomizedRunner
System.setProperty("solr.tests.mergeScheduler", "org.apache.lucene.index.ConcurrentMergeScheduler");
System.setProperty("solr.directoryFactory", "solr.RAMDirectoryFactory");
cloudSolrServer = new CloudSolrServer(miniCluster.getZkServer().getZkAddress(), false);
cloudSolrServer.setRequestWriter(new RequestWriter());
cloudSolrServer.setParser(new XMLResponseParser());
cloudSolrServer.setDefaultCollection("内容");
cloudSolrServer.setParallelUpdates(false);
cloudSolrServer.connect();
createCollection(cloudSolrServer, "内容", 2, 1, "内容");
}
protected static void uploadConfigToZk(String configDir, String configName) 抛出异常 {
SolrZkClient zkClient = null;
尝试 {
zkClient = new SolrZkClient(miniCluster.getZkServer().getZkAddress(), 10000, 45000, null);
uploadConfigFileToZk(zkClient, configName, "solrconfig.xml", new File(configDir, "solrconfig.xml"));
uploadConfigFileToZk(zkClient, configName, "schema.xml", new File(configDir, "schema.xml"));
uploadConfigFileToZk(zkClient, configName, "stopwords_en.txt", new File(configDir, "stopwords_en.txt"));
uploadConfigFileToZk(zkClient, configName, "stopwords_it.txt", new File(configDir, "stopwords_it.txt"));
System.out.println(zkClient.getChildren(ZkController.CONFIGS_ZKNODE + "/" + configName, null, true));
} 最后 {
if (zkClient != null)
zkClient.close();
}
}
protected static void uploadConfigFileToZk(SolrZkClient zkClient, String configName, String nameInZk, File file) 抛出异常 {
zkClient.makePath(ZkController.CONFIGS_ZKNODE + "/" + configName + "/" + nameInZk, file, false, true);
}
@下课以后
公共静态无效关闭()抛出异常{
miniCluster.shutdown();
}
protected static NamedList createCollection(CloudSolrServer server, String name, int numShards, int replicationFactor, String configName) throws Exception {
ModifiableSolrParams modParams = new ModifiableSolrParams();
modParams.set(CoreAdminParams.ACTION, CollectionAction.CREATE.name());
modParams.set("名称", 名称);
modParams.set("numShards", numShards);
modParams.set("replicationFactor", replicationFactor);
modParams.set("collection.configName", configName);
QueryRequest 请求 = 新 QueryRequest(modParams);
request.setPath("/admin/collections");
返回 server.request(request);
}
@测试
公共无效测试()抛出异常{
// 你在这里使用 cloudSolrServer 作为一个普通的 solrServer
}