首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >spring数据elasticsearch属性在反向代理(带有web上下文路径的URI)后面到达elasticsearch

spring数据elasticsearch属性在反向代理(带有web上下文路径的URI)后面到达elasticsearch
EN

Stack Overflow用户
提问于 2021-09-02 12:27:08
回答 2查看 304关注 0票数 0

我在使用spring 2.4.6和。

我想为elasticsearch设置我的配置,如下所示,因为服务器正在执行一个重写URL的反向代理。

代码语言:javascript
复制
spring.elasticsearch.rest.uris=https://admin-integration.hello.com/elasticsearch/api
spring.elasticsearch.rest.username=elastic
spring.elasticsearch.rest.password=blabla

我得到了

java.net.UnknownHostException: admin-integration.hello.com/elasticsearch/api

我该怎么处理呢?我在配置中找不到任何相关的属性。有线索吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-09-02 13:18:38

配置可以接受一个withPathPrefix(String)参数(参见https://docs.spring.io/spring-data/elasticsearch/docs/4.2.4/reference/html/#elasticsearch.clients.configuration上的文档)

这是Spring问题,因为这些属性由Spring处理,以设置Spring数据弹性搜索配置。

票数 1
EN

Stack Overflow用户

发布于 2021-09-03 14:44:09

多亏了PJ的回复,我才能解决这个问题

不要使用弹簧引导启动器,而是使用普通的弹簧数据。

代码语言:javascript
复制
    <!-- we use our own configuration because webpath prefix can not be provided with the stuffs below
        https://stackoverflow.com/questions/69030292/spring-data-elasticsearch-propery-to-reach-elasticsearch-behind-a-reverse-proxy/69031077#69031077
         https://reflectoring.io/spring-boot-elasticsearch/
         https://hackernoon.com/elasticsearch-in-java-spring-boot-starter-pack-3kx330h
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        --> 
        
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-elasticsearch</artifactId>
      </dependency>

使用普通配置

代码语言:javascript
复制
    @Configuration
@EnableElasticsearchRepositories(basePackages = "lu.esante.agence.referential.elasticsearch.accessdb.repositories")
@ComponentScan(basePackages = { "lu.esante.agence.referential.accessdb" })
public class ElasticsearchClientConfig extends AbstractElasticsearchConfiguration {

    @Value("${my.elasticsearch.rest.prefix:}")
    private String es_prefix;

    @Value("${my.elasticsearch.rest.uris}")
    private String es_uri;

    @Value("${my.elasticsearch.rest.username}")
    private String es_user;

    @Value("${my.elasticsearch.rest.password}")
    private String es_password;
    
    @Value("${my.elasticsearch.rest.ssl:false}")
    private String es_ssl;

    @Override
    @Bean
    public RestHighLevelClient elasticsearchClient() {

        final ClientConfiguration clientConfiguration = !StringUtils.isEmpty(es_prefix)
                ? ClientConfiguration.builder().connectedTo(es_uri).usingSsl().withPathPrefix(es_prefix)
                        .withBasicAuth(es_user, es_password).withConnectTimeout(Duration.ofSeconds(5))                            
                          .withSocketTimeout(Duration.ofSeconds(3)).build()
                : ClientConfiguration.builder().connectedTo(es_uri).withBasicAuth(es_user, es_password).build();

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

一些提示:

  • 前缀不能为空或空,您必须指定端口。如果没有,默认情况下将花费9200

代码语言:javascript
复制
my.elasticsearch.rest.uris=hello.com:443
my.elasticsearch.rest.ssl=true
my.elasticsearch.rest.prefix=elasticsearch/api
my.elasticsearch.rest.username=elastic
my.elasticsearch.rest.password=
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69030292

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档