So I have 2 services. Kweet
(which is a tweet) and User
. When I run the 2 services manually + rest of the services in docker, it works. Rest of services include MongoDB
, RabbitMQ
, Spring Cloud Gateway
, Eureka Discovery
. But the moment I run the 2 (micro)services in Docker (I just use a docker compose), the rabbitmq functionality stops working. The normal API calls work, it’s specifically the RabbitMQ calls that fail.
RabbitMQ functionality:
EditUsername, edits username in user-service.
Than sends data via RabbitMQ (this is where it goes wrong I think) to kweet-service where it edits the username of a kweet.
Docker-compose file:
version: '3.8'
services:
eureka-service:
build: ./eureka-discovery-service
restart: always
container_name: eureka-service
ports:
- 8087:8087
api-gateway:
build: ./api-gateway
restart: always
container_name: api-gateway
depends_on:
- eureka-service
ports:
- 8080:8080
user:
build: ./user
restart: unless-stopped
container_name: user-ms
ports:
- 8081:8081
depends_on:
- eureka-service
kweet:
build: ./kweet
restart: unless-stopped
container_name: kweet-ms
depends_on:
- eureka-service
ports:
- 8082:8082
mongodb:
image: mongo
restart: always
container_name: mongodb
ports:
- 27017:27017
rabbitmq:
image: rabbitmq:management
restart: always
container_name: rabbitmq
hostname: rabbitmq
ports:
- 5672:5672
- 15672:15672
When I try to make a call the console shows:
user-ms | 2022-04-27 08:52:04.823 INFO 1 --- [nio-8081-exec-4] o.s.a.r.c.CachingConnectionFactory : Attempting to connect to: [localhost:5672]
The postman status I get back is 503 Service Unavailable
which isn’t from any try-catch
‘s I made. Anybody have any clue where the problem might be?
EDIT[ConnectionFactory]:
I tried to use the documentation and added a the CachingConnectionFactory
but it had the same result. Am I doing it wrong?
I added this to the RabbitMQ/Message-config (HOST, USERNAME, PASSWORD come from application.properties
:
@Bean
public AmqpTemplate template() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory(HOST);
connectionFactory.setUsername(USERNAME);
connectionFactory.setPassword(PASSWORD);
final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
rabbitTemplate.setMessageConverter(converter());
return rabbitTemplate;
}
EDIT [docker-compose]:
Found this source (https://www.linkedin.com/pulse/binding-your-docker-app-container-rabbitmq-phani-bushan/) that got rid of my 503 Service Unavailable
error. The problem I found now is that whenever I start up the containers, it generates new queues and exchanges that aren’t the ones I set up in my application.properties.
Now whenever I make a call, it shows this log:
user-ms | 2022-04-28 07:36:28.825 INFO 1 --- [nio-8081-exec-1] o.s.a.r.c.CachingConnectionFactory : Created new connection: rabbitConnectionFactory#2ca65ce4:0/SimpleCo
nnection@7e7052f [delegate=amqp://[email protected]:5672/, localPort= 43208]
Things tried:
- change host to [rabbitmq-container-name] in code via
CachingConnectionFactory
- change host to [rabbitmq-container-name] in docker compose with
environment: - spring_rabbitmq_host=[rabbitmq-container-name]
build: ./user
restart: unless-stopped
container_name: user-ms
depends_on:
- eureka-service
- rabbitmq
ports:
- 8081:8081
environment:
- spring_rabbitmq_host=[rabbitmq-container-name]
- Instead of [rabbitmq-container-name] I’ve tried
host.docker.internal
andlocalhost
3
Answers
I have made a grave mistake. 2 things that I did before it started working.
ADD
command toCOPY
(Don't think that was the problem though)docker-compose up
I should've been typingdocker-compose up --build
. This was most likely the issueTry to use the container with environment variables. For me it is enough for working.
I can open this container in a browser: http://localhost:15672/
I use PHP FPM and Symfony and I pass env value to this container to connect to rabbitmq.
For Java you need to find and define or redefine application properties. The config may use the default value for localhost, like this
spring.rabbitmq.host=localhost
, but you need to use Docker’s host, it israbbitmq
.When you dockerize your services are no longer listening in
localhost
. If you need to network connect services you need to use container_name instead of localhost.localhost points to the container itself, where only one service is listening. Do not mistake for when you develop on your laptop without containers, where everything is in localhost
More about this here
You must configure, somewhere in your
user-ms
application (we do not know what kind of applicatin is), that RabbitMQ service is listening atrabbitmq
(container_name) notlocalhost
.