package estest;
import com.floragunn.searchguard.ssl.SearchGuardSSLPlugin;
import com.floragunn.searchguard.ssl.util.SSLConfigConstants;
import org.apache.commons.codec.binary.Base64;
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 java.net.InetAddress;
import java.net.UnknownHostException;
/**
* Created by tzq on 2019/2/18.
*/
public class GetClient {
public TransportClient transportClient() throws UnknownHostException {
// 一定要注意,9300为elasticsearch的tcp端口
InetSocketTransportAddress master = new InetSocketTransportAddress(InetAddress.getByName("hop-0"), 9300);
InetSocketTransportAddress node1 = new InetSocketTransportAddress(InetAddress.getByName("hop-1"), 9300);
InetSocketTransportAddress node2 = new InetSocketTransportAddress(InetAddress.getByName("hop-2"), 9300);
// InetSocketTransportAddress master = new InetSocketTransportAddress(InetAddress.getByName("hop-0"), 9300);
// InetSocketTransportAddress node1 = new InetSocketTransportAddress(InetAddress.getByName("hop-1"), 9300);
// InetSocketTransportAddress node2 = new InetSocketTransportAddress(InetAddress.getByName("hop-2"), 9300);
// 集群名称
Settings settings = Settings.builder()
.put(SSLConfigConstants.SEARCHGUARD_SSL_TRANSPORT_PEMKEY_FILEPATH, System.getProperty("user.dir")+"/src/main/resources/ssl/client.key")
.put(SSLConfigConstants.SEARCHGUARD_SSL_TRANSPORT_PEMCERT_FILEPATH, System.getProperty("user.dir")+"/src/main/resources/ssl/client.pem")
.put(SSLConfigConstants.SEARCHGUARD_SSL_TRANSPORT_PEMTRUSTEDCAS_FILEPATH, System.getProperty("user.dir")+"/src/main/resources/ssl/root-ca.pem")
.put(SSLConfigConstants.SEARCHGUARD_SSL_TRANSPORT_PEMKEY_PASSWORD, "dWHPUGUvBtr3")
// .put(SSLConfigConstants.SEARCHGUARD_SSL_TRANSPORT_PEMKEY_PASSWORD, "Ne0Vxrj6YZk2")
.put(SSLConfigConstants.SEARCHGUARD_SSL_TRANSPORT_ENFORCE_HOSTNAME_VERIFICATION, false)
// .put(SSLConfigConstants.SEARCHGUARD_SSL_HTTP_ENABLED, true)
// .put("searchguard.ssl.transport.enforce_hostname_verification", false)
.put("client.transport.sniff",false)
.put("cluster.name","ES-cluster" ).build();
TransportClient client = new PreBuiltTransportClient(settings, SearchGuardSSLPlugin.class);
// 添加
client.addTransportAddresses(master, node1, node2);
// client.threadPool().getThreadContext().addResponseHeader("Authorization", "Basic "+ Base64.encodeBase64("admin:admin".getBytes()));
System.out.println( Base64.encodeBase64("username:password".getBytes()));
return client;
}
}
评论0
最新资源