首页 > 解决方案 > Elasticsearch 和 jhipster

问题描述

我正在使用 jhipster 生成的应用程序和 MySQL 数据库。我的应用程序在 Ubuntu 18.04 上使用 nginx 部署在 tomcat 上(使用 mvnw 包 -Pprod 生成的 .war 文件)。为了部署,我使用了 .war.orig 文件。

在 Ubuntu 服务器上,我安装了 elasticsearch,就像这里的指南中描述的那样(我使用了 elasticsearch 6.3 版):https ://www.digitalocean.com/community/tutorials/how-to-install-and-configure-elasticsearch-在 ubuntu-16-04

而 /etc/elasticsearch/elasticsearch.yml 文件如下所示:

network.host: localhost
http.port: 9200

在 application-prod.yml 我有以下弹性搜索配置:

    data:
    elasticsearch:
        cluster-name:
        cluster-nodes: localhost:9200

当我在 tomcat 上部署应用程序时,会发生以下错误:

2018-06-17 22:58:49.675 ERROR 28733 --- [1-8080-exec-161] o.z.p.spring.web.advice.AdviceTrait      : Internal Server Error

org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: [{#transport#-1}{localhost}{127.0.0.1:9300}]

应用程序正在运行,但对后端的每个请求(登录除外)都会导致内部服务器错误 500。

该命令curl -X GET 'http://localhost:9200'给出了正确的返回。

我是否配置错误?

标签: javaspringspring-bootelasticsearchjhipster

解决方案


你能试试这个:

data:
    elasticsearch:
        cluster-name: <cluster_name>
        cluster-nodes: <usually it is same as cluster name, default qSea>
        properties:
            host: <ip address>
            user: <username:password>
            port: 9300
            enableSsl: false
            path:
                logs: target/elasticsearch/log
                data: target/elasticsearch/data

另外,我猜您正在通过传输层进行通信。您可以使用此方法构建传输客户端(如果未安装 x-pack,您可以更改它):

private static TransportClient buildTransPortClient() {

    String host = "<IP Address>";
    int port = 9300; // port is 9300 not 9200

    Settings settings = Settings.builder().put("client.transport.nodes_sampler_interval", "5s")
            .put("client.transport.sniff", false).put("transport.tcp.compress", true).put("cluster.name", "<cluster_name>")
            .put("xpack.security.transport.ssl.enabled", false).put("request.headers.X-Found-Cluster", "<cluster_name>")
            .put("xpack.security.user", "<user_name and password. by default it is elastic:changeme (of course if you have installed x-pack)>").build();

    TransportClient client = null;

    try {
        client = new PreBuiltXPackTransportClient(settings);

        for (InetAddress address : InetAddress.getAllByName(host)) {
            if ((address instanceof Inet6Address) || (address instanceof Inet4Address)) {
                client.addTransportAddress(new InetSocketTransportAddress(address, port));
            }
        }
    } catch (UnknownHostException e) {
        System.out.println("Unable to get the host" + e.getMessage());
    } catch (Exception e) {
        System.out.println("exception aaya hai");
        e.printStackTrace();
    }

    return client;
}

然后可以测试。如果这有效,那么您的 jhipster 应用程序与 elasticsearch 一起工作得很好。


推荐阅读