环境
Springboot1.5.6
solr5.3.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
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);
}
}
将单机版中注入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();
参考