Elasticsearch 9200自動配置

陽光帥青年發表於2020-10-16
自動配置類:

package com.search.config;

import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;

@Configuration
public class ElasticsearchConfig extends AbstractElasticsearchConfiguration {

    @Value("${spring.data.elasticsearch.custom.cluster-nodes}")
    private String clusterNodes ;

    @Override
    public RestHighLevelClient elasticsearchClient() {
        final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
                .connectedTo(clusterNodes)
                .build();

        return RestClients.create(clientConfiguration).rest();
    }

}

yaml檔案:

spring:  
  data:
    elasticsearch:
      custom:
        cluster-nodes: 172.22.61.202:9200

 

相關文章