Documentation Home

Client Setup

Once Zookeeper and Solr have been installed and configured, you can instruct Broadleaf to begin using them. This is a simple matter of configuration. Assuming you are using Broadleaf's Demo Site distribution, or some derivitive, you can find the Solr configuration in the core/src/main/java/com/mycompany/core/config/ApplicationSolrConfiguration.java. You will want to replace the class with the following:

@Component
public class ApplicationSolrConfiguration {

    @Value("#{'${solr.url.primary}'.split(',')}")
    protected List<String> primarySolrUrl;

    @Value("#{'${solr.url.reindex}'.split(',')}")
    protected List<String> reindexSolrUrl;

    @Value("${solr.cloud.defaultNumShards}")
    protected int defaultNumShards;

    @Value("${solr.cloud.configName}")
    protected String configName;

    @Bean
    public SolrConfiguration blCatalogSolrConfiguration() throws IllegalStateException {
        return new SolrConfiguration(solrServer(), solrReindexServer(), configName, defaultNumShards);
    }

    @Bean
    protected SearchService blSearchService() {
        return new SolrSearchServiceImpl();
    }

    @Bean
    public org.apache.solr.client.solrj.impl.CloudSolrClient solrServer() {
        CloudSolrClient solrClient = new CloudSolrClient.Builder(primarySolrUrl,empty()).build();

        solrClient.setDefaultCollection("catalog");
        solrClient.setRequestWriter(new RequestWriter());
        return solrClient;
    }

    @Bean
    public org.apache.solr.client.solrj.impl.CloudSolrClient solrReindexServer() {
        CloudSolrClient solrClient = new CloudSolrClient.Builder(reindexSolrUrl,empty()).build();

        solrClient.setDefaultCollection("catalog-reindex");
        solrClient.setRequestWriter(new RequestWriter());
        return solrClient;
    }

}

In core/src/main/resources/common.properties you'll want to make sure you have the following configuration:

...
solr.source.primary=solrServer
solr.source.reindex=solrReindexServer

solr.url.primary=localhost:2181,localhost:2182,localhost:2183
solr.url.reindex=localhost:2181,localhost:2182,localhost:2183

...

And There are a few important things to note about the above configuration. First, notice that the implementation of the SolrServer instances (SolrJ client) is org.apache.solr.client.solrj.impl.CloudSolrClient. Also, notice that the primary and reindex instances have different defaultCollection values, matching the aliases discussed in the section on setting up SolrCloud. Notice that the reindex instance has a requestWriter defined. The reason for this is that the default request writer is an instance of org.apache.solr.client.solrj.impl.BinaryRequestWriter. This does not properly serialize BigDecimal (or BigInteger) values, which Broadleaf uses extensively. The above configuration is an XML request writer that handles those values just fine. The values in the properties file configuration indicate to Spring to substibute the values for the bean names and the URL values. These can be changed on a per environment basis.

Finally, there are a few additional advanced configurations. If you have not already configured the appropriate SolrCloud collections and aliases, as described in the section on Solr Cloud Setup, do not worry. Broadleaf will create the necessary collections and aliases. However, there are a few pieces of data that are required to do this and Broadleaf uses some basic defaults. When creating a collection, Broadleaf needs to know how many shards to create. The default is 2 and leave it as is if continuing Solr Cloud Setup. This value can be changed by overriding a property by adding it to the core/src/main/resources/common.properties:

solr.cloud.defaultNumShards=3

You cannot have more shards than you have Solr instances.

Additionally, in the section on Solr Cloud Setup, we talked about how we upload a Solr configuration directory using the configuration name "blc". If you chose to use a different name, and you want Broadleaf to configure your collections and aliases, you can override that property as well in the core/src/main/resources/common.properties:

solr.cloud.configName=someOtherConfigName

When you build and start Broadleaf now, it will use the Solr configuration above to connect to Zookeeper to get the cluster information, and then it will load balance search requests appropriately among cluster members against the "primary" alias. If you are using the blSolrIndexService, an instance of org.broadleafcommerce.core.search.service.solr.SolrIndexServiceImpl, Broadleaf will reindex against the "reindex" alias. When it finishes reindexing, it will reassign the aliases so that the collection that was reindexed will become the primary, and vice versa.

Happy Searching!