在弹性搜索TransportClient启动期间,Spring Boot引发Netty4HttpServerTransport异常

时间:2019-03-07 21:45:35

标签: java spring-boot elasticsearch

我正在尝试使用Spring Boot(版本2)将我的项目转换为基于REST的项目。原始项目曾经使用弹性搜索(6.6.1)进行文本搜索,并且按预期方式工作。然后,在“ spring boot starter网站项目”中,我引入了RESTController来调用上述TextSearch类

请参阅相关课程

public class TextSearch {
    private final TransportClient client;
    public static final Logger LOGGER = LoggerFactory.getLogger(TextSearch.class);

    public TextSearch() {
        Settings settings = Settings.builder().put("cluster.name", "elasticsearch").put("client.transport.sniff", true).build();
        client= new PreBuiltTransportClient(settings);
        try {
            client.addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"), 9300));
        }catch (UnknownHostException e) {
            LOGGER.error("Unknown host exception {}", e);
        }
    }
}
// spring rest controller class
@RestController
public class SearchController {


    @RequestMapping("/search")
    public String search(@RequestParam(value="name", defaultValue="World") String name) {

        List<SCMappedData> mappedList = FetchScDataJsoup.fetch();
   System.out.println("#######no of entries" + mappedList.size());
        TextSearch textSearch = new TextSearch("legal", "sc");
        boolean indexCreation = textSearch.createIndex(mappedList);

        List<SearchHit> hits = textSearch.search(name);
                for (SearchHit hit:hits
             ) {
            System.out.println(hit.getSourceAsString());
        }
               if(hits.size() > 0) {
                   return hits.get(0).getExplanation().getDescription();
               } else {
                   return " nt found";
               }

                }
}

但是get请求失败,并出现以下错误

java.lang.NoSuchFieldError: Shared
    at org.elasticsearch.http.netty4.Netty4HttpServerTransport.<clinit>(Netty4HttpServerTransport.java:124) ~[transport-netty4-client-5.6.11.jar:5.6.11]
    at org.elasticsearch.transport.Netty4Plugin.getSettings(Netty4Plugin.java:55) ~[transport-netty4-client-5.6.11.jar:5.6.11]
    at org.elasticsearch.plugins.PluginsService.lambda$getPluginSettings$0(PluginsService.java:89) ~[elasticsearch-6.6.1.jar:6.6.1]
    at java.base/java.util.stream.ReferencePipeline$7$1.accept(ReferencePipeline.java:271) ~[na:na]
    at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1492) ~[na:na]
    at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484) ~[na:na]
    at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474) ~[na:na]
    at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913) ~[na:na]
    at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:na]
    at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578) ~[na:na]
    at org.elasticsearch.plugins.PluginsService.getPluginSettings(PluginsService.java:89) ~[elasticsearch-6.6.1.jar:6.6.1]
    at org.elasticsearch.client.transport.TransportClient.buildTemplate(TransportClient.java:147) ~[elasticsearch-6.6.1.jar:6.6.1]
    at org.elasticsearch.client.transport.TransportClient.<init>(TransportClient.java:288) ~[elasticsearch-6.6.1.jar:6.6.1]
    at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:128) ~[transport-6.6.1.jar:6.6.1]
    at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:114) ~[transport-6.6.1.jar:6.6.1]
    at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:104) ~[transport-6.6.1.jar:6.6.1]
    at search.TextSearch.<init>(TextSearch.java:40) ~[classes/:na]
    at run.SearchController.search(SearchController.java:25) ~[classes/:na]

我知道我可能需要提供更多详细信息才能得到答案,但是我不确定我必须提供哪些详细信息。

这是我第一次尝试使用Spring项目。因此,我想知道是否有人可以对此提供高水平的建议。

1 个答案:

答案 0 :(得分:1)

查看异常堆栈跟踪,ElasticSearch版本与netty客户端不匹配。因此,请使用相同的版本。

<dependency>
    <groupId>org.elasticsearch.plugin</groupId>
    <artifactId>transport-netty4-client</artifactId>
    <version>6.6.1</version>
</dependency>