springBoot集成Elasticsearch 报错 Health check failed的解决

这篇文章主要介绍了springBoot集成Elasticsearch 报错 Health check failed的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

springBoot集成Elasticsearch 报错 Health check failed

今天集成Elasticsearch 时启动报错

报错信息如下:

2018-11-01 20:52:51.310 INFO [hstao-supersearch,,,] 8528 --- [ main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8009
2018-11-01 20:52:51.314 INFO [hstao-supersearch,,,] 8528 --- [ main] c.i.g.hstaoSupersearchApplication : Started hstaoSupersearchApplication in 32.467 seconds (JVM running for 34.971)
2018-11-01 20:52:53.729 WARN [hstao-supersearch,,,] 8528 --- [on(6)-127.0.0.1] s.b.a.h.ElasticsearchJestHealthIndicator : Health check failed
org.apache.http.conn.HttpHostConnectException: Connect to localhost:9200 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:159) ~[httpclient-4.5.3.jar:4.5.3]
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:359) ~[httpclient-4.5.3.jar:4.5.3]
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:381) ~[httpclient-4.5.3.jar:4.5.3]
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:237) ~[httpclient-4.5.3.jar:4.5.3]
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185) ~[httpclient-4.5.3.jar:4.5.3]
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89) ~[httpclient-4.5.3.jar:4.5.3]
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111) ~[httpclient-4.5.3.jar:4.5.3]
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185) ~[httpclient-4.5.3.jar:4.5.3]
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83) ~[httpclient-4.5.3.jar:4.5.3]
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108) ~[httpclient-4.5.3.jar:4.5.3]
at io.searchbox.client.http.JestHttpClient.execute(JestHttpClient.java:50) ~[jest-0.1.7.jar:na]
at org.springframework.boot.actuate.health.ElasticsearchJestHealthIndicator.doHealthCheck(ElasticsearchJestHealthIndicator.java:44) ~[spring-boot-actuator-1.5.6.RELEASE.jar:1.5.6.RELEASE]
at org.springframework.boot.actuate.health.AbstractHealthIndicator.health(AbstractHealthIndicator.java:43) ~[spring-boot-actuator-1.5.6.RELEASE.jar:1.5.6.RELEASE]
at org.springframework.boot.actuate.health.CompositeHealthIndicator.health(CompositeHealthIndicator.java:68) [spring-boot-actuator-1.5.6.RELEASE.jar:1.5.6.RELEASE]
at org.springframework.boot.actuate.endpoint.HealthEndpoint.invoke(HealthEndpoint.java:85) [spring-boot-actuator-1.5.6.RELEASE.jar:1.5.6.RELEASE]
at org.springframework.boot.actuate.endpoint.HealthEndpoint.invoke(HealthEndpoint.java:35) [spring-boot-actuator-1.5.6.RELEASE.jar:1.5.6.RELEASE]
at org.springframework.boot.actuate.endpoint.jmx.DataEndpointMBean.getData(DataEndpointMBean.java:46) [spring-boot-actuator-1.5.6.RELEASE.jar:1.5.6.RELEASE]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71) [na:1.8.0_121]
at sun.reflect.GeneratedMethodAccessor131.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275) [na:1.8.0_121]
at javax.management.modelmbean.RequiredModelMBean$4.run(RequiredModelMBean.java:1252) [na:1.8.0_121]
at java.security.AccessController.doPrivileged(Native Method) [na:1.8.0_121]
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80) [na:1.8.0_121]
at javax.management.modelmbean.RequiredModelMBean.invokeMethod(RequiredModelMBean.java:1246) [na:1.8.0_121]
at javax.management.modelmbean.RequiredModelMBean.invoke(RequiredModelMBean.java:1085) [na:1.8.0_121]
at org.springframework.jmx.export.SpringModelMBean.invoke(SpringModelMBean.java:90) [spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at javax.management.modelmbean.RequiredModelMBean.getAttribute(RequiredModelMBean.java:1562) [na:1.8.0_121]
at org.springframework.jmx.export.SpringModelMBean.getAttribute(SpringModelMBean.java:109) [spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getAttribute(DefaultMBeanServerInterceptor.java:647) [na:1.8.0_121]
at com.sun.jmx.mbeanserver.JmxMBeanServer.getAttribute(JmxMBeanServer.java:678) [na:1.8.0_121]
at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1445) [na:1.8.0_121]
at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76) [na:1.8.0_121]
at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309) [na:1.8.0_121]
at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1401) [na:1.8.0_121]
at javax.management.remote.rmi.RMIConnectionImpl.getAttribute(RMIConnectionImpl.java:639) [na:1.8.0_121]
at sun.reflect.GeneratedMethodAccessor58.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:346) [na:1.8.0_121]
at sun.rmi.transport.Transport$1.run(Transport.java:200) [na:1.8.0_121]
at sun.rmi.transport.Transport$1.run(Transport.java:197) [na:1.8.0_121]
at java.security.AccessController.doPrivileged(Native Method) [na:1.8.0_121]
at sun.rmi.transport.Transport.serviceCall(Transport.java:196) [na:1.8.0_121]
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568) [na:1.8.0_121]
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826) [na:1.8.0_121]
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683) [na:1.8.0_121]
at java.security.AccessController.doPrivileged(Native Method) [na:1.8.0_121]
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682) [na:1.8.0_121]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) ~[na:1.8.0_121]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) ~[na:1.8.0_121]
at java.lang.Thread.run(Thread.java:745) ~[na:1.8.0_121]
Caused by: java.net.ConnectException: Connection refused: connect

在报错日志中看到 Connect to localhost:9200 被拒绝,检查代码发现项目中并没有 localhost:9200 的相关配置项,继续看上一步的日志

s.b.a.h.ElasticsearchJestHealthIndicator : Health check failed

可以看到执行了es的healthCheck,联想到以往集成redis也有healthCheck,便增加如下配置来试图关掉healthCheck

management.security.health.elasticsearch.enabled=false

竟然成功了,意外收获,Mark一下!

SpringBoot集成ElasticSearch出现的异常

1. 异常

在使用springboot2.2.8+elasticsearch6.8.10时,测试时报错:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'elasticsearchClient' defined in class path resource [org/springframework/boot/autoconfigure/data/elasticsearch/ElasticsearchAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.elasticsearch.client.transport.TransportClient]: Factory method 'elasticsearchClient' threw exception; nested exception is java.lang.IllegalStateException: availableProcessors is already set to [4], rejecting [4]

2. 原因

Elasticsearch 和 Redis 底层都使用到了 Netty , 在项目启动时会冲突。

涉及到的类:NettyRuntime,Netty4Util。

查看NettyRuntime类,可以看下源码:

public final class NettyRuntime {
    private static final NettyRuntime.AvailableProcessorsHolder holder = new NettyRuntime.AvailableProcessorsHolder();
    public static void setAvailableProcessors(int availableProcessors) {
        holder.setAvailableProcessors(availableProcessors);
    }
    public static int availableProcessors() {
        return holder.availableProcessors();
    }
    private NettyRuntime() {
    }
    static class AvailableProcessorsHolder {
        private int availableProcessors;
        AvailableProcessorsHolder() {
        }
        synchronized void setAvailableProcessors(int availableProcessors) {
            ObjectUtil.checkPositive(availableProcessors, "availableProcessors");
            // 简单说明:在项目启动时,redis自动设置好Netty处理器(availableProcessors就不为0),而此时elasticsearch也启动,发现Netty处理器已经被设置好了(发现availableProcessors!=0)然后会报异常。无论哪一个先启动,都会有判断去报这样的异常
            if (this.availableProcessors != 0) {
                // 看到这一句:跟上面报错的格式是一样的
                String message = String.format(Locale.ROOT, "availableProcessors is already set to [%d], rejecting [%d]", this.availableProcessors, availableProcessors);
                throw new IllegalStateException(message);
            } else {
                this.availableProcessors = availableProcessors;
            }
        }
        @SuppressForbidden(
            reason = "to obtain default number of available processors"
        )
        synchronized int availableProcessors() {
            if (this.availableProcessors == 0) {
                int availableProcessors = SystemPropertyUtil.getInt("io.netty.availableProcessors", Runtime.getRuntime().availableProcessors());
                this.setAvailableProcessors(availableProcessors);
            }
            return this.availableProcessors;
        }
    }
}

而 Elasticsearch 底层使用了 Netty4Util ,这个类调用NettyRuntime的方法:

  public static void setAvailableProcessors(final int availableProcessors) {
        // we set this to false in tests to avoid tests that randomly set processors from stepping on each other
      // 而这里就是解决办法:
        final boolean set = Booleans.parseBoolean(System.getProperty("es.set.netty.runtime.available.processors", "true"));
        if (!set) {
            return;
        }
        /*
         * This can be invoked twice, once from Netty4Transport and another time from Netty4HttpServerTransport; however,
         * Netty4Runtime#availableProcessors forbids settings the number of processors twice so we prevent double invocation here.
         */
        if (isAvailableProcessorsSet.compareAndSet(false, true)) {
            // 看着,回去调用NettyRuntime的setAvailableProcessors
            NettyRuntime.setAvailableProcessors(availableProcessors);
        } else if (availableProcessors != NettyRuntime.availableProcessors()) {
            /*
             * We have previously set the available processors yet either we are trying to set it to a different value now or there is a bug
             * in Netty and our previous value did not take, bail.
             */
            // 看下面的格式跟报错的格式一样
            final String message = String.format(
                    Locale.ROOT,
                    "available processors value [%d] did not match current value [%d]",
                    availableProcessors,
                    NettyRuntime.availableProcessors());
            throw new IllegalStateException(message);
        }
    }

3. 解决

在Netty4Util源码可以看到,配置es.set.netty.runtime.available.processors设置为false就不会去检查Netty处理器是否配置。

因为得启动的时候就得去解决冲突,所以设置在 启动类那

@SpringBootApplication
public class XXXApplication {
    @PostConstruct
    public void init() {
        // 解决netty启动冲突的问题(主要体现在启动redis和elasticsearch)
        // 可以看Netty4Util.setAvailableProcessors(..)
        System.setProperty("es.set.netty.runtime.available.processors", "false");
    }
    public static void main(String[] args) {
        SpringApplication.run(CommunityApplication.class, args);
    }
}

配置在main函数,调用run方法前也可以。

这个问题搞我一天时间,当时是直接copy Netty4Util类的System… , 没发现它是getProperty,导致还是出现这个异常,我真的不知道咋解决了。所以要看清楚,这里是setProperty

4. 其他注意点

一定要让本地的ElasticSearch跟SpringBoot集成的es版本保持一致。否则可能会出错。特别是一个使用6版本和一个使用7版本。

Maven可以看SpringBoot集成的es版本:

如果需要改就在这里:pom中

    <properties>
        <java.version>1.8</java.version>
        <!--定义elasticsearch版本依赖,保证跟本地版本一致,否则可能出错-->
<!--        <elasticsearch.version>7.6.2</elasticsearch.version>-->
    </properties>

7版本不太熟,改动也很大,以后有时间再升级。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持北冥有鱼。

您可能感兴趣的文章:

  • SpringBoot 整合 ElasticSearch操作各种高级查询搜索
  • SpringBoot整合Elasticsearch游标查询的示例代码(scroll)
  • es(elasticsearch)整合SpringCloud(SpringBoot)搭建教程详解
  • Spring Boot整合ElasticSearch实现多版本兼容的方法详解
  • SpringBoot整合Elasticsearch并实现CRUD操作
  • Spring Boot集成ElasticSearch实现搜索引擎的示例
  • Spring Boot 集成Elasticsearch模块实现简单查询功能

相关推荐:

Python Pika库报StreamConnection: Connection to ... failed原因

Connectionfailed本质是TCP层连接失败,非Pika逻辑问题;根本原因在于操作系统网络栈不可达,需检查服务状态、地址端口、防火墙、TLS配置及virtual_host等底层连通性。 Connectionfailed错误本质是TCP层连不上,不是Pika自身逻辑问题 这个报错看起来像pi...

GraalVM 中 Java Map 与 Python 的双向互操作实践

本文详解如何在GraalVMPolyglot环境中安全、高效地将JavaMap传递给Python脚本,调用其方法获取值,并将处理结果(如新Map)返回Java,重点解决AttributeError:foreignobjecthasnoattribute'items'等常见互操作异常。 本文详解如何在...

PySpark Java Gateway Exited 错误的根源与解决方案

本文详解pyspark报错`[java_gateway_exited]javagatewayprocessexitedbeforesendingitsportnumber`的根本原因——虚拟环境(venv)隔离导致`java_home`和系统级java路径未被正确继承,并提供可落地的修复步骤与最佳实...

python与java用途区别有哪些

Python适合快速开发、数据科学、AI建模、轻量Web服务及自动化脚本;Java适合大型企业系统、Android开发、高并发中间件及需长期稳定维护的场景。 Python和Java虽然都能做通用开发,但实际用起来分工很明确——不是“哪个更好”,而是“谁更合适”。关键看你要解决什么问题、在什么环境里跑...

Python配置文件加载_ini与json解析【教程】

应根据实际需求选择:ini适合分节、带注释的简单配置(如数据库连接),用configparser读取;json适合嵌套复杂、跨语言共享的配置(如API参数),用json模块加载。 Python读取配置文件,ini和json是最常用的两种格式。选哪种不取决于“哪个更好”,而要看实际需求:ini更适合分...

redis怎么使用java

使用redisjava库需:1.添加依赖(jedis3.6.3);2.连接到redis服务器(指定主机和端口);3.进行基本操作(设置/获取值、检查键是否存在、删除键);4.执行复杂操作(哈希表、列表、集合、有序集合);5.发布/订阅消息(使用jedispubsub);6.关闭连接(jedis.cl...

10 对 -3 取余,结果是 1 还是 -2?Java 和 MySQL 的结果为何不同?

10对-3取余结果:1还是-2? 问题: 对-3取余时,10的余数是1还是-2?使用java和mysql计算得出的结果不同,这是否意味着这些函数存在错误? 答案: 余数正负 余数可以是正数,也可以是负数。数学上计算余数的公式为:余数=被除数-向下取整(被除数/除数)*除数根据该公式,10对-3取余的...