Java 多实例的Spring Boot Solr配置
几次以前,我的任务是在同一个应用程序中使用两个独立的ApacheSolr实例。我试图找到一些信息,但没有结果。本文介绍如何配置对几个独立ApacheSolr的访问,并使用Spring数据通过不同的存储库访问它们 Solr配置 每个Solr都需要有一个独立的配置。例如:Java 多实例的Spring Boot Solr配置,java,apache,spring-boot,solr,Java,Apache,Spring Boot,Solr,几次以前,我的任务是在同一个应用程序中使用两个独立的ApacheSolr实例。我试图找到一些信息,但没有结果。本文介绍如何配置对几个独立ApacheSolr的访问,并使用Spring数据通过不同的存储库访问它们 Solr配置 每个Solr都需要有一个独立的配置。例如: @Configuration @EnableSolrRepositories(basePackages = {"com.project.repository.first"}, solrCli
@Configuration
@EnableSolrRepositories(basePackages = {"com.project.repository.first"},
solrClientRef = "firstSolrClient",
solrTemplateRef = "firstSolrTemplate")
public class FirstSolrConfig {
@Value("${solr.first.url}")
private String solrHost;
@Bean
public SolrClient solrClient() {
return new HttpSolrClient.Builder(solrHost).build();
}
@Bean("firstSolrTemplate")
public SolrTemplate solrTemplate() {
return new SolrTemplate(this::solrClient);
}
}
-
来自第一个配置的SolrClient Bean必须具有“SolrClient”名称!否则,您将无法提升Spring上下文。
您需要将存储库放在不同的包中
这是我为这个案子找到的唯一有效方法。今天我遇到了同样的问题。看起来,
solrClientRef=“secondSolrClient”
不再工作了。在不深入细节的情况下,我可以告诉您,此功能已被删除或可能已随此一起移动。现在我还没能想出另一个解决办法,所以我坚持下去
必须至少存在一个名为solrClient
的Bean
以下是一些代码,说明了我的工作解决方案:
@Configuration
@EnableSolrRepositories(solrTemplateRef = "firstSolrTemplate")
@EntityScan
@ComponentScan
public class FirstSolrConfiguration {
@Bean("firstSolrClient")
public SolrClient firstSolrClient(@Qualifier("firstSolrCredentialsProvider") final CredentialsProvider credentialsProvider) {
//
}
@Bean
@ConditionalOnMissingBean(name = "solrClient")
public SolrClient solrClient(@Qualifier("firstSolrCredentialsProvider") final CredentialsProvider credentialsProvider) {
//
}
@Bean("firstSolrTemplate")
public SolrTemplate solrTemplate() {
//
}
}
我还有第二个连接的第二个配置。这在不同的软件包中有效吗?
@Configuration
@EnableSolrRepositories(solrTemplateRef = "firstSolrTemplate")
@EntityScan
@ComponentScan
public class FirstSolrConfiguration {
@Bean("firstSolrClient")
public SolrClient firstSolrClient(@Qualifier("firstSolrCredentialsProvider") final CredentialsProvider credentialsProvider) {
//
}
@Bean
@ConditionalOnMissingBean(name = "solrClient")
public SolrClient solrClient(@Qualifier("firstSolrCredentialsProvider") final CredentialsProvider credentialsProvider) {
//
}
@Bean("firstSolrTemplate")
public SolrTemplate solrTemplate() {
//
}
}