elasticsearch-6.8.23配置transport连接xpack密码,springboot2.1.18

本文详细描述了如何在使用较旧SpringBoot版本(2.0.1)和旧版ES(6.8.23)的项目中,通过生成证书、配置ES的SSL和密码,以及调整SpringDataElasticsearch的依赖来实现与ES的安全连接,特别强调了在不升级到高阶客户端的情况下,采用TransportClient的解决方案。
摘要由CSDN通过智能技术生成

项目场景:

项目使用spring-boot-starter-data-elasticsearch集成操作es,现在需要给es新增密码配置并成功连接es,项目版本写法比较老,代码耦合太多,无法升级high-level-client,只能配置transport连接

ES版本:6.8.23

springboot版本:2.0.1.RELEASE


1.配置ES

在这里插入图片描述

1.1 生成证书

在es项目目录bin中,运行命令

./elasticsearch-certutil ca
./elasticsearch-certutil cert --ca elastic-stack-ca.p12

会生成两个证书

elastic-cerificates.p12
elastic-stack-ca.p12

将两个文件移动到es中config文件夹

1.2 配置密码

编辑es配置文件elasticsearch.yml,新增以下配置

xpack.security.enabled: true #开启密码配置
xpack.security.transport.ssl.enabled: true #开启ssl连接
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.keystore.path: /es/elasticsearch-6.8.23/config/elastic-certificates.p12 #证书路径
xpack.security.transport.ssl.truststore.path: /es/elasticsearch-6.8.23/config/elastic-certificates.p12 #证书路径

在es中bin目录,运行命令
./elasticsearch-setup-passwords interactive

会让你输入多个内置账户的密码,自己记录一下。这里同时修改证书路径,1.1中生成的p12证书绝对路径;

注意:这里ssl必须设置为开启状态,否则es将无法启动,报错[ Transport SSL must be enabled…]

2.java配置

2.1 java配置类
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.util.Assert;

import java.net.InetAddress;
@Slf4j
@Configuration
public class ElasticsearchConfig {
    //连接信息改成自己的

    @Value("${elasticsearch.clusterName}")
    private String clusterName;

    @Value("${elasticsearch.clusterNodes}")
    private String clusterNodes;

    //用户名:密码
    @Value("${elasticsearch.clusterPassword}")
    private String clusterPassword;

    @Value("${elasticsearch.certPath}")
    private String certPath;

    static final String COLON = ":";//分号
    static final String COMMA = ",";//逗号

    @Bean
    public Client client() throws Exception {
        Settings settings = Settings.builder()
                .put("cluster.name", clusterName)
                .put("xpack.security.user", clusterPassword)
                .put("xpack.security.enabled", true)
                .put("xpack.security.transport.ssl.keystore.path", certPath)
                .put("xpack.security.transport.ssl.truststore.path", certPath)
                .put("xpack.security.transport.ssl.verification_mode", "certificate")
                .put("xpack.security.transport.ssl.enabled", true)
                .build();
        PreBuiltXPackTransportClient client = new PreBuiltXPackTransportClient(settings);
        for (String clusterNode : StringUtils.split(clusterNodes, COMMA)) {
            String hostName = StringUtils.substringBeforeLast(clusterNode, COLON);
            String port = StringUtils.substringAfterLast(clusterNode, COLON);
            Assert.hasText(hostName, "[Assertion failed] missing host name in 'clusterNodes'");
            Assert.hasText(port, "[Assertion failed] missing port in 'clusterNodes'");
            log.info("adding transport node : " + clusterNode);
            client.addTransportAddress(new TransportAddress(InetAddress.getByName(hostName), Integer.parseInt(port)));
        }
        return client;
    }

    @Bean
    public ElasticsearchTemplate elasticsearchTemplate() throws Exception {
        ElasticsearchTemplate elasticsearchTemplate;
        try {
            elasticsearchTemplate = new ElasticsearchTemplate(client());
            return elasticsearchTemplate;
        } catch (Exception e) {
            e.printStackTrace();
            return new ElasticsearchTemplate(client());
        }
    }

}

配置文件

elasticsearch.clusterName=my-application
elasticsearch.clusterNodes=192.168.223.132:9300
elasticsearch.clusterPassword=elastic:123456
elasticsearch.certPath=/es/elasticsearch-6.8.23/config/elastic-certificates.p12
2.2 依赖版本不对

由于es和spring-data的依赖版本不一致,导致ElasticsearchConfig中许多类没有,这里是spring-data与es的版本对应关系

在这里插入图片描述

重新配置依赖,需要引入transport和x-pack依赖

<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>6.8.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    <version>2.1.18.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-elasticsearch</artifactId>
    <version>3.2.13.RELEASE</version>
</dependency>
<dependency>
    <groupId>javax.jms</groupId>
    <artifactId>javax.jms-api</artifactId>
    <version>2.0.1</version>
</dependency>
<dependency>
    <groupId>org.elasticsearch.plugin</groupId>
    <artifactId>transport-netty4-client</artifactId>
    <version>6.8.0</version>
</dependency>
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>transport</artifactId>
    <version>6.8.0</version>
</dependency>
        <!-- https://mvnrepository.com/artifact/org.elasticsearch.client/x-pack-transport -->
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>x-pack-transport</artifactId>
    <version>6.8.11</version>
</dependency>
<dependency>
    <groupId>com.unboundid</groupId>
    <artifactId>unboundid-ldapsdk</artifactId>
    <version>3.2.0</version>
</dependency>

需要修改spring-boot版本为2.1.18

<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.18.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值