当前位置: 首页 > 知识库问答 >
问题:

Spring elasticsearch java。网未知后异常

徐德海
2023-03-14

尝试更新一个小型实验性Spring Elasticsearch项目,以反映3.2.6版本参考指南中的更改。

如前所述:TransportClient从Elasticsearch 7开始被弃用,并将在8中被删除。尝试重新配置到高级Rest客户端。

但是得到以下错误

    Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'elasticDatasource' defined in file [/Users/erichuang/Desktop/JE/00-Development/dev/lab/lab-elastic-search/lab-elastic-search/target/classes/com/elastic/labelasticsearch/config/ElasticDatasource.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'carElasticRepo': Invocation of init method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.elasticsearch.repository.support.SimpleElasticsearchRepository]: Constructor threw exception; nested exception is org.springframework.data.elasticsearch.ElasticsearchException: Error while for indexExists request: org.elasticsearch.action.admin.indices.get.GetIndexRequest@b5d8ebb
    org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
    com.elastic.labelasticsearch.LabElasticSearchApplication.main(LabElasticSearchApplication.java:15)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'carElasticRepo': Invocation of init method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.elasticsearch.repository.support.SimpleElasticsearchRepository]: Constructor threw exception; nested exception is org.springframework.data.elasticsearch.ElasticsearchException: Error while for indexExists request: org.elasticsearch.action.admin.indices.get.GetIndexRequest@b5d8ebb
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1796)

    ... 24 common frames omitted

Caused by: java.net.UnknownHostException: localhost/<unresolved>: nodename nor servname provided, or not known
at java.base/java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.base/java.net.InetAddress$PlatformNameService.lookupAllHostAddr(InetAddress.java:932)
at java.base/java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1505)
at java.base/java.net.InetAddress$NameServiceAddresses.get(InetAddress.java:851)
at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1495)
at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1354)
at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1288)
...
... 63 common frames omitted

旧设置(有效)

弹性搜索配置

@Configuration
@EnableElasticsearchRepositories(basePackages = "com.elastic.labelasticsearch.repo")
public class ElasticSearchConfig  {
    private static final String esHost = "localhost";
    private static final int esPort = 9300;

    @Bean
    public Client client() throws UnknownHostException {
        var transportClient = new PreBuiltTransportClient(Settings.EMPTY);
        transportClient.addTransportAddress(new TransportAddress(InetAddress.getByName(esHost), esPort));

        return transportClient;
    }

    @Bean(name ={"elasticsearchOperations", "elasticsearchTemplate"})
    public ElasticsearchOperations esTemplate() throws UnknownHostException {
        return new ElasticsearchTemplate(client());
    }

    @Bean
    public RestTemplate restTemplate(){
       return new RestTemplate();
    }

}

新设置

@Configuration
@EnableElasticsearchRepositories(basePackages = "com.elastic.labelasticsearch.repo")
public class RestClientConfig extends AbstractElasticsearchConfiguration {

    @Override
    @Bean
    public RestHighLevelClient elasticsearchClient() {

        final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
                .connectedTo("localhost:9300")
                .build();

        return RestClients.create(clientConfiguration).rest();
    }

}

我错过了什么?

共有2个答案

鄢博简
2023-03-14

您需要使用以下方式配置您的bean,允许分离使用的主机端口和超文本传输协议:

@Configuration
@EnableElasticsearchRepositories(basePackages = "*")
public class ElasticsearchClientConfig {

    @Value("${elasticsearch.host}")
    private String host;

    @Value("${elasticsearch.port}")
    private int port;

    @Value("${elasticsearch.protocol}")
    private String protocol;

    @Value("${elasticsearch.username}")
    private String userName;

    @Value("${elasticsearch.password}")
    private String password;

    @Bean(destroyMethod = "close")
    public RestHighLevelClient restClient() {

        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));

        RestClientBuilder builder = RestClient.builder(new HttpHost(host, port, protocol)).setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));

        RestHighLevelClient client = new RestHighLevelClient(builder);
    
        return client;
    }
}
蒲功
2023-03-14

这是Java中Java 14的as更改导致的错误。网InetSocketAddress。toString()方法。这已在4.0中修复。RC2(昨天发布)。

编辑:我将把它也向后移植到3.2分支中

 类似资料:
  • 我收到这个错误消息"java.net.未知主机异常:无法解决主机"api.themoviedb.org":没有与主机名相关联的地址"。我读到的所有stackoverflow帖子都说清单文件可能是错误的,但我认为在这一点上是正确的,所以我不确定如何继续。 AndroidManifest。xml: MainActivity.java: }

  • 我有一个使用AWS Fargate部署的Spring Boot映像和使用AWS Elasticsearch Service的Elasticsearch集群。两者都在同一个VPC和子网下。以下是Elasticsearch的访问策略: 安全组:Fargate:sg-test033f776d5fbed5c0000 子网: Fargate: Elasticsearch: 在elasticsearch的安全

  • 我不知道如何为所有文件类型添加自定义元数据,如txt,doc,docx,xls,xlsx,ppt,pptx,pdf等。我已经尝试使用文件类setAttribute()方法为txt文件添加自定义元数据,但我得到了错误。 我没有得到我要错的地方...我得到了下面的错误

  • 问题内容: 我正在尝试通过android应用程序中的HTTP Post请求访问textalertapp.com。但是我收到了未知主机错误。谁能帮我解决这个问题。 代码是 问题答案: 我只是在清单文件中添加了这一行。问题解决了

  • 我在应用程序中使用了Spring3和Hibernate4。在运行时,我得到的异常值低于异常值。

  • 我的机器运行的是hbase-0.94.16服务器,因此当我尝试使用hbase definitive primitive guide中的文档来安装hbase hush服务器时,它出现了以下异常 14/05/22 11:03:43信息zookeeper.zookeeper:客户端环境:java.io.tmpdir=/tmp 14/05/22 11:03:43信息zookeeper.zookeeper: