如何使用原始spring-data-solr在嵌入式solr上配置多个内核

时间:2017-05-17 07:36:57

标签: spring configuration multicore spring-data-solr

我想在Solr 5.5.0嵌入式服务器环境中添加另一个核心。 "在我的世界"我创建了一个嵌入式服务器,让spring-data加载核心配置。但是根据我的解决方案,似乎所有数据都进入了默认核心"集合1"。到目前为止,我无法在spring-boot旁边找到一个例子。但这不是一种选择。

到目前为止,我的配置如下:

@Import({
    AppctxSolrEmbedded.class,
    AppctxSolrHttp.class
})
@EnableSolrRepositories(value = "de.my.application.*.repository", multicoreSupport = true)
@Configuration
public class AppctxSolr {

    public @Bean SolrTemplate solrTemplate(
            @Named("solrClient") SolrClient solrClient) {
        return new SolrTemplate(solrClient, "collection1");
    }

    public @Bean SolrTemplate operatorSolrTemplate(
            @Named("solrClient") SolrClient solrClient) {
        return new SolrTemplate(solrClient, "operator1");
    }
}

@Dev @Qual @RemoteDev
@Configuration
public class AppctxSolrEmbedded {

    @Bean
    public EmbeddedSolrServerFactoryBean solrClient(
            @Value("${solr.server}") String solrHome) {
        EmbeddedSolrServerFactoryBean factory = new EmbeddedSolrServerFactoryBean();
        factory.setSolrHome(solrHome);
        return factory;
    }
}

@Prod
@Configuration
public class AppctxSolrHttp {

    @Bean
    public HttpSolrClientFactoryBean solrClient(
            @Value("${solr.server}") String baseURL) {
        HttpSolrClientFactoryBean factory = new HttpSolrClientFactoryBean();
        factory.setUrl(baseURL);
        return factory;
    }
}

1 个答案:

答案 0 :(得分:2)

我将如何做到这一点

@Configuration
@EnableSolrRepositories(multicoreSupport = true)
public class MulticoreSolrConfiguration {

  @Autowired
  private SolrClient solrClient;

  @Bean
  public SolrOperations solrTemplate() throws ParserConfigurationException, SAXException, IOException {
    return new SolrTemplate(this.solrClient);
  }

  @Bean
  public MulticoreSolrClientFactory multicoreSolrClientFactory()
      throws ParserConfigurationException, SAXException, IOException {
    return new MulticoreSolrClientFactory(this.solrClient, "operator1", "collection1");
  }

  @Bean(name = "operatorSolrClient")
  public SolrClient operatorSolrClient() throws IOException, SAXException, ParserConfigurationException {
    return this.multicoreSolrClientFactory().getSolrClient("operator1");
  }

  @Bean(name = "operatorSolrTemplate")
  public SolrTemplate operatorSolrTemplate() throws IOException, SAXException, ParserConfigurationException {
    return new SolrTemplate(this.operatorSolrClient());
  }
}

  @Bean(name = "collectionSolrClient")
  public SolrClient collectionSolrClient() throws IOException, SAXException, ParserConfigurationException {
    return this.multicoreSolrClientFactory().getSolrClient("collection1");
  }

  @Bean(name = "collectionSolrTemplate")
  public SolrTemplate collectionSolrTemplate() throws IOException, SAXException, ParserConfigurationException {
    return new SolrTemplate(this.collectionSolrClient());
  }
}

然后代替AppctxSolrEmbedded和AppctxSolrHttp,你可以做这样的事情

@Configuration
class SolrConfiguration {

  private final SolrProperties solrProperties; // Has details about solr host, port, directory .....

  @Autowired
  public SolrConfiguration(final SolrProperties solrProperties) {
    this.solrProperties = solrProperties;
  }

  @Bean
  SolrClient solrClient() {
    final SolrClient solrClient;
    if (this.solrProperties.isEmbedded()) {
      solrClient = createEmbeddedSolrClient();
    } else {
      solrClient = createStandaloneSolrClient();
    }

    return solrClient;
  }

  private SolrClient createEmbeddedSolrClient() {
    final String solrConfigurationFolder = this.solrProperties.getSolr().getHome();

    final EmbeddedSolrServerFactoryBean factoryBean = new EmbeddedSolrServerFactoryBean();
    factoryBean.setSolrHome(solrConfigurationFolder);

    return factoryBean.getSolrClient();
  }

  private SolrClient createStandaloneSolrClient() {
    final String solrUrl = this.solrProperties.getHost();

    return new HttpSolrClient(solrUrl);
  }
}  

正如您所看到的,我正在基于某些属性集创建embeddedsolrclient或standaloneclient。您可以根据配置文件(自动装配环境和检查配置文件)将其更改为

相关问题