spring-boot - 由于 RabbitServiceAutoConfiguration,Spring-boot App 无法部署到 PCF
问题描述
我收到以下错误:
找不到唯一的服务匹配接口 org.springframework.amqp.rabbit.connection.ConnectionFactory
描述:
- RabbitMQ 在开发服务器上运行 Pivotal Cloud Foundry 平台。
- 在 PCF 平台上,没有 RabbitMQ 服务(市场)
- Springboot App 用于
spring-cloud-starter-bus-amqp
连接 RabbitMQ。 - 如果 Springboot App 在本地运行,一切正常
- 但是,当
cf push
Springboot App 到 PCF 时,我得到了上述异常。 - 根本原因是 PCF 使用 autoconfigure
RabbitServiceAutoConfiguration.CloudProfile.CloudConnectors.UseCloudConnectors
来创建rabbitmqConnectionFactory
,当然它失败了,因为我的 PCF 空间根本没有 RabbitMQ 服务。
问题:如何配置 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 cloud stream 源码后,我发现了在平台上为 Rabbitmq 绕过云配置的解决方案。这样,如果您在 PCF 平台上没有 Rabbitmq,您仍然可以将 Spring Boot 应用程序部署到 PCF 上而不会出现任何问题。就我而言,Rabbitmq 在平台外的 Dev VM 服务器上运行。
在您的application.properties
中,设置以下内容:
spring.cloud.stream.override-cloud-connectors=true
推荐阅读
- c++ - 如何检查函数中的数组长度?
- node.js - 如何避免在对象内部创建对象并使用猫鼬将元素直接推送到快速数组中?
- r - R ggplot颜色箱宽度看起来不相等
- spring - 如何从 Junit5 中的配置中获取 DataSource bean
- flutter - 使用带有大量数据的 Dio 发送请求时,Flutter 应用程序正在关闭
- mysql - SQLAlchemy 无法连接到 Kubernetes 本地主机上的 Mysql 服务器
- c - PNG 文件被截断,不能由每个应用程序显示
- python-3.x - 如何将 DataFrame(包括数组)中的两列与 CSV(Dataframe/Dictionary)中的两列匹配
- python - 在类外调用变量
- can-bus - 在 CANopen 中发送超过 8 字节数据的事件(异步数据)