TransportClient基於Elasticsearch6.8.6 X-PACK


Elasticsearch6.8.6開啟X-PACK服務后,Transport連接方式

轉載,請注明出處:https://www.cnblogs.com/cchilei/p/13086160.html

pom文件

    <repositories>
        <!-- add the elasticsearch repo -->
        <repository>
            <id>elasticsearch-releases</id>
            <url>https://artifacts.elastic.co/maven</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>
        <!-- add the x-pack jar as a dependency -->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>x-pack-transport</artifactId>
            <version>6.8.6</version>
        </dependency>
    </dependencies>

1、沒有密鑰連接(博主沒有成功,不知道為啥,還在解決)

官網:https://www.elastic.co/guide/en/elasticsearch/reference/6.8/java-clients.html

import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient;

TransportClient client = new PreBuiltXPackTransportClient(Settings.builder()
        .put("cluster.name", "myClusterName")
        .put("xpack.security.user", "transport_client_user:x-pack-test-password")
        ...
        .build())
    .addTransportAddress(new TransportAddress("localhost", 9300))
    .addTransportAddress(new TransportAddress("localhost", 9301));

2、密鑰連接(親測成功)

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.InetAddress;
import java.net.UnknownHostException;

@Configuration
public class xpackdemo {

    @Bean
    public TransportClient transportClient() {
        TransportClient client = null;
        try {
            Settings settings = Settings.builder()
                    .put("cluster.name", "mycluster")
                    .put("xpack.security.user", "elastic:123456")
                    .put("xpack.ssl.key", "/demo/cert/x-pack/instance/instance.key")
                    .put("xpack.ssl.certificate", "/demo/cert/x-pack/instance/instance.crt")
                    .put("xpack.ssl.certificate_authorities", "/demo/cert/x-pack/ca/ca.crt")
                    .put("xpack.security.transport.ssl.verification_mode", "certificate")
                    .put("xpack.security.transport.ssl.enabled", "true")
                    .build();
            client = new PreBuiltXPackTransportClient(settings)
                    .addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return client;
    }
}

注:

/opt/cert/x-pack/instance/instance.key

/opt/cert/x-pack/instance/instance.crt

/opt/cert/x-pack/ca/ca.crt

是從config/x-pack文件夾中拷貝出來,放到程序中的

參考elasticsearch6.8.6配置xpack(生成密鑰):https://www.cnblogs.com/cchilei/p/13085842.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM