Spring 2.1.18集成Elasticsearch 6.4.3配置报错求助
解决方案
一、问题根源分析
xpack.security.user配置无效:Elasticsearch 6.4.3的TransportClient不支持直接通过该配置项设置账号密码,且缺少xpack-transport依赖,导致无法识别xpack相关安全配置。- 版本兼容性冲突:spring-data-elasticsearch 4.0.0要求Spring Boot 2.3及以上版本,与你固定使用的Spring Boot 2.1.18.RELEASE不兼容,因此升级后出现类加载失败错误,必须使用适配的3.1.x版本。
二、修正依赖配置
在pom.xml中补充xpack-transport依赖(版本需与Elasticsearch 6.4.3完全一致),并统一所有Elasticsearch相关依赖版本:
<dependencies> <!-- Spring Data Elasticsearch 适配Spring Boot 2.1.x --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-elasticsearch</artifactId> <version>3.1.21.RELEASE</version> </dependency> <!-- Elasticsearch 核心依赖 --> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>6.4.3</version> </dependency> <!-- Transport Client --> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>6.4.3</version> </dependency> <!-- X-Pack Transport 用于安全认证 --> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>x-pack-transport</artifactId> <version>6.4.3</version> </dependency> </dependencies>
三、修正Elasticsearch配置类
替换原配置类,正确配置TransportClient的SSL连接与账号密码认证:
@Configuration @EnableElasticsearchRepositories(basePackages = "path.to.elastic") @ComponentScan(basePackages = {"path.to.elastic"}) public class ElasticConfiguration { private final int port = 1234; private final String username = "login"; private final String password = "pass"; private final String clusterName = "test"; @Bean public Client client() throws Exception { // 构建SSL上下文(示例为信任所有证书,生产环境建议替换为加载合法证书的实现) SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, new TrustManager[]{new X509TrustManager() { @Override public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {} @Override public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {} @Override public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } }}, new SecureRandom()); Settings settings = Settings.builder() .put("cluster.name", clusterName) // 启用xpack安全传输 .put("xpack.security.transport.ssl.enabled", true) .put("xpack.security.transport.ssl.verification_mode", "none") .put("xpack.security.transport.ssl.ssl_context", sslContext) // 配置账号密码 .put("xpack.security.user", username + ":" + password) .build(); // 使用PreBuiltXPackTransportClient而非普通PreBuiltTransportClient TransportClient client = new PreBuiltXPackTransportClient(settings); client.addTransportAddress(new TransportAddress(InetAddress.getByName("bah.elk1.com"), port)); client.addTransportAddress(new TransportAddress(InetAddress.getByName("bah.elk2.com"), port)); client.addTransportAddress(new TransportAddress(InetAddress.getByName("bah.elk3.com"), port)); return client; } @Bean public ElasticsearchOperations elasticsearchTemplate() throws Exception { return new ElasticsearchTemplate(client()); } }
四、额外优化建议
- 实体类
@Document注解中,Elasticsearch 6.x开始弱化type概念,建议将type设置为_doc以适配后续版本:
@Data @Document(indexName = "things-*", type = "_doc") public class Things { @Id private Long id; private String earName; private String name; }
- 确保ES集群的transport端口(你使用的1234)对外开放,且服务器防火墙允许你的应用服务器访问。
- 确认所用账号拥有目标索引的CRUD权限。
内容的提问来源于stack exchange,提问作者Aleksey Bakharev
相关产品推荐
相关产品推荐

