问题
In springboot application, the configuration of application-prod.yml
are :
spring:
rabbitmq:
host: rabbitmq
port: 5672
username: guest
password: guest
gitlab-ci.yml
configuration are :
services:
- docker:dind
- rabbitmq:management
variables:
RABBITMQ_DEFAULT_USER: guest
RABBITMQ_DEFAULT_PASS: guest
AMQP_URL: 'amqp://guest:guest@rabbitmq:5672'
stages:
- build
maven-foo-build:
stage: build
script:
- cd foo
- ./mvnw package -B -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn
artifacts:
paths:
- foo/target/*.jar
I got the following exception on gitlab pipeline :
org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused (Connection refused)
at org.springframework.amqp.rabbit.support.RabbitExceptionTranslator.convertRabbitAccessException(RabbitExceptionTranslator.java:62)
at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:509)
at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.createConnection(CachingConnectionFactory.java:702)
at org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.createConnection(ConnectionFactoryUtils.java:214)
at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:2073)
at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:2047)
at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:2027)
at org.springframework.amqp.rabbit.core.RabbitAdmin.getQueueProperties(RabbitAdmin.java:403)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.attemptDeclarations(AbstractMessageListenerContainer.java:1787)
I also tried to make a new stage and run on pipeline, before running build stage such as :
docker-image-test:
stage: test
script:
- docker run -d --hostname rabbitmq --name rabbitmq -p 5672:5672 rabbitmq:management
But got the same result , and unable to connect with RabbitMQ. And also follow the following link here and got nothing.
Want to achieve to deploy RabbitMQ on production with gitlab CI/CD pipeline which currently we are using docker image rabbitmq:management
. What am I going to miss there, any help would be grateful, Thanks
回答1:
I have found the solution , maybe it may help someone. Problem is with the connection factory in springboot application, which is unable to connect with docker image rabbitmq:Management
when we go for live(prod). So we need to override the connection factory in our RabbitMQ Configurations.
Please find the source code here :
private static final String BROKER_URI = "amqp://guest:guest@rabbitmq:5672";
@Bean
public CachingConnectionFactory rabbitConnectionFactory(RabbitProperties config)
throws Exception {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.getRabbitConnectionFactory().setUri(BROKER_URI);
return connectionFactory;
}
来源:https://stackoverflow.com/questions/57786388/unable-to-deploy-rabbitmq-with-gitlab-ci-pipeline-on-google-cloud