Jest:ProtocolException:未指定目标主机

时间:2016-09-12 09:26:32

标签: jestjs

这是我第一次使用Jest for Elasticsearch。我使用以下代码:

  public static JestClient getJestClient(ContainerRequestContext crc) {

            JSONObjeect elasticsearchServers = new JSONObject();
            elasticsearchServers.putAll((Map) crc.getProperty("serverProperties"));
            List<String> connectionUrls = new ArrayList<String>();

            Set serverSet = elasticsearchServers.entrySet();
            Iterator serverIterator = serverSet.iterator();
            while (serverIterator.hasNext()) {
                Map.Entry pair = (Map.Entry) serverIterator.next();
                connectionUrls.add(String.valueOf(pair.getValue()));
            }
            return buildJestClient(connectionUrls);
        }


    public static JestClient buildJestClient(List<String> connectionUrls) {
        final Builder builder = new Builder(connectionUrls)
                .multiThreaded(true);

        builder.gson(JestRichResult.createGsonWithDateFormat());
        HttpClientConfig clientConfig = builder.build();

        // Construct a new Jest client according to configuration via factory
        JestClientFactory factory = new JestClientFactory();
        factory.setHttpClientConfig(clientConfig);
        return factory.getObject();
    }

我正在尝试创建索引:

CreateIndex createIndex = new CreateIndex.Builder("inde").build();
jestClient.execute(createIndex);

但我面临以下错误:

org.apache.http.ProtocolException: Target host is not specified

我的Elasticsearch服务正在所有服务器上运行。

这是我的connectionUrls ips列表:[1.2.3.4,5.6.7.8,10.11.12.13]

我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

我做错了是,我只提供服务器的IP地址,但它应该是完整地址,如:

http:// 1.2.3.4:9200

所以我更改了添加connectionUrls的行 connectionUrls.add("http://"+String.valueOf(pair.getValue())+":9200");