java - 地址必须解析,但没有解析 - InetSocketAddress#getAddress() 返回 null

标签 java elasticsearch-5 elasticsearch-java-api

当我启动通过elasticsearch java api 与ElasticSearch 5.6.10 交互的java 应用程序并且我的elasticsearch 集群有超过1 个节点时,我收到以下异常:

**

**Caused by: java.lang.IllegalArgumentException: Address must be resolved but wasn't - InetSocketAddress#getAddress() returned null
              at org.elasticsearch.common.transport.InetSocketTransportAddress.<init>(InetSocketTransportAddress.java:48)
              at com.intel.mar.data.elasticsearch.client.TransportClientFactory.createTransportClient(TransportClientFactory.java:30)
              at com.intel.mar.service.config.ServiceConfig.serviceDataManager(ServiceConfig.java:81)
              at com.intel.mar.service.config.ServiceConfig$$EnhancerBySpringCGLIB$$77f6a134.CGLIB$serviceDataManager$2(<generated>)
              at com.intel.mar.service.config.ServiceConfig$$EnhancerBySpringCGLIB$$77f6a134$$FastClassBySpringCGLIB$$b506e141.invoke(<generated>)
              at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
              at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358)
              at com.intel.mar.service.config.ServiceConfig$$EnhancerBySpringCGLIB$$77f6a134.serviceDataManager(<generated>)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
              at java.lang.reflect.Method.invoke(Method.java:498)
              at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
              ... 40 more**

**

elasticsearch.yml

network.bind_host: 0.0.0.0
network.publish_host: "{{getenv "IP"}}"
path.data: /shared/el
cluster.name:  "{{  getv (print "/solutions/" (getenv "COMPONENT_NAMESPACE") "/services/mar-server/es_cluster_name")}}"
cluster.routing.allocation.awareness.attributes: aws_zone_id
node.name: "{{getenv "ID"}}"
node.master: true
node.data: true
discovery.zen.ping.unicast.hosts: [{{getv (print "/solutions/" (getenv "COMPONENT_NAMESPACE") "/services/mar-server/elastic_search_seeds")}}]
discovery.zen.minimum_master_nodes: "{{getv (print "/solutions/" (getenv "COMPONENT_NAMESPACE") "/services/mar-server/es_minimum_master_nodes")}}"
http.cors.allow-origin: "/.*/"
http.cors.enabled: true

# AWS discovery

http.enabled: true
http.port: 9200

action.auto_create_index: true

transport.tcp.port: 9300
indices.fielddata.cache.size: 25%
script.inline: true
thread_pool.bulk.queue_size: 900

我通过以下方式创建elasticsearch java客户端:

    public Client createTransportClient() {
        Settings settings = Settings.builder()
                .put("cluster.name", config.db().clusterName())
                .build();

    return new PreBuiltTransportClient(settings)
                    .addTransportAddress(
                            new InetSocketTransportAddress(
                                    new InetSocketAddress(
                                            config.db().getElasticSearchHost(),
                                            config.db().getElasticSearchNodePort()
                                    )
                            )
                    );
}

最佳答案

问题在于 config.db().getElasticSearchHost() 返回由“,”分隔的主集群节点列表。解决方案如下:

Settings settings = Settings.builder()
                .put("cluster.name", config.db().clusterName())
                .build();

    PreBuiltTransportClient preBuiltTransportClient = new PreBuiltTransportClient(settings);
    String[] hosts = config.db().getElasticSearchHost().split(SPLIT_SEPARATOR);

    for(int i=0; i<hosts.length;i++){
        preBuiltTransportClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hosts[i])
                ,config.db().getElasticSearchNodePort()));
    }

    return preBuiltTransportClient;

关于java - 地址必须解析,但没有解析 - InetSocketAddress#getAddress() 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52339646/

相关文章:

java - 根据第一个 JComboBox 的选择使第二个 JCombobox 可见

java.io.IOException : Server returned HTTP response code: 500 异常

elasticsearch - Elasticsearch :对同一数据运行多个分析器

java - Elasticsearch 子聚合 tophits JAVA API 无法正常工作

java - 在 Elasticsearch Java 客户端中执行 UpdateByQueryRequest

elasticsearch - 从elasticsearch 5.6获取所有文档

java - Maven 项目导入向导不显示子项目 pom

java - 我需要关闭套接字的读取器吗?

elasticsearch - 请求在Elasticsearch中通过ID失败删除记录?

elasticsearch - Elastic Search Cluster Config - 在一个物理上自动发现、配置和多弹性