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; } }