无法在Google Cloud上使用gitlab-ci管道部署RabbitMQ?

时间:2019-09-04 10:20:30

标签: spring-boot docker rabbitmq gitlab gitlab-ci

在springboot应用程序中,application-prod.yml的配置为:

spring:
  rabbitmq:
    host: rabbitmq
    port: 5672
    username: guest
    password:  guest

gitlab-ci.yml的配置是:

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

我在gitlab管道上遇到以下异常:

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)

在运行构建阶段(例如:

)之前,我还尝试创建一个新阶段并在管道上运行
docker-image-test:
  stage: test
  script:
    - docker run -d --hostname rabbitmq --name rabbitmq  -p 5672:5672 rabbitmq:management

但是得到了相同的结果,并且无法与RabbitMQ连接。并点击下面的链接here,一无所获。

想要通过gitlab CI / CD管道实现在生产环境中部署RabbitMQ,目前我们正在使用docker image rabbitmq:management。我在那里会想念什么,任何帮助将不胜感激,谢谢

1 个答案:

答案 0 :(得分:0)

我已经找到了解决方案,也许它可以对某人有所帮助。问题是springboot应用程序中的连接工厂,当我们进行live(prod)时,它无法与docker映像rabbitmq:Management连接。因此,我们需要在RabbitMQ配置中覆盖连接工厂。

请在此处找到源代码:

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;
        }