rest-high-level-client使用示例

rest-high-level-client使用示例,第1张

rest-high-level-client使用示例

rest-high-level-client使用示例
  • pom引入
  • 代码

pom引入

  org.elasticsearch.client
  elasticsearch-rest-high-level-client
  6.3.2

代码
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;

public class SecurityRestClient {
    private static final int HTTP_PORT = 20019;
    private static final String IP = "10.10.10.13";
    private static final String SecurityUser = "jiesi-6.3";
    private static final String SecurityPassword = "***********"; 
     
    public static RestHighLevelClient RestClient() {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost(IP, HTTP_PORT, "http")
                    ).setHttpClientConfigCallback(httpClientBuilder ->
                    httpClientBuilder.setDefaultCredentialsProvider(init()))
                );
        return client;
    }
     
    private static CredentialsProvider init() {
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials(SecurityUser, SecurityPassword));
        return credentialsProvider;
      }
}

注意回调方法setHttpClientConfigCallback内不要添加

httpClientBuilder.disableAuthCaching()

方法。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5677262.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存