记一次springboot集成elasticsearch的netty版本冲突问题

1.pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.fzc</groupId>
    <artifactId>articletest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>articletest</name>
    <description>Demo project for Spring Boot</description>

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.0.1</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>




    <!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch -->
    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>5.6.8</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.elasticsearch.client/transport -->
    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>transport</artifactId>
        <version>5.6.8</version>
    </dependency>
</dependencies>


<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

2.es的配置

@Configuration
public class MyConfig {
    
    @Value("${spring.data.elasticsearch.cluster-name}")
    private String clusterName;
    @Value("${spring.data.elasticsearch.cluster-nodes}")
    private String clusterNodes;
    
    @SuppressWarnings("all")
    @Bean
    public TransportClient client() throws UnknownHostException{
        System.out.println("创建client...");
        System.out.println("获取value:"+clusterNodes+"--"+clusterName);
        
        //es集群配置(自定义配置) 连接自己安装的集群名称
        Settings settings = Settings.builder().put("cluster.name",clusterName).build();
        PreBuiltTransportClient client = new PreBuiltTransportClient(settings);
        //es集群连接
        TransportAddress node;
        String[]  nodes = clusterNodes.split(",");
        for (String item : nodes) {
            node = new InetSocketTransportAddress(InetAddress.getByName(item.substring(0,item.indexOf(":"))), Integer.valueOf(item.substring(item.indexOf(":")+1)));
            client.addTransportAddress(node);
            System.out.println("node ="+item+"start:"+item.substring(0,item.indexOf(":"))+"end:"+item.substring(item.indexOf(":")+1));
        }
        return client;
    }
}

运行项目报如下异常

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'articleSearchControlller': Unsatisfied dependency expressed through field 'client'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'client' defined in class path resource [com/fzc/articletest/common/MyConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.elasticsearch.client.transport.TransportClient]: Factory method 'client' threw exception; nested exception is java.lang.NoSuchMethodError: io.netty.util.internal.ObjectUtil.checkPositive(ILjava/lang/String;)I
	at  省略。。。。。。。。

研究异常信息提取重点

 nested exception is java.lang.NoSuchMethodError: io.netty.util.internal.ObjectUtil.checkPositive(ILjava/lang/String;)I

解决办法:
在pom中直接引用netty4.13

     <dependency>
        <groupId>io.netty</groupId>
        <artifactId>netty-all</artifactId>
        <version>4.1.13.Final</version>
    </dependency>

经过引入上面的jar,冲突解决。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值