Elasticsearch问题解决:unknown setting [xpack.security.transport.ssl.enabled] please check that any required plugins are installed
今天有个小伙伴说Elasticsearch从高版本的java客户端换成原生的TransportClient后,连不上ES集群了,我查看并测试了这个问题。
一、查看现有集群信息
我使用他提供的ES集群链接信息,在服务器上看了一下集群的基本信息,发现es是7.10版本的。
[bi@t1-test:~]$
[bi@t1-test:~]$
[bi@t1-test:~]$ curl -u username:password -X GET http://XX.XX.XX.XX:9200/
{
"name" : "XX.XX.XX.XX",
"cluster_name" : "es7-t1",
"cluster_uuid" : "43eoRwXkQO-to_SRKzwZNQ",
"version" : {
"number" : "7.10.0",
"build_flavor" : "default",
"build_type" : "rpm",
"build_hash" : "XXXXXXX8d0374a0f3f5c6e8f3a7997850f96",
"build_date" : "2020-11-09T21:30:33.964949Z",
"build_snapshot" : false,
"lucene_version" : "8.7.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
[bi@t1-test:~]$
[bi@t1-test:~]$
二、使用原生TransportClient客户端连接测试
ES最原生的客户端是TransportClient,需要使用原生的jar包。搭了一个maven项目测试了一下:
原生的依赖:
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.10.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>7.10.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>x-pack-transport</artifactId>
<version>7.10.0</version>
</dependency>
java测试:
package com.wuxl.es;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import java.net.InetAddress;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class ESTest {
public static void main(String[] args) throws Exception {
// es没有xpack认证情况下是可以访问的
// Settings settings = Settings.builder()
// .put("cluster.name", "es7-t1") //设置ES实例的名称
// .put("client.transport.sniff", true) //自动嗅探整个集群的状态,把集群中其他ES节点的ip添加到本地的客户端列表中
// .build();
// TransportClient client = new PreBuiltTransportClient(settings)
// .addTransportAddress(new TransportAddress(InetAddress.getByName("XX.XX.XX.XX"), 9300));
// es有Xpack认证情况下访问失败
Settings settings = Settings.builder()
.put("cluster.name", "es7-t1")
.put("xpack.security.user","user:password") // 设置账号密码
.put("xpack.security.transport.ssl.enabled",false)//设置xpack权限用户
.put("client.transport.sniff", true)
.build();
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("XX.XX.XX.XX"), 9300));
// 数据详情
Map<String, Object> json = new HashMap<String, Object>();
json.put("user", "kimchy");
json.put("postDate", new Date());
json.put("message", "trying out Elasticsearch");
// 索引文档
IndexResponse indexResponse = client.prepareIndex("twitter", "_doc", "1").setSource(json, XContentType.JSON).get();
String _index = indexResponse.getIndex();
String _type = indexResponse.getType();
String _id = indexResponse.getId();
long _version = indexResponse.getVersion();
RestStatus status = indexResponse.status();
// 搜索文档
GetResponse documentFields = client.prepareGet("twitter", "_doc", "1").get();
System.out.println(documentFields.getIndex());
System.out.println(documentFields.getType());
System.out.println(documentFields.getId());
System.out.println(documentFields.getSourceAsString());
// 关闭客户端链接
client.close();
}
}
使用上面的代码测试es,在es没有安装xpack情况下是可以正常访问的,但是安装xpack后,测试报错:无效参数xpack.security.transport.ssl.enabled和xpack.security.user
Exception in thread "main" java.lang.IllegalArgumentException: unknown setting [xpack.security.transport.ssl.enabled] please check that any required plugins are installed, or check the breaking changes documentation for removed settings
at org.elasticsearch.common.settings.AbstractScopedSettings.validate(AbstractScopedSettings.java:544)
at org.elasticsearch.common.settings.AbstractScopedSettings.validate(AbstractScopedSettings.java:489)
at org.elasticsearch.common.settings.AbstractScopedSettings.validate(AbstractScopedSettings.java:460)
at org.elasticsearch.common.settings.AbstractScopedSettings.validate(AbstractScopedSettings.java:431)
at org.elasticsearch.common.settings.SettingsModule.<init>(SettingsModule.java:149)
at org.elasticsearch.client.transport.TransportClient.buildTemplate(TransportClient.java:157)
at org.elasticsearch.client.transport.TransportClient.<init>(TransportClient.java:293)
at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:130)
at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:116)
at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:106)
at com.wuxl.es.ESTest.main(ESTest.java:37)
Suppressed: java.lang.IllegalArgumentException: unknown setting [xpack.security.user] please check that any required plugins are installed, or check the breaking changes documentation for removed settings
... 11 more
翻阅官方文档后,发现xpack作为插件只维护到6.2版本,后面就不维护了,具体参考https://www.elastic.co/guide/en/x-pack/current/index.html
同时,elastic stack的版本兼容性矩阵也显示es6.3之后就不用在安装主动xpack插件了,参考https://www.elastic.co/cn/support/matrix#matrix_compatibility
es6.3之后,Xpack会默认安装在es中,也就是xpack变成了es的内置功能,而不是以插件的形式存在低版本的es中了,以前xpack是收费插件,现在开源了,具体参考:https://www.elastic.co/cn/what-is/open-x-pack。在高版本的es中安装xpck插件也会提示这一点:
关于Elastic stack的生命周期的说明可以参考:https://www.elastic.co/cn/support/eol
上面的测试代码中的Transport的原生API在es7.0中高级API中已经过期了,在8.0以后也会被移除,所以不建议使用了。
三、高级API
官方建议使用高级API,下面就是高级api的测试:
依赖包:
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.10.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.10.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.10.0</version>
</dependency>
测试类:
package com.wuxl.es;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import com.alibaba.fastjson.JSON;
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.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
public class EsUtil {
public static void main(String[] args) throws Exception{
/** 用户认证对象 */
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
/** 设置账号密码 */
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("user", "password"));
/** 创建rest client对象 */
RestClientBuilder builder = RestClient.builder(new HttpHost("XX.XX.XX.XX", 9200))
.setHttpClientConfigCallback(new HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
});
RestHighLevelClient client = new RestHighLevelClient(builder);
//1.创建索引的请求
CreateIndexRequest request = new CreateIndexRequest("lisen_index");
CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
System.out.println(response);
//2.插入数据
User user = new User("lisen",27);
IndexRequest request = new IndexRequest("lisen_index");
request.id("1");
//设置超时时间
request.timeout("1s");
//将数据放到json字符串
request.source(JSON.toJSONString(user), XContentType.JSON);
//发送请求
IndexResponse response = client.index(request,RequestOptions.DEFAULT);
System.out.println("添加文档-------"+response.toString());
System.out.println("添加文档-------"+response.status());
client.close();
}
}
|