【问题标题】:Java Data Elasticsearch client appending port 9200 to urlJava Data Elasticsearch 客户端将端口 9200 附加到 url
【发布时间】:2020-09-29 03:37:25
【问题描述】:

Spring Data Elasticsearch 版本:

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-elasticsearch</artifactId>
    <version>3.2.6.RELEASE</version>
</dependency>

我不明白为什么我的 Elasticsearch Highlevel 客户端总是将端口强制为 9200,即使我指定了端口 443

这是我定义RestHighLevelClient的方式:

@Slf4j
@Configuration
public class ElasticsearchConfiguration {

    @Value("${elasticsearch.host:127.0.0.1}")
    private String elasticsearchHost;

    @Value("${elasticsearch.port:9200}")
    private String elasticsearchPort;

    @Bean(destroyMethod = "close")
    public RestHighLevelClient client() throws IOException {
        log.info("Creating High level rest client for Elasticsearch with host: " + elasticsearchHost);
        ClientConfiguration configuration = ClientConfiguration.builder()
                    .connectedTo(elasticsearchHost + ":" + elasticsearchPort)
                    .usingSsl()
                    .build();

        return RestClients.create(configuration).rest();
    }

    @Bean
    public ElasticsearchRestTemplate getElasticsearchTemplate() throws IOException {
        return new ElasticsearchRestTemplate(client());
    }
}

以及我是如何使用模板的:

private ElasticsearchRestTemplate elasticsearchTemplate;

@Autowired
public ElasticsearchServiceImpl(@Qualifier("getElasticsearchTemplate") ElasticsearchRestTemplate elasticsearchRestTemplate) {
    this.elasticsearchTemplate = elasticsearchRestTemplate;
}

@PostConstruct
public void init() throws IOException {
    create indexes...
}

我已验证主机和端口都在属性文件中设置:

--elasticsearch.host=https://vpc-example-ryphgjfwji3zonliyhhd3nu.eu-west-2.es.amazonaws.com
--elasticsearch.port=443

在我收到的堆栈跟踪错误中:Caused by: java.io.IOException: https://vpc-example-ryphgjfwji3zonliyhhd3nu.eu-west-2.es.amazonaws.com:443:9200,它似乎将9200 附加到 url 的末尾。为什么会这样?

我已验证通过 curl 成功建立连接。

编辑:添加了另一个堆栈跟踪

un 09 21:44:44 ip-10xxxx95.pre.xxx-prototype.xxx.uk bash[19738]: 2020-06-09 21:44:44.023 ERROR 19738 --- [           main] c.g.c.s.impl.ElasticsearchServiceImpl    : https://vpc-example-ryphgjfwji3zonliyhhd3nu.eu-west-2.es.amazonaws.com: Name or service not known
Jun 09 21:44:44 ip-10-2xxxxx5.pre.xxx-prototype.xxx.uk bash[19738]: 2020-06-09 21:44:44.024 ERROR 19738 --- [           main] c.g.c.s.impl.ElasticsearchServiceImpl    : [org.elasticsearch.client.RestClient$SyncResponseListener.get(RestClient.java:964), org.elasticsearch.client.RestClient.performRequest(RestClient.java:233), org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1764),

【问题讨论】:

    标签: java spring elasticsearch resthighlevelclient


    【解决方案1】:

    您可以尝试创建如下所示的 resthighleval 客户端吗:

    RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
                    RestClient.builder(new HttpHost(configuration.getElasticsearchConfig().getHost(),
                            configuration.getElasticsearchConfig().getPort(),
                            "https")));
    

    并使用以下配置,注意我将端口配置留空:

    --elasticsearch.host=https://vpc-example-ryphgjfwji3zonliyhhd3nu.eu-west-2.es.amazonaws.com --elasticsearch.port=

    我过去创建了与 AWS ES 和托管在 AWS 中的 ES 的连接,如果您遇到代码问题,请告诉我,我们很乐意提供进一步帮助。

    【讨论】:

      【解决方案2】:

      指定主机时不要指定协议(https)。在您的情况下,它将是:

      --elasticsearch.host=vpc-example-ryphgjfwji3zonliyhhd3nu.eu-west-2.es.amazonaws.com
      --elasticsearch.port=443
      

      【讨论】:

        猜你喜欢
        • 2017-07-05
        • 2021-10-04
        • 2020-10-14
        • 1970-01-01
        • 2017-12-21
        • 1970-01-01
        • 2018-10-13
        • 1970-01-01
        • 2015-10-19
        相关资源
        最近更新 更多