【问题标题】:Can I use multiple elastic search hosts in Spring Bean configuration for multiple elastic search indexes我可以在 Spring Bean 配置中为多个弹性搜索索引使用多个弹性搜索主机吗
【发布时间】:2019-08-14 04:29:45
【问题描述】:

我有一个 Java API 可以将文档插入三个弹性索引,并且它正在工作。我想在 API 中更改一个索引的主机。通常,EsConfig 文件和 ElasticSearchTemplate 代码是 ;

public class EsConfig {
    @Value("${elasticsearch.host}")
    private String EsHost;
   @Value("${elasticsearch.port}")
   private int EsPort;
   @Value("${elasticsearch.clustername}")
   private String EsClusterName;

   @Bean
   public Client client() throws Exception {
      Settings settings = Settings.builder()
            .put("cluster.name", EsClusterName)
            //.put("index.max_result_window", 4000000)
            .build();

      TransportClient client = new PreBuiltTransportClient(settings)
            .addTransportAddress(new 
      TransportAddress(InetAddress.getByName(EsHost), EsPort));
      return client;
     }

     @Bean
     public ElasticsearchTemplate elasticsearchTemplate() throws Exception {
          ElasticsearchTemplate elasticsearchTemplate = new ElasticsearchTemplate(client());
          return elasticsearchTemplate;
      }

}

我想将此结构配置为同时使用两个主机。这种结构有可能还是我应该完全改变和删除单例bean结构?

【问题讨论】:

    标签: java elasticsearch indexing host spring-bean


    【解决方案1】:

    Elasticsearch 客户端 api 允许您以以下方式配置多个主机名,但不幸的是,它们无法按预期工作。

    据此LINK

    TransportClient 远程连接到 Elasticsearch 集群 使用传输模块。它不加入集群,而只是 获取一个或多个初始传输地址并与 他们在每个动作中以循环方式进行(尽管大多数动作都会 可能是“两跳”操作)。

    TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
            .addTransportAddress(new TransportAddress(InetAddress.getByName("host1"), 9300))
            .addTransportAddress(new TransportAddress(InetAddress.getByName("host2"), 9300));
    

    您可以做的也许是继续实现 Profiles 的 Spring Boot 概念,您可以在其中创建多个 application.properties。以下是实现这一目标的方法之一。

    如果我有两个不同的主机/环境,例如devprod,我最终会创建三个应用程序属性文件(两个用于环境,一个属性会提及您要选择的环境)。

    application-dev.properties

    elasticsearch.clustername = mycluster_name
    elasticsearch.host = mydev.abc.com          <-- Configure the name of host 1
    elasticsearch.port = 9300
    

    application-prod.properties

    elasticsearch.clustername = mycluster_name_2
    elasticsearch.host = myprod.abc.com         <-- Configure the name of host 2
    elasticsearch.port = 9300
    

    application.properties

    spring.profiles.active=dev                 <-- Configure which of the above properties do you want to start application with
    

    现在,当您将应用程序作为 Spring Boot 运行时,它最终会启动 dev 环境。

    请注意,我假设这两个节点都在不同的集群中,而不是同一个集群的一部分。我指定这个的原因是,elasticsearch 在内部会继续更新其他节点的副本分片,如果它接收到一个新的/更新的文档。在同一个集群中,您指向集群中的哪个主机都没有关系。

    如果这就是您要找的,请告诉我。

    【讨论】:

    • 其实我需要同时使用三个不同的集群和hosts。如果我使用 dev/prod 属性,是同时使用它们还是根据起始类型进行选择?另一种方法,我发现,我可以将 Elasticsearchtemplate 客户端增加三倍,但在这个解决方案中,我需要实现并覆盖所有查找/更新/删除方法。现在,我将项目拆分为每个集群的三个独立项目 :)
    猜你喜欢
    • 2021-05-18
    • 1970-01-01
    • 1970-01-01
    • 2021-08-17
    • 1970-01-01
    • 2017-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多