From e76844cf2df0018bc37100dc794c2925e7fff111 Mon Sep 17 00:00:00 2001 From: Andrej Zabka <andrej.zabka@mavenir.com> Date: Sat, 6 May 2023 10:51:55 +0200 Subject: [PATCH] fix: communication between modules inside docker --- README.md | 8 ++++---- .../cz/muni/pa165/service/ApplicationService.java | 13 +++++++++++-- docker-compose.yaml => compose.yaml | 4 ++++ .../cz/muni/pa165/service/CarComponentService.java | 13 +++++++++++-- .../main/java/cz/muni/pa165/service/CarService.java | 13 +++++++++++-- 5 files changed, 41 insertions(+), 10 deletions(-) rename docker-compose.yaml => compose.yaml (95%) diff --git a/README.md b/README.md index b4e9283..33e5653 100644 --- a/README.md +++ b/README.md @@ -5,8 +5,8 @@ - [Build and run the app](#build-and-run-the-app) - [Seed and clear DB](#seed-and-clear-db) - [Build and run the app with Docker](#build-and-run-the-app-with-docker) -- [Collecting Metrics](#collecting-metrics) -- [Grafana](#grafana) +- [Collecting Metrics](#collecting-and-displaying-metrics) +- [Grafana](#grafana-setup) ## About Project @@ -138,14 +138,14 @@ docker build -t pa165-formula-team-management-<module> --target pa165-formula-te To run specific built docker image use command ```bash -docker run -p 8090:8090 pa165-formula-team-management-<module> +docker run -p 8090:8090 -e DOCKER='1' pa165-formula-team-management-<module> ``` To avoid confusion the best way is to be consistent with ports listed above, e.g. for running `application` image use: ```bash -docker run -p 8081:8081 pa165-formula-team-management-application +docker run -p 8081:8081 -e DOCKER='1' pa165-formula-team-management-application ``` ### Docker compose diff --git a/application/src/main/java/cz/muni/pa165/service/ApplicationService.java b/application/src/main/java/cz/muni/pa165/service/ApplicationService.java index a2d8779..9706d53 100644 --- a/application/src/main/java/cz/muni/pa165/service/ApplicationService.java +++ b/application/src/main/java/cz/muni/pa165/service/ApplicationService.java @@ -26,17 +26,26 @@ import java.util.Optional; @Service public class ApplicationService { private static final Logger log = LoggerFactory.getLogger(ApplicationService.class); - private static final String NOTIFICATION_MODULE_URL = "http://localhost:8083"; + private static final String NOTIFICATION_MODULE_DOCKER = "http://notification:8083"; + private static final String NOTIFICATION_MODULE_LOCALHOST = "http://localhost:8083"; + private String NOTIFICATION_MODULE_URL; private static final String NOTIFICATION_MODULE_URI_NEW = "/notification/application/new"; private static final String NOTIFICATION_MODULE_URI_STATUS = "/notification/application/status"; private static final List<String> NOTIFICATION_RECEIVERS = List.of("formula.team.management@gmail.com"); - private final ApplicationRepository applicationRepository; private final WebClient webClient; @Autowired public ApplicationService(ApplicationRepository applicationRepository, WebClient.Builder webClientBuilder) { this.applicationRepository = applicationRepository; + + //if running in docker, modules cannot communicate through localhost + if (System.getenv("DOCKER") != null) { + NOTIFICATION_MODULE_URL = NOTIFICATION_MODULE_DOCKER; + } + else { + NOTIFICATION_MODULE_URL = NOTIFICATION_MODULE_LOCALHOST; + } this.webClient = webClientBuilder.baseUrl(NOTIFICATION_MODULE_URL).build(); } diff --git a/docker-compose.yaml b/compose.yaml similarity index 95% rename from docker-compose.yaml rename to compose.yaml index fffb702..7dd4db4 100644 --- a/docker-compose.yaml +++ b/compose.yaml @@ -11,6 +11,8 @@ services: - "8081:8081" extra_hosts: - "host.docker.internal:host-gateway" + environment: + - DOCKER=1 core: build: @@ -20,6 +22,8 @@ services: - "8090:8090" extra_hosts: - "host.docker.internal:host-gateway" + environment: + - DOCKER=1 notification: build: diff --git a/core/src/main/java/cz/muni/pa165/service/CarComponentService.java b/core/src/main/java/cz/muni/pa165/service/CarComponentService.java index 206402e..f7f6dfe 100644 --- a/core/src/main/java/cz/muni/pa165/service/CarComponentService.java +++ b/core/src/main/java/cz/muni/pa165/service/CarComponentService.java @@ -22,8 +22,9 @@ public class CarComponentService extends DomainService<CarComponent> { private static final Logger log = LoggerFactory.getLogger(CarComponentService.class); private final CarComponentRepository componentRepository; private final WebClient webClient; - - private static final String NOTIFICATION_MODULE_URL = "http://localhost:8083"; + private static final String NOTIFICATION_MODULE_DOCKER = "http://notification:8083"; + private static final String NOTIFICATION_MODULE_LOCALHOST = "http://localhost:8083"; + private String NOTIFICATION_MODULE_URL; private static final String NOTIFICATION_MODULE_URI = "/notification/component"; private static final List<String> NOTIFICATION_RECEIVERS = List.of("formula.team.management@gmail.com"); @@ -31,6 +32,14 @@ public class CarComponentService extends DomainService<CarComponent> { public CarComponentService(CarComponentRepository repository, WebClient.Builder webClientBuilder) { super(repository, CarComponent.class); this.componentRepository = repository; + + //if running in docker, modules cannot communicate through localhost + if (System.getenv("DOCKER") != null) { + NOTIFICATION_MODULE_URL = NOTIFICATION_MODULE_DOCKER; + } + else { + NOTIFICATION_MODULE_URL = NOTIFICATION_MODULE_LOCALHOST; + } this.webClient = webClientBuilder.baseUrl(NOTIFICATION_MODULE_URL).build(); } diff --git a/core/src/main/java/cz/muni/pa165/service/CarService.java b/core/src/main/java/cz/muni/pa165/service/CarService.java index 085b116..96d2646 100644 --- a/core/src/main/java/cz/muni/pa165/service/CarService.java +++ b/core/src/main/java/cz/muni/pa165/service/CarService.java @@ -21,7 +21,6 @@ import org.springframework.core.io.Resource; import org.springframework.web.client.RestClientException; import org.springframework.web.reactive.function.client.WebClient; -import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -30,7 +29,9 @@ import java.util.Optional; @Service public class CarService extends DomainService<Car> { private static final Logger log = LoggerFactory.getLogger(CarService.class); - private static final String VISUALIZATION_MODULE_URL = "http://localhost:8082"; + private static final String VISUALIZATION_MODULE_DOCKER = "http://visualization:8082"; + private static final String VISUALIZATION_MODULE_LOCALHOST = "http://localhost:8082"; + private String VISUALIZATION_MODULE_URL; private static final String VISUALIZATION_MODULE_URI = "/visualization"; private final DriverRepository driverRepository; private final CarRepository carRepository; @@ -44,6 +45,14 @@ public class CarService extends DomainService<Car> { this.driverRepository = driverRepository; this.carRepository = carRepository; this.carComponentRepository = carComponentRepository; + + //if running in docker, modules cannot communicate through localhost + if (System.getenv("DOCKER") != null) { + VISUALIZATION_MODULE_URL = VISUALIZATION_MODULE_DOCKER; + } + else { + VISUALIZATION_MODULE_URL = VISUALIZATION_MODULE_LOCALHOST; + } this.webClient = webClientBuilder.baseUrl(VISUALIZATION_MODULE_URL).build(); } -- GitLab