Search code examples
springsolrapache-zookeeperspring-data-solr

Does Spring Data Solr work with zookeeper host?


I have a spring boot application and in my properties file I have:

spring.data.solr.host=http://{SOLR.HOST}/solr

This works fine but if I replaced the SOLR.HOST with ZOOKEEPER.HOST, it throws an I/O exception while talking to the server. It seems to me that spring data is trying to hit the zookeeper endpoint directly through http request, and zookeeper doesn't handle http requests? If so what can I do to allow spring data to work with zookeeper?


Solution

  • It looks like you need to use the CloudSolrClient to instantiate the SolrClient bean https://dzone.com/articles/spring-data-solr-cloud-zookeeper-mongodb-ubuntu-in

    @Configuration
    @EnableSolrRepositories(basePackages = { "ca.knc.restaurant.repository.solr" }, multicoreSupport = true)
    public class SpringSolrConfig {
        @Value("${spring.data.solr.zk-host}")
        private String zkHost;
        @Bean
        public SolrClient solrClient() {
            return new CloudSolrClient(zkHost);
        }
        @Bean
        public SolrTemplate solrTemplate(SolrClient solrClient) throws Exception {
            return new SolrTemplate(solrClient);
        }
    }
    

    You can also set it through the application.properties and let Spring Boot autoconfigure it :

    spring.data.solr.zk-host= # ZooKeeper host address in the form HOST:PORT.

    https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

    https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/solr/SolrAutoConfiguration.java


    Otherwise, you can use a LoadBalancer to serve your Solr Instances and use a regular Solr Host configuration

    For example, I have the following configuration (yml config) :

    spring.data.solr.host: https://foo.bar.com/solr
    

    Where foo.bar.com serves one of my Solr instance with Apache as a standard Load Balancer.