错误呈现

测试代码如下

@Test
    public void indexData() throws IOException {
        IndexRequest request = new IndexRequest("users");
        request.id("1");
        //request.source("user", "kimchy","postDate", new Date(),"message", "trying out Elasticsearch");
        User user = new User();
        user.setUserName("zhangsan");
        user.setAge(20);
        user.setGender("男");
        String josnString = JSON.toJSONString(user);
        request.source(josnString, XContentType.JSON);  //要保存的内容

        //执行操作
        IndexResponse index = client.index(request, GulimallElasticSearchConfig.COMMON_OPTIONS);
        System.out.println(index);
        //提取有用的响应数据
    }

代码是没什么问题的,但是报了一个错误

java.net.ConnectException: Connection refused: no further information

	at org.elasticsearch.client.RestClient.extractAndWrapCause(RestClient.java:788)
	at org.elasticsearch.client.RestClient.performRequest(RestClient.java:218)
	at org.elasticsearch.client.RestClient.performRequest(RestClient.java:205)
	at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1454)
	at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1424)
	at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1394)
	at org.elasticsearch.client.RestHighLevelClient.index(RestHighLevelClient.java:836)
	at com.oyz.gulimall.search.GulimallSearchApplicationTests.indexData(GulimallSearchApplicationTests.java:43)
	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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

异常处理

配置文件中添加 elasticsearch.rest.uris
spring:
  data:
    elasticsearch:
      cluster-name: docker-cluster
      cluster-nodes: 60.205.1.158:9300
      repositories:
        enabled: true
    # 异常处理
  elasticsearch:
    rest:
      uris: 60.205.1.158:9200
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐