У меня есть приложение Spring Boot 2.1 с интеграционными тестами. Для целей интеграционных тестов я хочу запустить контейнер RabbitMq с инфраструктурой testcontainers. Когда я запускаю их на своей локальной машине, кажется, что все работает, я могу получить доступ к своему rabbitMQ во время ИТ-тестов. Однако, как только я выполняю под gitlab-ci, я постоянно получаю исключения для отказа в соединении.
Вот мое приложение-это-свойства
spring.rabbitmq.host=localhost
spring.rabbitmq.virtualHost=/
spring.rabbitmq.port=5673
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.dynamic=true
spring.rabbitmq.template.retry.enabled=true
spring.rabbitmq.listener.simple.acknowledgeMode=AUTO
spring.rabbitmq.listener.simple.concurrency=5
Это мой шаг проверки в gitlab-ci
verify:feature:
stage: verify
script:
- git config --global user.email [email protected]
- git config --global user.name gitlab
- git fetch --all
- git checkout origin/develop
- git merge $CI_BUILD_REF --no-commit --no-ff
- mvn $MAVEN_CLI_OPTS verify sonar:sonar $SONAR_PREVIEW_CLI_OPTS
only:
- /feature.*/
И вот как я запускаю свой тестовый контейнер RabbitMQ
@Slf4j
@RunWith(SpringRunner.class)
@TestPropertySource(locations = {"classpath:application-it.properties"})
@SpringBootTest
public class TransformerServiceApplicationIt {
private static final int EXPOSED_RABBITMQ_PORT = 5672;
private static final int EXPORTED_RABBITMQ_PORT = 5673;
/**
* Start the rabbitmq.
*/
static {
final Consumer<CreateContainerCmd> rabbitCmd = e -> e.withPortBindings(new PortBinding(Ports.Binding.bindPort(EXPORTED_RABBITMQ_PORT), new ExposedPort(EXPOSED_RABBITMQ_PORT)));
final GenericContainer rabbitMq = new GenericContainer("rabbitmq:3-management").withExposedPorts(EXPOSED_RABBITMQ_PORT)
.withCreateContainerCmdModifier(rabbitCmd);
rabbitMq.start();
}....
}
И это мое исключение
[org.springframework.amqp.rabbit.core.RabbitTemplate]: Factory method 'rabbitTemplate' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'itRabbitMQConfig': Invocation of init method failed; nested exception is org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused (Connection refused)
Я предполагаю, что это как-то связано с разрешением имени хоста для localhost на gitlab.
Попробуй это:
static {
final GenericContainer rabbitMq = new GenericContainer("rabbitmq:3-management").withExposedPorts(EXPOSED_RABBITMQ_PORT);
rabbitMq.start();
// Pass the properties directly to the app. Do not use properties file.
System.setProperty("spring.rabbitmq.host", rabbitMq.getContainerIpAddress());
System.setProperty("spring.rabbitmq.port", rabbitMq.getMappedPort(5672).toString());
}