2017-05-17 9 views
0

Je voudrais ajouter un autre noyau à mon environnement de serveur embarqué Solr 5.5.0. "Dans mon monde" je crée un serveur embarqué et laisse les données printanières charger les configurations de base. Mais avec ma solution, il semble que toutes les données vont dans le cœur par défaut "collection1". Jusqu'à présent, je ne pouvais pas trouver un exemple à côté de spring-boot. Mais ce n'est pas une option.Comment configurer plusieurs coeurs sur solr intégré avec spring-data-solr brut

Ma configuration ressemble à ceci jusqu'à présent:

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

Répondre

2

Voici comment je le ferais

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

Et puis au lieu de AppctxSolrEmbedded et AppctxSolrHttp, vous pouvez faire quelque chose comme ça

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

Comme vous pouvez le voir, je crée embeddedsolrclient ou standaloneclient basé sur certains ensemble de propriétés. Vous pouvez le modifier en fonction du profil (Autowire Environment et vérifier le profil)

+0

Merci, cela a résolu mon problème. –