zh-hongda

搜索引擎-Springboot与solrCloud

springboot整合solrCloud

环境

Springboot1.5.6

solr5.3.1

  1. 在application.yml文件下做如下配置
#application.properties
spring:
  data:
    solr:
      host: http://127.0.0.1:8180/solr
      repositories:
        enabled: true
      zk-host: 127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183
  1. 手动加载clientbean
package com.redgo.controller;

import org.apache.solr.client.solrj.impl.CloudSolrClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SearchConfig {

    @Value("${spring.data.solr.zk-host}")
    private String zkHost;

    @Bean
    public CloudSolrClient solrClient() {
        return new CloudSolrClient(zkHost);
    }
}
  1. 将单机版中注入solrClient类的位置,替换为CloudSolrClient类,但是与单机版不同的地方在于,使用时需要指定collection。

    @Repository
    public class FileDAOImpl implements FileDAO{
    
        @Autowired
    //    private SolrClient solrClient;
        public CloudSolrClient solrClient;
        ...
        ...
        ...
    
        @Override
        public HashMap<String, Object> findAll(String queryString, String page, String powerStation, String unit, String docType){
    
            HashMap<String, Object> response = null;
            try {
                response = new HashMap<>();
    
                Integer pageNumb = Integer.parseInt(page);
                solrClient.setDefaultCollection("core2");//与单机版不同在使用时需要指定collection。
    
                SolrQuery params = new SolrQuery();
    
    1. 整合完毕。其他地方与单机版一致无需修改。

参考

springboot中使用SolrClient和CloudSolrClient

Spring Boot 中使用 SolrCloud