springboot如何连接solr集群

springboot已经集成solr,我这边使用的是集成的solr。solr的版本为1.4.3,JDK1.8

1、maven依赖配置如下:


            org.springframework.boot
            spring-boot-starter-data-solr

       

2、application.yml配置如下:

    spring:
      data:
        solr:
          host: http://10.25.129.183:8080/solr
          repositories:
            enabled: true

          zk-host: 10.174.10.184:2181,10.25.129.183:2181,10.45.40.118:2181

3、简单的请求java代码如下:

        配置solrclient注入:

@Autowired
        private CloudSolrClient cloudSolrClient;

public SolrDocumentList query(String value) {
SolrDocumentList st = null;
SolrQuery param = new SolrQuery();
param.setQuery( value );
param.setRows(5);
QueryResponse rsp = null;
try {
QueryRequest req = new QueryRequest( param,METHOD.POST );
req.setBasicAuthCredentials("username", "password");//如果有密码的话
cloudSolrClient.setDefaultCollection("corename");
rsp = req.process(cloudSolrClient);
st = rsp.getResults();
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return st;
}

你可能感兴趣的:(springboot,solr集群)