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,冲突解决。