嵌入式ElasticSearch无法在集成测试中连接到传输端口

时间:2016-10-14 08:54:50

标签: elasticsearch integration-testing

我正在尝试创建用于集成测试的嵌入式ElasticSearch节点。

以下是代码创建

private static final String THREAD_NAME = "ES-THREAD";
private static final String CLUSTER_NAME = "ES-INTEGRATION-TEST";
private static final String ES_HOME_PATH = "elastic-search-home";
private static final String ES_DATA_PATH = "elastic-search-data";
private static final String DATA_PORTS = "9500-9599";
private static final String TRANSPORT_PORTS = "9600-9699";
public void before() throws Throwable {
        try {
            homeDir = Files.createTempDirectory(ES_HOME_PATH);
            dataDir = Files.createTempDirectory(ES_DATA_PATH);

            log.info("Created temp directory {} and {}", homeDir, dataDir);
        } catch (IOException ex) {
            throw new IllegalStateException("Temp Elastic Search directory not created", ex);
        }

        Properties props = new Properties();

        props.setProperty("name", THREAD_NAME);
        props.setProperty("path.home", homeDir.toString());
        props.setProperty("path.data", dataDir.toString());
        props.setProperty("http.port", DATA_PORTS);
        props.setProperty("transport.tcp.port", TRANSPORT_PORTS);
        props.setProperty("node.local", "true");
        props.setProperty("script.groovy.sandbox.enabled", "true");
        props.setProperty("script.engine.groovy.inline.aggs", "true");
        props.setProperty("script.engine.groovy.inline.search", "true");
        props.setProperty("script.engine.groovy.inline.update", "true");
        props.setProperty("script.engine.groovy.inline.mapping", "true");


        esNode = NodeBuilder.nodeBuilder().local(false).client(false)
                .settings(Settings.settingsBuilder().put(props).build()).clusterName(CLUSTER_NAME).build();

        esNode.start();
}

在测试的代码中,有以下方法创建到ElasticSearch的传输连接

private Client createClient() throws UnknownHostException {
    Settings.Builder builder = Settings.builder();
    builder.put("cluster.name", clusterName);
    builder.put("client.transport.ignore_cluster_name", true);

    Settings settings = builder.build();
    return TransportClient.builder().settings(settings).build()
            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostname), port));
}

当我进行测试时,我收到了例外情况

java.net.BindException: Can't assign requested address
    at sun.nio.ch.Net.connect0(Native Method)
    at sun.nio.ch.Net.connect(Net.java:454)
    at sun.nio.ch.Net.connect(Net.java:446)
    at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:648)
    at org.jboss.netty.channel.socket.nio.NioClientSocketPipelineSink.connect(NioClientSocketPipelineSink.java:108)
    at org.jboss.netty.channel.socket.nio.NioClientSocketPipelineSink.eventSunk(NioClientSocketPipelineSink.java:70)
    at org.jboss.netty.channel.DefaultChannelPipeline.sendDownstream(DefaultChannelPipeline.java:574)
    at org.jboss.netty.channel.Channels.connect(Channels.java:634)
    at org.jboss.netty.channel.AbstractChannel.connect(AbstractChannel.java:216)
    at org.jboss.netty.bootstrap.ClientBootstrap.connect(ClientBootstrap.java:229)
    at org.jboss.netty.bootstrap.ClientBootstrap.connect(ClientBootstrap.java:182)
    at org.elasticsearch.transport.netty.NettyTransport.connectToChannelsLight(NettyTransport.java:913)
    at org.elasticsearch.transport.netty.NettyTransport.connectToNode(NettyTransport.java:880)
    at org.elasticsearch.transport.netty.NettyTransport.connectToNodeLight(NettyTransport.java:852)
    at org.elasticsearch.transport.TransportService.connectToNodeLight(TransportService.java:250)
    at org.elasticsearch.client.transport.TransportClientNodesService$SimpleNodeSampler.doSample(TransportClientNodesService.java:354)
    at org.elasticsearch.client.transport.TransportClientNodesService$NodeSampler.sample(TransportClientNodesService.java:300)
    at org.elasticsearch.client.transport.TransportClientNodesService$ScheduledNodeSampler.run(TransportClientNodesService.java:333)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

我为测试代码发送的主机名值是localhost,我通过

发送我从嵌入式ElasticSearch中提取的端口
NodeInfo nodeInfo = esNode.client().admin().cluster().prepareNodesInfo(localNodeId).get().iterator().next();
transportAddress = nodeInfo.getTransport().address().publishAddress().getAddress();

我看到传输端口始终为0,当我评估nodeInfo.getTransport()。address()时,它的值是local [1]。

节点创建中的错误是什么? 我需要添加其他配置吗?

谢谢,

丹妮拉

1 个答案:

答案 0 :(得分:0)

当我改变props.setProperty(" node.local"," true"); to props.setProperty(" node.local"," false");传输端口已创建。