Spring 在 heroku 上部署时启动绑定到 cloudmaqp 错误

Spring boot Binding to cloudmaqp error when deployed on heroku

我将一项服务部署到 heroku。我的服务使用 cloudamqp 作为云消息服务。当我在本地环境中的服务 运行 连接成功时,我从未发现此错误。

但是当我将服务部署到 heroku 时,出现了这个错误。

2017-10-30T06:03:05.231416+00:00 app[web.1]: 2017-10-30 06:03:05.231  INFO 4 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Getting all instance registry info from the eureka server
2017-10-30T06:03:05.256787+00:00 app[web.1]: 2017-10-30 06:03:05.256  INFO 4 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : The response status is 200
2017-10-30T06:03:07.002922+00:00 app[web.1]: 2017-10-30 06:03:07.002  WARN 4 --- [nfoReplicator-0] o.s.b.a.health.RabbitHealthIndicator     : Health check failed
2017-10-30T06:03:07.002970+00:00 app[web.1]: 
2017-10-30T06:03:07.002971+00:00 app[web.1]: org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused (Connection refused)
2017-10-30T06:03:07.002972+00:00 app[web.1]:    at org.springframework.amqp.rabbit.support.RabbitExceptionTranslator.convertRabbitAccessException(RabbitExceptionTranslator.java:62)
2017-10-30T06:03:07.002972+00:00 app[web.1]:    at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:368)
2017-10-30T06:03:07.002973+00:00 app[web.1]:    at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.createConnection(CachingConnectionFactory.java:573)
2017-10-30T06:03:07.002974+00:00 app[web.1]:    at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:1430)
2017-10-30T06:03:07.002974+00:00 app[web.1]:    at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:1411)
2017-10-30T06:03:07.002975+00:00 app[web.1]:    at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:1387)
2017-10-30T06:03:07.002976+00:00 app[web.1]:    at org.springframework.boot.actuate.health.RabbitHealthIndicator.getVersion(RabbitHealthIndicator.java:49)
2017-10-30T06:03:07.002976+00:00 app[web.1]:    at org.springframework.boot.actuate.health.RabbitHealthIndicator.doHealthCheck(RabbitHealthIndicator.java:45)
2017-10-30T06:03:07.002977+00:00 app[web.1]:    at org.springframework.boot.actuate.health.AbstractHealthIndicator.health(AbstractHealthIndicator.java:43)
2017-10-30T06:03:07.002977+00:00 app[web.1]:    at org.springframework.boot.actuate.health.CompositeHealthIndicator.health(CompositeHealthIndicator.java:68)
2017-10-30T06:03:07.002978+00:00 app[web.1]:    at org.springframework.cloud.netflix.eureka.EurekaHealthCheckHandler.getHealthStatus(EurekaHealthCheckHandler.java:103)
2017-10-30T06:03:07.002978+00:00 app[web.1]:    at org.springframework.cloud.netflix.eureka.EurekaHealthCheckHandler.getStatus(EurekaHealthCheckHandler.java:99)
2017-10-30T06:03:07.002978+00:00 app[web.1]:    at com.netflix.discovery.DiscoveryClient.refreshInstanceInfo(DiscoveryClient.java:1362)
2017-10-30T06:03:07.002979+00:00 app[web.1]:    at com.netflix.discovery.InstanceInfoReplicator.run(InstanceInfoReplicator.java:100)
2017-10-30T06:03:07.002980+00:00 app[web.1]:    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
2017-10-30T06:03:07.002980+00:00 app[web.1]:    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
2017-10-30T06:03:07.002981+00:00 app[web.1]:    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access1(ScheduledThreadPoolExecutor.java:180)
2017-10-30T06:03:07.002981+00:00 app[web.1]:    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
2017-10-30T06:03:07.002982+00:00 app[web.1]:    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
2017-10-30T06:03:07.002983+00:00 app[web.1]:    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
2017-10-30T06:03:07.002983+00:00 app[web.1]:    at java.lang.Thread.run(Thread.java:748)
2017-10-30T06:03:07.002984+00:00 app[web.1]: Caused by: java.net.ConnectException: Connection refused (Connection refused)
2017-10-30T06:03:07.002985+00:00 app[web.1]:    at java.net.PlainSocketImpl.socketConnect(Native Method)
2017-10-30T06:03:07.002985+00:00 app[web.1]:    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
2017-10-30T06:03:07.002986+00:00 app[web.1]:    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
2017-10-30T06:03:07.002987+00:00 app[web.1]:    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
2017-10-30T06:03:07.002987+00:00 app[web.1]:    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
2017-10-30T06:03:07.002988+00:00 app[web.1]:    at java.net.Socket.connect(Socket.java:589)
2017-10-30T06:03:07.002989+00:00 app[web.1]:    at com.rabbitmq.client.impl.SocketFrameHandlerFactory.create(SocketFrameHandlerFactory.java:50)
2017-10-30T06:03:07.002989+00:00 app[web.1]:    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:907)
2017-10-30T06:03:07.002990+00:00 app[web.1]:    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:859)
2017-10-30T06:03:07.002990+00:00 app[web.1]:    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:799)
2017-10-30T06:03:07.002991+00:00 app[web.1]:    at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:352)
2017-10-30T06:03:07.002992+00:00 app[web.1]:    ... 19 common frames omitted
2017-10-30T06:03:07.002993+00:00 app[web.1]: 

当我看到我的 rabbitmq 管理页面时,我的服务成功连接到我的 cloudamqp rabbitmq 服务器。像这样显示

这是我的 application.yml 服务..

spring:
    application:
        name: filehandler
    jackson:
        serialization.write_dates_as_timestamps: false
    jpa:
        open-in-view: false
        hibernate:
            ddl-auto: update
            naming:
                physical-strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
                implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
    messages:
        basename: i18n/messages
    mvc:
        favicon:
            enabled: false
    thymeleaf:
        mode: XHTML
    cloud:
        stream:
            bindings:
                fileOutput:
                    destination: file
                    binder: rabbitmqserver
                    contentType: application/x-java-serialized-object
                indexInput:
                    destination: index
                    binder: rabbitmqserver
                    contentType: application/x-java-serialized-object
            binders:
                rabbitmqserver:
                    type: rabbit
                    environment:
                        spring:
                            rabbitmq:
                                addresses: elephant.rmq.cloudamqp.com
                                username: username
                                password: password
                                virtual-host: virtualhost

谁能帮我解决这个问题。

禁用 rabbitmq 管理健康检查,我不知道它在做什么,但它导致了你的问题:

management.health.rabbit.enabled=false

https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

已修复,

忘记禁用 RabbitAutoConfiguration 导致我的服务连接 localhost:5672 messagebroker 服务器。

通过添加

修复了它
@EnableAutoConfiguration(exclude = {RabbitAutoConfiguration.class})

根据此页面 https://github.com/spring-cloud/spring-cloud-stream-binder-rabbit/blob/master/docs/src/main/asciidoc/overview.adoc,在 spring 云流应用程序上使用多个绑定器时禁用 RabbitAutoConfiguration 很重要。