【问题标题】:Spring Boot Solr configuration for multiple Instances多个实例的 Spring Boot Solr 配置
【发布时间】:2019-01-29 13:37:43
【问题描述】:

几次前,我的任务是在同一个应用程序中使用两个独立的 Apache Solr 实例。我试图找到一些信息,但无济于事。本文是关于如何配置对几个独立的 Apache Solr 的访问,并使用 Spring Data 通过不同的存储库访问它们。

Solr 配置

您需要为每个 Solr 进行独立配置。例如:

    @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);
        }

    }

-

@Configuration
@EnableSolrRepositories(basePackages = {"com.project.repository.second"},
        solrClientRef = "secondSolrClient",
        solrTemplateRef = "secondSolrTemplate")
public class SecondSolrConfig {

    @Value("${solr.second.url}")
    private String solrHost;

    @Bean("secondSolrClient")
    public SolrClient solrClient() {
        return new HttpSolrClient.Builder(solrHost).build();
    }

    @Bean("secondSolrTemplate")
    public SolrTemplate solrTemplate() {
        return new SolrTemplate(this::solrClient);
    }

}

来自第一个配置的 SolrClient Bean 必须具有“solrClient”名称!否则,您将无法引发 Spring Context。 您需要将存储库放在不同的包中。

这是我为这个案例找到的唯一可行的方法。

【问题讨论】:

  • 这在不同的包中有效吗?

标签: java apache spring-boot solr


【解决方案1】:

今天我遇到了同样的问题。看起来solrClientRef = "secondSolrClient" 不再起作用了。在不深入细节的情况下,我可以告诉您,此功能已被删除或可能与此 commit 一起移动。目前我无法想出另一个解决方案,所以我坚持

必须至少存在一个名为 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() {
        //
    }

}

我还有第二个配置用于我的第二个连接。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-10
    • 1970-01-01
    • 2018-02-26
    • 1970-01-01
    • 2018-08-01
    • 2019-07-27
    • 1970-01-01
    • 2017-04-27
    相关资源
    最近更新 更多