首页 > 解决方案 > 由于 RabbitServiceAutoConfiguration,Spring-boot App 无法部署到 PCF

问题描述

我收到以下错误:

找不到唯一的服务匹配接口 org.springframework.amqp.rabbit.connection.ConnectionFactory

描述:

问题:如何配置 Springboot App 并且可以毫无问题地部署到 PCF?

我试过的......(下),但它没有用。

@Profile("cloud")
@Configuration
@EnableConfigurationProperties(RabbitmqInfo.class)
@Slf4j
public class RabbitmqConfig extends AbstractCloudConfig {

    @Bean
    public ConnectionFactory rabbitmqConnectionFactory(@Autowired RabbitmqInfo rabbitmq) {

        CachingConnectionFactory factory = new CachingConnectionFactory();
        factory.setHost(rabbitmq.getHost());
        factory.setPort(rabbitmq.getPort());
        factory.setUsername(rabbitmq.getUsername());
        factory.setPassword(rabbitmq.getPassword());

        log.info("[*] rabbitmqConnectionFactory uses CachingConnectionFactory");
        log.info("[*] rabbitmq.host: {}", factory.getHost());

        return factory;
    }

    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory rabbitmqConnectionFactory) {
        return new RabbitTemplate(rabbitmqConnectionFactory);
    }

}

我的build.gradle依赖:

...
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-bus-amqp', version: '2.1.1.RELEASE'
...

堆栈跟踪:

scaAnnotationConfigApplicationContext :在上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory.BeanCreationException:在 org.springframework.cloud.stream.binder.rabbit.config.RabbitServiceAutoConfiguration$CloudProfile$ 中创建名称为“rabbitConnectionFactory”的bean时出错CloudConnectors$UseCloudConnectors:通过工厂方法进行 Bean 实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.amqp.rabbit.connection.ConnectionFactory]:工厂方法“rabbitConnectionFactory”抛出异常;嵌套异常是 org.springframework.cloud.CloudException: No unique service matching interface org.springframework.amqp.rabbit.connection.ConnectionFactory found。预期 1,发现 0

感谢您的时间。

标签: spring-bootrabbitmqamqppcf

解决方案


在查看了 Spring cloud stream 源码后,我发现了在平台上为 Rabbitmq 绕过云配置的解决方案。这样,如果您在 PCF 平台上没有 Rabbitmq,您仍然可以将 Spring Boot 应用程序部署到 PCF 上而不会出现任何问题。就我而言,Rabbitmq 在平台外的 Dev VM 服务器上运行。

在您的application.properties中,设置以下内容:

spring.cloud.stream.override-cloud-connectors=true

推荐阅读