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版本不太熟,改动也很大,以后有时间再升级。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • Vue+SpringBoot前后端分离中的跨域问题

    在前后端分离开发中,需要前端调用后端api并进行内容显示,如果前后端开发都在一台主机上,则会由于浏览器的同源策略限制,出现跨域问题(协议.域名.端口号不同等),导致不能正常调用api接口,给开发带来不便. 封装api请求 import axios from 'axios' //axios.create创建一个axios实例,并对该实例编写配置,后续所有通过实例发送的请求都受当前配置约束 const $http = axios.create({ baseURL: '', timeout: 1000

  • springboot 配置文件里部分配置未生效的解决

    springboot 配置文件里部分配置未生效 最近用springboot搭了个项目,上线过段时间就会出现卡死,猜测是数据库连接池的连接被占满,用的连接池是druid,于是给项目加上了一个数据库连接池监控. 代码如下: @Configuration public class DruidConfiguration { /** * * 注册一个StatViewServlet * * @return * */ @Bean public ServletRegistrationBean DruidStat

  • 手把手带你分析SpringBoot自动装配完成了Ribbon哪些核心操作

    目录 一.项目案例准备 1.Order服务 2.User服务 二.Ribbon原理分析 1.RibbonAutoConfiguration 2.LoadBalancerAutoConfiguration 总结 一.项目案例准备 首先我们大家案例环境,通过[RestTemplate]来实现服务调用,通过[Ribbon]实现客户端负载均衡操作. 1.Order服务 我们的Order服务作为服务提供者.创建SpringBoot项目,并添加相关依赖 <?xml version="1.0"

  • 使用Springboot实现word在线编辑保存

    目录 一.查看官网 二.查看下载包 1.springboot 引入 pageoffice5.2.0.12.jar 2.springboot 引入thymleaf 3.编写配置文件 4.编写 index.html 和 word.html 4.1 index.html 4.2 word.html 5.编写PageOfficeController 6.application.yml 配置 7.注意 项目结构 三.测试 输入网址 注意事项 四.gitee地址 总结 一.查看官网 http://www.z

  • 在springboot中注入FilterRegistrationBean不生效的原因

    springboot注入FilterRegistrationBean不生效 回顾 最近自定义了两个过滤器,接口请求返回加密和sql注入处理过滤器,因为在封装一些工具包,我在单独调好之后,就打算做成一个注解,像springboot启动类上加@EnableScheduling一样,可以随意控制,当我不想让这俩过滤器生效的时候,那就不加这个注解就可以了. 当然我想到了FilterRegistrationBean的使用方法,注入这两个过滤器. 但是当我写完之后,打成包之后,发现只有sql注入过滤器生效.

  • 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

  • SpringBoot集成MybatisPlus报错的解决方案

    这篇文章主要介绍了SpringBoot集成MybatisPlus报错的解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 问题 启动的时候总是报如下错误: java.lang.annotation.AnnotationFormatError: Invalid default: public abstract java.lang.Class 解决方案 需要一个mybatis-spring-boot-starter的包,在pom文件加上之后,完

  • idea中springboot项目连接数据库报错的原因解析

    一.我在运用mybatis下报错,反复检查无逻辑问题,下图为项目结构截图 二.最后总结问题如下: 1)报错代码: Caused by: java.lang.RuntimeException: Failed to load driver class com.mysql.cj.jdbc.Driver in either of HikariConfig class loader or Thread context classloader at com.zaxxer.hikari.HikariConfi

  • SpringBoot启动yaml报错的解决

    SpringBoot启动yaml报错 报错找不到org.yaml里的一个方法 10:45:54.742 [main] ERROR org.springframework.boot.SpringApplication - Application run failed java.lang.NoSuchMethodError: org.yaml.snakeyaml.nodes.ScalarNode.getScalarStyle()Lorg/yaml/snakeyaml/DumperOptions$Sc

  • vue component 中引入less文件报错 Module build failed

    错误是这样发生的: //在vue component组件中引入less文件 <style lang="less"> @import '@/assets/css/public/layout.less'; </style> 然后就报错了: Module build failed: @import '@/assets/css/public/layout.less'; ^ Can't resolve './@/assets/css/public/layout.less'

  • 解决SpringBoot中@Email报错问题

    JSR303校验相关 现象:在springboot中使用@Email注解进行数据校验时,报没有该注解的错误. 解决方法: 在pom.xml中加该配置 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency> 测试成功 ==测试成功== ![i

  • Springboot集成Elasticsearch的步骤与相关功能

    目录 集成配置步骤 步骤1:加入 Maven 相关依赖 步骤2:配置 elasticsearch 的主机和端口 步骤3:配置 Elaseticsearch 客户端 步骤4:创建文档实体 步骤5:创建 controller,service, dao 层 相关功能实现 1. 添加文档  2. 修改文档  3. 根据ID查询文档  4. 根据ID删除文档  5. 查询所有文档  6. 条件查询(单个条件)  7. 条件查询(多条件)  8. 分页查询(降序)  9. 分页查询(升序)  10. 分页查

  • SpringBoot Test 多线程报错的根本原因(dataSource already closed)

    背景 使用Springboot test进行相关测试的时候,发现开启线程操作数据库的时候异常. 排查方法 将线程移除,采用并行的方式,操作数据库正常. 根本原因 SpringBoot Test 主线程退出,导致Spring 容器关闭. Spring容器关闭,导致DruidDataSource 关闭 此时用户线程去访问已关闭的数据源,导致报错. 解决方法 提供一个全局的线程池,然后使用线程池开启线程操作,然后添加监听器,监听线程池里面是否有未完成的任务,如果有则不关闭容器. @Component

  • 关于获取DIV内部内容报错的原因分析及解决办法

    1.错误描述 2.错误原因 由于向div中添加元素,利用append(); $("#divStyle").append("<div><label>_data[i].name</label></div>"); append里面是动态数据,当请求数据为空时,获取并判断div中的内容: var divContent = $("#divStyle").html(); if(divContent == nul

  • 微信二次分享报错invalid signature问题及解决方法

    基于微信公众号开发的h5页面(使用jssdk接口),由用户A分享给用户B,用户B再次分享这个页面时,不能成功分享.问题出在用户B收到的分享链接与用户A打开的链接不同 A用户的链接为 http://test.com/test.html B用户收到的连接 http://test.com/test.html&from=singlemessage from=singlemessage是微信客户端为了区分分享来源再链接后自动添加的标记,再次分享时,需要在js代码中对自动获取的连接进行encodeURICo

随机推荐