springboot(2.0.2.RELEASE) 添加es集群

添加maven依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

添加application.yml配置

spring:
  data:
    elasticsearch:
      mobile-matrix:
        cluster: es-data-matrix
        itemList:
        - host: 192.168.1.179
          port: 9300
        - host: 192.168.1.66
          port: 9300
        - host: 192.168.1.69
          port: 9300

config 配置

package com.huiyu.ftp.config.elasticsearch;

import com.alibaba.fastjson.JSONObject;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;

/**
 * @author chenpeng
 * @date 2018/8/23
 */
@Configuration
@ConfigurationProperties(prefix = "spring.elasticsearch.mobile-matrix")
public class ElasticsearchConfig {
    private static final Logger logger = LoggerFactory.getLogger(ElasticsearchConfig.class);

    private String cluster;
    private List<ElasticsearchItem> itemList;

    @Bean
    @Primary
    public TransportClient transportClient() throws UnknownHostException {

        for (ElasticsearchItem elasticsearchItem : itemList) {
            logger.info("load es config:" + JSONObject.toJSONString(elasticsearchItem));
        }

        Settings settings = Settings.builder().put("cluster.name", cluster).put("client.transport.sniff", true).build();
        PreBuiltTransportClient preBuiltTransportClient = new PreBuiltTransportClient(settings);
        for (ElasticsearchItem elasticsearchItem : itemList) {
            preBuiltTransportClient.addTransportAddresses(new InetSocketTransportAddress(InetAddress.getByName(elasticsearchItem.getHost()), elasticsearchItem.getPort()));
        }
        return preBuiltTransportClient;

    }

    public String getCluster() {
        return cluster;
    }

    public void setCluster(String cluster) {
        this.cluster = cluster;
    }

    public List<ElasticsearchItem> getItemList() {
        return itemList;
    }

    public void setItemList(List<ElasticsearchItem> itemList) {
        this.itemList = itemList;
    }
}

class ElasticsearchItem {
    private String host;
    private int port;

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }
}

测试(todo)

results matching ""

    No results matching ""