Reputation: 665
I got the following error:
No unique service matching interface org.springframework.amqp.rabbit.connection.ConnectionFactory found
Description:
spring-cloud-starter-bus-amqp
to connect to RabbitMQ.cf push
Springboot App to PCF, I got the above exception.RabbitServiceAutoConfiguration.CloudProfile.CloudConnectors.UseCloudConnectors
to create the rabbitmqConnectionFactory
, and it failed of course because my PCF space did not have RabbitMQ service at all.Question: How do I configure Springboot App and it can be deployed to PCF without issues?
What I tried ... (below), and it didn't work.
@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);
}
}
My build.gradle
dependencies:
...
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-bus-amqp', version: '2.1.1.RELEASE'
...
Stack trace:
s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'rabbitConnectionFactory' defined in org.springframework.cloud.stream.binder.rabbit.config.RabbitServiceAutoConfiguration$CloudProfile$CloudConnectors$UseCloudConnectors: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.amqp.rabbit.connection.ConnectionFactory]: Factory method 'rabbitConnectionFactory' threw exception; nested exception is org.springframework.cloud.CloudException: No unique service matching interface org.springframework.amqp.rabbit.connection.ConnectionFactory found. Expected 1, found 0
Thank you for your time.
Upvotes: 0
Views: 1427
Reputation: 665
After looking into Spring cloud stream source code, I've found out the solution that bypassed the cloud configure for Rabbitmq on platform. That way, if you don't have Rabbitmq on PCF platform, you can still deploy your Spring Boot App onto PCF without any issue. In my case, Rabbitmq is running on a Dev VM server off the platform.
In your application.properties
, set the following:
spring.cloud.stream.override-cloud-connectors=true
Upvotes: 3