Elasticsearch(ES)有两种连接方式:transport、rest。transport通过TCP方式访问ES(只支持java),rest方式通过http API 访问ES(没有语言限制)。
Transport
- 引入maven依赖
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
- 创建transport client(5.x&6.X版本ES)
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
Settings settings = Settings.builder()
.put("cluster.name", "myClusterName")
.put("client.transport.sniff", true)
.build();
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host1"), 9300));
设置client.transport.sniff为true来使客户端去嗅探整个集群的状态,把集群中其它机器的ip地址加到客户端中,这样做的好处是一般你不用手动设置集群里所有集群的ip到连接客户端,它会自动帮你添加,并且自动发现新加入集群的机器。
rest(已引入transport不能够再引入 rest,包会发生冲突)
官方提供low-level rest client(支持5.0及以后版本) 和high-level rest client(支持版本为 5.6及以后版本)
low-level rest client
- 引入maven依赖
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>6.5.4</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>rest</artifactId>
<version>5.3.2</version>
</dependency>
- 创建rest client
Header[] defaultHeaders = new Header[] { new BasicHeader("header", "value") };
RestClient restClient = RestClient.builder(
new HttpHost("localhost", 9200, "http"))
.setDefaultHeaders(defaultHeaders) //设置默认标头
.setMaxRetryTimeoutMillis(10000) //同一请求重试超时时间,默认30s
.setFailureListener(new RestClient.FailureListener() {
}) //设置侦听器,节点出现故障会收到通知
.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
//create the authentication scope
AuthScope authScope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope
.ANY_REALM);
// Create credential pair(username,password)
UsernamePasswordCredentials usernamePasswordCredentials = new UsernamePasswordCredentials("username", "password");
credentialsProvider.setCredentials(authScope, usernamePasswordCredentials);
return httpClientBuilder.setKeepAliveStrategy(new ConnectionKeepAliveStrategy() {
@Override
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
return 30 * 1000;
}
}).setDefaultCredentialsProvider(credentialsProvider);
}
})
.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
@Override
public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) {
return requestConfigBuilder.setSocketTimeout(10000);
}
})
.build();
high level rest client
- 引入maven依赖
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>rest</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
- 创建rest client
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
ES官方建议使用rest方式, transport 将计划在后面的版本中废弃。