Elasticsearch客户端transport vs rest

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 将计划在后面的版本中废弃。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值