简体   繁体   English

使用Java客户端和X-Pack / HTTPS连接到ElasticSearch Cloud 5.x.

[英]Connecting to ElasticSearch Cloud 5.x with Java Client and X-Pack/HTTPS

Is it possible to connect to ElasticSearch via HTTPS using X-Pack? 是否可以使用X-Pack通过HTTPS连接到ElasticSearch? With the switch to use certificate, the own way of connecting is no longer work. 随着切换到使用证书,自己的连接方式不再起作用。 I have no problem with using certificate, but I need to know where to get one or upload the key to the cloud instance, but I cannot find information anywhere. 我使用证书没有问题,但我需要知道从哪里获取或将密钥上传到云实例,但我无法在任何地方找到信息。 I'm also getting no answer from anyone in the forum or IRC. 我也没有得到论坛或IRC中任何人的回答。

Has anyone successfully done this? 有没有人成功完成这个? There is no longer a warning when launching a 5.x instance so I assumed this is possible, but I just can't figure out how to do it. 启动5.x实例时不再有警告,所以我认为这是可能的,但我无法弄清楚如何做到这一点。 I was also told that I can no longer launch a 2.4.1 instance and only 2.4.2 is available (too unstable so far) so I'm kinda stuck not being able to launch a usable instance. 我还被告知我不能再启动一个2.4.1实例,只有2.4.2可用(到目前为止太不稳定)所以我有点无法启动一个可用的实例。


Update 更新

I was told that no certificate is required since public CA is being used. 有人告诉我,因为使用了公共CA,所以不需要证书。 However, I'm still not able to figure out how to connect to 5.1.1 instance. 但是,我仍然无法弄清楚如何连接到5.1.1实例。

Settings settings = Settings.builder()
            .put("transport.ping_schedule", "5s")
            .put("cluster.name", "<cluster_id>")
            .put("xpack.security.transport.ssl.enabled", "true")
            .put("xpack.security.user", "elastic:<password_from_cluster_creation>")
            .build();

    String hostname = "<cluster_id>.us-east-1.aws.found.io";
    TransportClient client = new PreBuiltXPackTransportClient(settings)
        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostname), 9343));

What am I doing wrong here? 我在这做错了什么? I'm getting an error saying node is not available. 我收到一个错误,说节点不可用。

Exception in thread "main" NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{xlhZqKBCQniYrN4OWzByFQ}{<cluster_id>.us-east-1.aws.found.io}{<id_address>:9343}]]

I also tried creating a user with transport_client role, but still the same error. 我也尝试使用transport_client角色创建用户,但仍然是同样的错误。


Update 2 更新2

I tried the sample code from here and it still doesn't work. 我从这里尝试了示例代码,它仍然无法正常工作。

https://github.com/elastic/found-shield-example/blob/master/src/main/java/org/elasticsearch/cloud/transport/example/TransportExample.java https://github.com/elastic/found-shield-example/blob/master/src/main/java/org/elasticsearch/cloud/transport/example/TransportExample.java

Here's an instance you can test with. 这是您可以测试的实例。 I will destroy it later after I have figured out the issue. 在我弄清楚这个问题之后,我会把它毁掉。


Update 3 更新3

I figured out the issue and destroyed the test cluster. 我弄清楚了问题并破坏了测试集群。 Find the solution below. 找到下面的解决方案。

After some discussion with people on ES forum, I got it working finally. 在ES论坛上与人们进行了一些讨论后,我终于开始工作了。 Here are some tips when you run into an issue with connection: 当您遇到连接问题时,以下是一些提示:

1) You probably didn't add this in the header since it's not mentioned in the documentation 1)您可能没有在标题中添加它,因为文档中没有提到它

.put("request.headers.X-Found-Cluster", clusterName)

2) If that still doesn't work, use the sample code from here to verify that you hostname is correct and you have the correct credential 2)如果仍然无效,请使用此处的示例代码验证您的主机名是否正确并且您拥有正确的凭据

https://github.com/elastic/found-shield-example#running https://github.com/elastic/found-shield-example#running

3) If you are trying to run the sample code within Eclipse, replace ${cluster.name} before you run it or it won't work. 3)如果您尝试在Eclipse中运行示例代码,请在运行之前替换$ {cluster.name},否则它将无效。

Hope this help save people some time trying to figure out connection issue. 希望这有助于节省人们一些时间来解决连接问题。 Connecting to ES 5.x is about as easy as 2.x. 连接到ES 5.x几乎和2.x一样简单。 The problem is that the documentation is not clear and there is no link to sample client. 问题是文档不清楚,并且没有到示例客户端的链接。

I had the same issue before and I resolved it using the following configuration 之前我有同样的问题,我使用以下配置解决了它

Required dependencies: 必需的依赖项:

(transport and x-pack-transport should have the same version of the elasticsearch 5.5.3) (transport和x-pack-transport应该具有相同版本的elasticsearch 5.5.3)

<dependencies>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-elasticsearch</artifactId>
        <version>3.0.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>transport</artifactId>
        <version>5.5.3</version>
    </dependency>
    <!--add the x-pack jar as a dependency-->
    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>x-pack-transport</artifactId>
        <version>5.5.3</version>
    </dependency>
</dependencies>

<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>

Java Config: Java配置:

@Value("${elasticsearch.cluster.url}")
private String elasticsearchClusterUrl;

@Value("${elasticsearch.cluster.port}")
private int elasticsearchClusterPort;

@Value("${elasticsearch.cluster.name}")
private String elasticsearchClusterName;

@Value("${elasticsearch.cluster.credentials}")
private String elasticsearchCredentials;

@Bean
public TransportClient elasticsearchClient()
        throws Exception {

    Settings settings = Settings.builder()
            .put("cluster.name", elasticsearchClusterName)
            .put("request.headers.X-Found-Cluster", elasticsearchClusterName)
            .put("xpack.security.transport.ssl.enabled", true)
            .put("xpack.security.user", elasticsearchCredentials)
            .build();

    return new PreBuiltXPackTransportClient(settings)
            .addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress(elasticsearchClusterUrl, elasticsearchClusterPort)));
}

application.yml: application.yml:

elasticsearch:
  index: dev_index
  cluster:
    name: 2083ddf0fbf5a3b1f0c50ff257ded077
    url: 2083ddf0fbf5a3b1f0c50ff257ded077.eu-west-1.aws.found.io
    port: 9343
    credentials: elastic:pass

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM