SolrCloud在springboot中的应用

搭建solrcloud可以看之前的文章,这里主要记录一下springboot中使用solrCloud

博主的应用场景:三台solr,多个collection的情况
首先我们要明白,我们用zookeeper来管理solr ,这样我们在连接solr做增删改的时候就不需要再连solr了,这里直接连三个zk的集群

配置文件:
spring:
    data:
        solr:
            repositories:
                enabled: true
            zk-host: 这里写你自己的zk地址
代码

这里可以直接在方法内build,也可以通过bean注入的方式

第一种
CloudSolrClient solrClient = new CloudSolrClient.Builder(Arrays.asList(ZK_HOST.split(",")), Optional.empty()).build();
client.setDefaultCollection(“这里写自己的collection”);

第二种
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;

import java.util.Arrays;
import java.util.Collections;
import java.util.Optional;

@Configuration
public class SearchConfig {

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

    /**
     * 注入cloudSolrClient
     *
     * @return
     */
    @Bean
    public CloudSolrClient solrClient() {

        CloudSolrClient solrClient = new CloudSolrClient.Builder(Arrays.asList(ZK_HOST.split(",")), Optional.empty()).build();
        return solrClient;
    }
}

service层:

@Autowired
    private CloudSolrClient client;

public void test(){
	client.setDefaultCollection(“这里写自己的collection”);
}

遇到的问题:
Exception in thread “main” java.lang.RuntimeException: Couldn’t initialize a HttpClusterStateProvider (is/are the Solr server(s), [localhost:8983], down?)
解决:
https://cloud.tencent.com/developer/ask/sof/105701440
主要是加上Optional.empty()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值