【问题标题】:Use ElasticSearch with Dropwizard将 ElasticSearch 与 Dropwizard 结合使用
【发布时间】:2016-04-29 15:21:33
【问题描述】:

我正在尝试在 Dropwizard 应用程序中使用 ElasticSearch java API。

我找到了 dropwizard-elasticsearch 包:https://github.com/dropwizard/dropwizard-elasticsearch,这似乎正是我所需要的。 不幸的是,它提供了零个“有用”文档,并且没有使用示例。

我仍然不明白如何使用 TransportClient 连接到远程服务器,因为由于没有 drop wizard-elasticsearch 配置的文档,我应该“随机”尝试,直到找到正确的配置键......

有人尝试过使用 dropwizard-elasticsearch 吗?或者有人有这个的真实使用例子吗?

提前致谢,

【问题讨论】:

  • dropwizard-elasticsearch repo 似乎没有维护

标签: java elasticsearch dropwizard


【解决方案1】:

除非你真的需要加入 Elasticsearch 集群,否则我会避免使用 Elasticsearch 提供的 Java 类。如果您确实以这种方式连接到 Elasticsearch,则需要使 Elasticsearch 和您的应用程序使用的 JVM 版本保持同步。

相反,您可以使用 GitHub 上的 Jest 客户端连接到 Elasticsearch。这将允许您通过 REST 接口连接到 Elasticsearch,就像所有其他客户端库一样。

您需要为 Elasticsearch 创建一个简单的配置块,以指定 REST 接口的 URL。此外,您还需要创建一个 Manager 来启动和停止 JestClient。

更新:您可以在 GitHub 上找到我用于连接到 Elasticsearch 的 Dropwizard 包。以下是 Java 8 的一些基本使用说明:

在项目的 POM 中包含捆绑包的依赖项。

<dependency>
  <groupId>com.meltmedia.dropwizard</groupId>
  <artifactId>dropwizard-jest</artifactId>
  <version>0.1.0</version>
</dependency>

在应用程序配置的某处定义 JestConfiguraion 类。

import com.meltmedia.dropwizard.jest.JestConfiguration;

...

@JsonProperty
protected JestConfiguration elasticsearch;

public JestConfiguration getElasticsearch() {
  return jest;
}

然后在您的应用程序的initialize 方法中包含捆绑包。

import com.meltmedia.dropwizard.jest.JestBundle;

...
protected JestBundle jestBundle;

@Override
public void initialize(Bootstrap<ExampleConfiguration> bootstrap) {
  bootstrap.addBundle(jestBundle = JestBundle.<ExampleConfiguration>builder()
    .withConfiguration(ExampleConfiguration::getElasticsearch)
    .build());
}

最后,使用捆绑包访问客户端供应商。

@Override
public void run(ExampleConfiguration config, Environment env) throws Exception {
  JestClient client = jestBundle.getClientSupplier().get();
}

【讨论】:

    【解决方案2】:

    评论太长了。

    请检查 README.md ->“使用”和“配置”。如果您希望 dropwizard 创建托管 TransportClient,您的配置设置应为 something like this nodeClient: false clusterName: dropwizard_elasticsearch_test servers: - 127.23.42.1:9300 - 127.23.42.2:9300 - 127.23.42.3

    如何获取dropwizard管理的TransportClient?此处示例:public void transportClientShouldBeCreatedFromConfig()

    @Override public void run(DemoConfiguration config, Environment environment) { final ManagedEsClient managedClient = new ManagedEsClient(configuration.getEsConfiguration()); Client client = managedClient.getClient(); ((TransportClient) client).transportAddresses().size(); // [...] }

    还有一个sample blog application 使用 Dropwizard 和 ElasticSearch。请参阅 README.md 中的“致谢”部分。

    【讨论】:

      【解决方案3】:

      我已将 Java api 用于 elasticsearch,问题是您正在使用的捆绑包,我也探索过,但文档部分不鼓励使用它。在这里,您可以在没有此捆绑包的情况下使用弹性:-

      1. 在 .yml 文件中定义弹性配置。

        elasticsearchHost: 127.0.0.1
        elasticPort: 9300
        clusterName: elasticsearch
        
      2. 现在在配置文件中(在我的例子中是 mkApiConfiguration)创建静态函数,这实际上是这些弹性配置的 getter 方法:-

        @NotNull
        private static String elasticsearchHost;
        
        @NotNull
        private static Integer elasticPort;
        
        @NotNull
        private static String clusterName;
        
        @JsonProperty
        public static String getElasticsearchHost() {
            return elasticsearchHost;
        }
        //This function will be called while reading configurations from yml file
        @JsonProperty
        public void setElasticsearchHost(String elasticsearchHost) {
            mkApiConfiguration.elasticsearchHost = elasticsearchHost;
        }
        
        @JsonProperty
        public void setClusterName(String clusterName) {
            mkApiConfiguration.clusterName = clusterName;
        }
        
        public void setElasticPort(Integer elasticPort) {
            mkApiConfiguration.elasticPort = elasticPort;
        }
        
        @JsonProperty
        public static String getClusterName() {
            return clusterName;
        }
        
        @JsonProperty
        public static Integer getElasticPort() {
            return elasticPort;
        }
        
      3. 现在创建一个弹性工厂,您可以从中获取传输客户端,更好地将其创建为单例类,以便为弹性配置创建和共享一个实例,我们可以使用 getter 方法获取配置配置类,因为这些是静态方法,所以我们不需要创建对象来访问这些方法。所以这个工厂的代码是这样的

        public class ElasticFactory {
        
        //Private constructor
        private ElasticFactory(){};
        public static Client getElasticClient(){
            try {     
                 /*
                 * Creating Transport client Instance
                 */
                 Client client = TransportClient.builder().build()
                   .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(mkApiConfiguration.getElasticsearchHost()), mkApiConfiguration.getElasticPort()));
            return client;
        }
        catch (Exception e){
            e.printStackTrace();
            return null;
        }
        

        }

      4. 现在您可以从任何类中调用此弹性工厂方法,如下所示:-

        /*As getInstance is a static method so we can access it directly without creating an object of ElasticFactory class */ Client elasticInstance= ElasticFactory.getElasticClient();

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-02
        • 2022-01-14
        • 2015-01-01
        相关资源
        最近更新 更多