1. 程式人生 > >java建立elasticsearch5.X的Client,鑑權非鑑權兩種模式

java建立elasticsearch5.X的Client,鑑權非鑑權兩種模式

  java連線客戶端主要為兩種方式,一種是沒有鑑權,一種是叢集通過search-guard新增ssl鑑權。

  • 無鑑權模式
import org.elasticsearch.client.Client;
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.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class Test {

        private Client client = null;
        private DataSource dataSource;
        private JdbcTemplate jdbcTemplate;

        public void setDataSource(DataSource dataSources) {
            this.dataSource = dataSources;
            this.jdbcTemplate = new JdbcTemplate(dataSource);
        }

        public Client getESClient() {
            //叢集ip
            String host = "127.0.0.1";
            //叢集tcp埠,預設9300
            int port = 9300;
            //叢集名稱,配置裡的cluster_name
            String clusterName = "test_es";

            Settings settings = Settings.builder()
                    .put("cluster.name", clusterName)
                    .put("client.transport.ping_timeout", "10s")
                    .build();

            TransportClient client = new PreBuiltTransportClient(settings);
            try {
                //多節點的叢集,在此處分別呼叫client.add..即可
                client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), port));
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
            return  client;
        }

    }
  • 需要新增驗證的 (需要驗證方提供鑑權檔案,然後引入search-guard-ssl jar檔案)
import org.elasticsearch.client.Client;
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.springframework.jdbc.core.JdbcTemplate;
import com.floragunn.searchguard.ssl.SearchGuardSSLPlugin;

import javax.sql.DataSource;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class Test {

        private Client client = null;
        private DataSource dataSource;
        private JdbcTemplate jdbcTemplate;

        public void setDataSource(DataSource dataSources) {
            this.dataSource = dataSources;
            this.jdbcTemplate = new JdbcTemplate(dataSource);
        }

        public Client getESClient() {
            //叢集ip
            String host = "127.0.0.1";
            //叢集tcp埠,預設9300
            int port = 9300;
            //叢集名稱,配置裡的cluster_name
            String clusterName = "test_es";
            //鑑權檔案路徑
            String pathHome = "/home/test/";
            //key檔名稱
            String keyFile = "";
            //jks檔名稱
            String jksFile = "";

            Settings settings = Settings.builder()
                    .put("cluster.name", clusterName)
                    .put("client.transport.ping_timeout", "10s")
                    .put("path.home",pathHome)
                    .put("searchguard.ssl.transport.enabled", true)
                    .put("searchguard.ssl.transport.keystore_filepath", keyFile)
                    .put("searchguard.ssl.transport.truststore_filepath", jksFile)
                    .put("searchguard.ssl.transport.enforce_hostname_verification", false)
                    .build();

            TransportClient client = new PreBuiltTransportClient(settings);
            try {
                //多節點的叢集,在此處分別呼叫client.add..即可
                client = new PreBuiltTransportClient(settings,SearchGuardSSLPlugin.class)
                        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), port));
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
            return  client;
        }

    }