es启动异常

424 阅读1分钟

记录一次es启动异常的问题

版本

springboot:2.1.7

es:6.3.2

jar:6.2.3

依赖

            <dependency>
                <groupId>org.elasticsearch.client</groupId>
                <artifactId>elasticsearch-rest-high-level-client</artifactId>
                <version>6.2.3</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.elasticsearch</groupId>
                        <artifactId>elasticsearch</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
​
            <dependency>
                <groupId>org.elasticsearch.client</groupId>
                <artifactId>elasticsearch-rest-client</artifactId>
                <version>6.2.3</version>
            </dependency>
​
            <dependency>
                <groupId>org.elasticsearch</groupId>
                <artifactId>elasticsearch</artifactId>
                <version>6.2.3</version>
            </dependency>

dev环境启动异常

异常信息

Caused by: java.lang.NoSuchMethodError: org.elasticsearch.client.Request.<init>(Ljava/lang/String;Ljava/lang/String;)V
        at org.springframework.boot.actuate.elasticsearch.ElasticsearchRestHealthIndicator.doHealthCheck(ElasticsearchRestHealthIndicator.java:60)
        at org.springframework.boot.actuate.health.AbstractHealthIndicator.health(AbstractHealthIndicator.java:82)
        at org.springframework.boot.actuate.health.CompositeHealthIndicator.health(CompositeHealthIndicator.java:95)
        at org.springframework.boot.actuate.health.HealthEndpoint.health(HealthEndpoint.java:50)
        at org.springframework.boot.actuate.health.HealthEndpointWebExtension.health(HealthEndpointWebExtension.java:53)
        at sun.reflect.GeneratedMethodAccessor306.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:282)
        at org.springframework.boot.actuate.endpoint.invoke.reflect.ReflectiveOperationInvoker.invoke(ReflectiveOperationInvoker.java:76)
        at org.springframework.boot.actuate.endpoint.annotation.AbstractDiscoveredOperation.invoke(AbstractDiscoveredOperation.java:60)
        at org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$ServletWebOperationAdapter.handle(AbstractWebMvcEndpointHandlerMapping.java:278)
        at org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(AbstractWebMvcEndpointHandlerMapping.java:334)
        at sun.reflect.GeneratedMethodAccessor305.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
        at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
        at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797)
        at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1039)
        ... 65 common frames omitted

异常原因

springboot启动es的时候会对es进行健康检测,代码在ElasticsearchRestHealthIndicator类中。

 Response response = this.client.performRequest(new Request("GET", "/_cluster/health/"));
 StatusLine statusLine = response.getStatusLine();

springboot中es的Request对象版本较高,有两个参数的构造函数,但是es【6.2.3的jar】中Request对象只有四个参数的构造函数。于是抛出NoSuchMethodError异常信息。

解决方法

关闭健康检测

@Configuration
@ConditionalOnClass({Client.class})
@ConditionalOnBean({Client.class})
@ConditionalOnEnabledHealthIndicator("elasticsearch")
@AutoConfigureBefore({HealthIndicatorAutoConfiguration.class})
@AutoConfigureAfter({ElasticsearchAutoConfiguration.class})
@EnableConfigurationProperties({ElasticsearchHealthIndicatorProperties.class})
public class ElasticSearchClientHealthIndicatorAutoConfiguration extends CompositeHealthIndicatorConfiguration<ElasticsearchHealthIndicator, Client> {
}
@ConfigurationProperties(
    prefix = "management.health.elasticsearch",
    ignoreUnknownFields = false
)
public class ElasticsearchHealthIndicatorProperties {
}

在properties文件中加上management.health.elasticsearch.enabled=false

举一反三

当使用的es版本低于springboot默认的版本时,都有可能出现这种情况。