o.s.b.a.amqp.RabbitHealthIndicator:Rabbit health check failed【解决方法】

今天爱分享给大家带来o.s.b.a.amqp.RabbitHealthIndicator:Rabbit health check failed【解决方法】,希望能够帮助到大家。
学习 Spring Cloud Stream 的时候用到 RabbitMQ,连接本地的 RabbitMQ 一切正常,连接远程的 RabbitMQ 始终会报 Rabbit health check failed.

问题描述
错误提示是这样的:


2020-08-04 11:34:29.029 WARN 2688 — [-192.168.25.141] o.s.b.a.amqp.RabbitHealthIndicator : Rabbit health check failed

org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused: connect
at org.springframework.amqp.rabbit.support.RabbitExceptionTranslator.convertRabbitAccessException(RabbitExceptionTranslator.java:62) ~[spring-rabbit-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:510) ~[spring-rabbit-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.createConnection(CachingConnectionFactory.java:751) ~[spring-rabbit-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.createConnection(ConnectionFactoryUtils.java:215) ~[spring-rabbit-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:2085) ~[spring-rabbit-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:2058) ~[spring-rabbit-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:2038) ~[spring-rabbit-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.boot.actuate.amqp.RabbitHealthIndicator.getVersion(RabbitHealthIndicator.java:49) ~[spring-boot-actuator-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.boot.actuate.amqp.RabbitHealthIndicator.doHealthCheck(RabbitHealthIndicator.java:44) ~[spring-boot-actuator-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.boot.actuate.health.AbstractHealthIndicator.health(AbstractHealthIndicator.java:82) ~[spring-boot-actuator-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.boot.actuate.health.HealthIndicator.getHealth(HealthIndicator.java:37) [spring-boot-actuator-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.boot.actuate.health.HealthEndpoint.getHealth(HealthEndpoint.java:81) [spring-boot-actuator-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.boot.actuate.health.HealthEndpoint.getHealth(HealthEndpoint.java:38) [spring-boot-actuator-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.boot.actuate.health.HealthEndpointSupport.getContribution(HealthEndpointSupport.java:108) [spring-boot-actuator-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.boot.actuate.health.HealthEndpointSupport.getAggregateHealth(HealthEndpointSupport.java:119) [spring-boot-actuator-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.boot.actuate.health.HealthEndpointSupport.getContribution(HealthEndpointSupport.java:105) [spring-boot-actuator-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.boot.actuate.health.HealthEndpointSupport.getHealth(HealthEndpointSupport.java:83) [spring-boot-actuator-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.boot.actuate.health.HealthEndpointSupport.getHealth(HealthEndpointSupport.java:70) [spring-boot-actuator-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.boot.actuate.health.HealthEndpoint.health(HealthEndpoint.java:75) [spring-boot-actuator-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.boot.actuate.health.HealthEndpoint.health(HealthEndpoint.java:65) [spring-boot-actuator-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_60]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_60]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_60]
at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_60]
at org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:282) [spring-core-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.boot.actuate.endpoint.invoke.reflect.ReflectiveOperationInvoker.invoke(ReflectiveOperationInvoker.java:77) [spring-boot-actuator-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.boot.actuate.endpoint.annotation.AbstractDiscoveredOperation.invoke(AbstractDiscoveredOperation.java:60) [spring-boot-actuator-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.boot.actuate.endpoint.jmx.EndpointMBean.invoke(EndpointMBean.java:121) [spring-boot-actuator-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.boot.actuate.endpoint.jmx.EndpointMBean.invoke(EndpointMBean.java:96) [spring-boot-actuator-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) [na:1.8.0_60]
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801) [na:1.8.0_60]
at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1471) [na:1.8.0_60]
at javax.management.remote.rmi.RMIConnectionImpl.access300 ( R M I C o n n e c t i o n I m p l . j a v a : 76 ) [ n a : 1.8. 0 6 0 ] a t j a v a x . m a n a g e m e n t . r e m o t e . r m i . R M I C o n n e c t i o n I m p l 300(RMIConnectionImpl.java:76) [na:1.8.0_60] at javax.management.remote.rmi.RMIConnectionImpl300(RMIConnectionImpl.java:76)[na:1.8.0 
6
​	
 0]atjavax.management.remote.rmi.RMIConnectionImplPrivilegedOperation.run(RMIConnectionImpl.java:1312) [na:1.8.0_60]
at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1404) [na:1.8.0_60]
at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:832) [na:1.8.0_60]
at sun.reflect.GeneratedMethodAccessor117.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_60]
at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_60]
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:323) [na:1.8.0_60]
at sun.rmi.transport.Transport$1.run(Transport.java:200) [na:1.8.0_60]
at sun.rmi.transport.Transport1. r u n ( T r a n s p o r t . j a v a : 197 ) [ n a : 1.8. 0 6 0 ] a t j a v a . s e c u r i t y . A c c e s s C o n t r o l l e r . d o P r i v i l e g e d ( N a t i v e M e t h o d ) [ n a : 1.8. 0 6 0 ] a t s u n . r m i . t r a n s p o r t . T r a n s p o r t . s e r v i c e C a l l ( T r a n s p o r t . j a v a : 196 ) [ n a : 1.8. 0 6 0 ] a t s u n . r m i . t r a n s p o r t . t c p . T C P T r a n s p o r t . h a n d l e M e s s a g e s ( T C P T r a n s p o r t . j a v a : 568 ) [ n a : 1.8. 0 6 0 ] a t s u n . r m i . t r a n s p o r t . t c p . T C P T r a n s p o r t 1.run(Transport.java:197) [na:1.8.0_60] at java.security.AccessController.doPrivileged(Native Method) [na:1.8.0_60] at sun.rmi.transport.Transport.serviceCall(Transport.java:196) [na:1.8.0_60] at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568) [na:1.8.0_60] at sun.rmi.transport.tcp.TCPTransport1.run(Transport.java:197)[na:1.8.0 
6
​	
 0]atjava.security.AccessController.doPrivileged(NativeMethod)[na:1.8.0 
6
​	
 0]atsun.rmi.transport.Transport.serviceCall(Transport.java:196)[na:1.8.0 
6
​	
 0]atsun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)[na:1.8.0 
6
​	
 0]atsun.rmi.transport.tcp.TCPTransportConnectionHandler.run0(TCPTransport.java:826) [na:1.8.0_60]
at sun.rmi.transport.tcp.TCPTransportC o n n e c t i o n H a n d l e r . l a m b d a ConnectionHandler.lambdaConnectionHandler.lambdarun256 ( T C P T r a n s p o r t . j a v a : 683 ) [ n a : 1.8. 0 6 0 ] a t j a v a . s e c u r i t y . A c c e s s C o n t r o l l e r . d o P r i v i l e g e d ( N a t i v e M e t h o d ) [ n a : 1.8. 0 6 0 ] a t s u n . r m i . t r a n s p o r t . t c p . T C P T r a n s p o r t 256(TCPTransport.java:683) [na:1.8.0_60] at java.security.AccessController.doPrivileged(Native Method) [na:1.8.0_60] at sun.rmi.transport.tcp.TCPTransport256(TCPTransport.java:683)[na:1.8.0 
6
​	
 0]atjava.security.AccessController.doPrivileged(NativeMethod)[na:1.8.0 
6
​	
 0]atsun.rmi.transport.tcp.TCPTransportConnectionHandler.run(TCPTransport.java:682) [na:1.8.0_60]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) ~[na:1.8.0_60]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) ~[na:1.8.0_60]
at java.lang.Thread.run(Thread.java:745) ~[na:1.8.0_60]
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[na:1.8.0_60]
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85) ~[na:1.8.0_60]
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_60]
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_60]
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_60]
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[na:1.8.0_60]
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_60]
at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_60]
at com.rabbitmq.client.impl.SocketFrameHandlerFactory.create(SocketFrameHandlerFactory.java:60) ~[amqp-client-5.7.3.jar:5.7.3]
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1113) ~[amqp-client-5.7.3.jar:5.7.3]
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1063) ~[amqp-client-5.7.3.jar:5.7.3]
at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.connect(AbstractConnectionFactory.java:526) ~[spring-rabbit-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:473) ~[spring-rabbit-2.2.2.RELEASE.jar:2.2.2.RELEASE]
… 50 common frames omitted

解决思路

首先来看我的项目的 yml 配置文件:


server:
  port: 8801

spring:
  application:
    name: cloud-stream-provider
  cloud:
    stream:
      binders: # 在此处配置要绑定的rabbitmq的服务信息;
        defaultRabbit: # 表示定义的名称,用于于binding整合
          type: rabbit # 消息组件类型
          environment: # 设置rabbitmq的相关的环境配置
            spring:
              rabbitmq:
                host: rabbitmq
                port: 5672
                username: guest
                password: guest
                virtual-host: /

      bindings: # 服务的整合处理
        output: # 这个名字是一个通道的名称
          destination: studyExchange # 表示要使用的Exchange名称定义
          content-type: application/json # 设置消息类型,本次为json,文本则设置“text/plain”
          binder: defaultRabbit # 设置要绑定的消息服务的具体设置

eureka:
  client: # 客户端进行Eureka注册的配置
    service-url:
      defaultZone: http://localhost:7001/eureka
  instance:
    lease-renewal-interval-in-seconds: 2 # 设置心跳的时间间隔(默认是30秒)
    lease-expiration-duration-in-seconds: 5 # 如果现在超过了5秒的间隔(默认是90秒)
    instance-id: send-8801.com  # 在信息列表时显示主机名称
    prefer-ip-address: true     # 访问的路径变为IP地址


看似没有任何问题,但有几项参数并没有在 yml 中配置:


spring:
  rabbitmq:
    host: rabbitmq
    port: 5672
    username: guest
    password: guest

如果上述参数没有配置,默认会尝试连接 localhost:5672,就会造成 Rabbit health check failed。配置了上述参数后的 yml 配置文件如下:


server:
  port: 8801

spring:
  application:
    name: cloud-stream-provider
  cloud:
    stream:
      binders: # 在此处配置要绑定的rabbitmq的服务信息;
        defaultRabbit: # 表示定义的名称,用于于binding整合
          type: rabbit # 消息组件类型
          environment: # 设置rabbitmq的相关的环境配置
            spring:
              rabbitmq:
                host: rabbitmq
                port: 5672
                username: guest
                password: guest
                virtual-host: /

      bindings: # 服务的整合处理
        output: # 这个名字是一个通道的名称
          destination: studyExchange # 表示要使用的Exchange名称定义
          content-type: application/json # 设置消息类型,本次为json,文本则设置“text/plain”
          binder: defaultRabbit # 设置要绑定的消息服务的具体设置
  #如果连接到远程RabbitMQ报Rabbit health check failed异常
  #配置以下信息
  #因为默认会尝试连接localhost:5672
  rabbitmq:
    host: rabbitmq
    port: 5672
    username: guest
    password: guest

eureka:
  client: # 客户端进行Eureka注册的配置
    service-url:
      defaultZone: http://localhost:7001/eureka
  instance:
    lease-renewal-interval-in-seconds: 2 # 设置心跳的时间间隔(默认是30秒)
    lease-expiration-duration-in-seconds: 5 # 如果现在超过了5秒的间隔(默认是90秒)
    instance-id: send-8801.com  # 在信息列表时显示主机名称
    prefer-ip-address: true     # 访问的路径变为IP地址


再次运行项目,问题成功解决,Rabbit health check failed 没有出现。

人已赞赏
Java

main ERROR processing element appender configuration: null:CLASS_NOT_FOUND【解决方法】

2020-12-30 21:27:52

Java

ERROR:Could not install packages duetoan EnvirmentError:[WinError5]【解决方法】

2020-12-30 22:14:45