From a4215e5a6bd8a6ea4db93bcb20b8aaceacb11812 Mon Sep 17 00:00:00 2001 From: iakovlysenko Date: Mon, 30 Jun 2025 00:39:30 +0300 Subject: [PATCH 1/7] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BF=D0=B0=D1=82=D1=82=D0=B5=D1=80=D0=BD?= =?UTF-8?q?=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api-gateway/pom.xml | 45 ++++++++++++++++ .../apigateway/ApiGatewayApplication.java | 11 ++++ .../src/main/resources/application.yml | 23 +++++++++ config-server/pom.xml | 45 ++++++++++++++++ .../configserver/ConfigServerApplication.java | 13 +++++ .../src/main/resources/application.yml | 15 ++++++ discovery-server/pom.xml | 41 +++++++++++++++ .../DiscoveryServerApplication.java | 13 +++++ .../src/main/resources/application.yml | 9 ++++ notification-service/pom.xml | 51 ++++++++++++++----- user-service/pom.xml | 17 ++++--- 11 files changed, 264 insertions(+), 19 deletions(-) create mode 100644 api-gateway/pom.xml create mode 100644 api-gateway/src/main/java/ru/project/iakov/apigateway/ApiGatewayApplication.java create mode 100644 api-gateway/src/main/resources/application.yml create mode 100644 config-server/pom.xml create mode 100644 config-server/src/main/java/ru/project/iakov/configserver/ConfigServerApplication.java create mode 100644 config-server/src/main/resources/application.yml create mode 100644 discovery-server/pom.xml create mode 100644 discovery-server/src/main/java/ru/project/iakov/discoveryserver/DiscoveryServerApplication.java create mode 100644 discovery-server/src/main/resources/application.yml diff --git a/api-gateway/pom.xml b/api-gateway/pom.xml new file mode 100644 index 0000000..8aaa85e --- /dev/null +++ b/api-gateway/pom.xml @@ -0,0 +1,45 @@ + + + 4.0.0 + + ru.project.iakov + api-gateway + 1.0-SNAPSHOT + + + org.springframework.boot + spring-boot-starter-parent + 3.2.5 + + + + + 21 + 2023.0.1 + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + + + + + org.springframework.cloud + spring-cloud-starter-gateway + + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-client + + + \ No newline at end of file diff --git a/api-gateway/src/main/java/ru/project/iakov/apigateway/ApiGatewayApplication.java b/api-gateway/src/main/java/ru/project/iakov/apigateway/ApiGatewayApplication.java new file mode 100644 index 0000000..29f365a --- /dev/null +++ b/api-gateway/src/main/java/ru/project/iakov/apigateway/ApiGatewayApplication.java @@ -0,0 +1,11 @@ +package ru.project.iakov.apigateway; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class ApiGatewayApplication { + public static void main(String[] args) { + SpringApplication.run(ApiGatewayApplication.class, args); + } +} diff --git a/api-gateway/src/main/resources/application.yml b/api-gateway/src/main/resources/application.yml new file mode 100644 index 0000000..bb2fe75 --- /dev/null +++ b/api-gateway/src/main/resources/application.yml @@ -0,0 +1,23 @@ +server: + port: 8082 +spring: + application: + name: api-gateway + cloud: + gateway: + routes: + - id: user-service + uri: lb://user-service + predicates: + - Path=/users/** + - id: notification-service + uri: lb://notification-service + predicates: + - Path=/api/v1/email/** + config: + import: optional:configserver:http://localhost:8888 + +eureka: + client: + service-url: + defaultZone: http://localhost:8761/eureka \ No newline at end of file diff --git a/config-server/pom.xml b/config-server/pom.xml new file mode 100644 index 0000000..b3d8605 --- /dev/null +++ b/config-server/pom.xml @@ -0,0 +1,45 @@ + + + 4.0.0 + + ru.project.iakov + config-server + 1.0-SNAPSHOT + + + org.springframework.boot + spring-boot-starter-parent + 3.2.5 + + + + + 21 + 2023.0.1 + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + + + + + org.springframework.cloud + spring-cloud-config-server + + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-client + + + \ No newline at end of file diff --git a/config-server/src/main/java/ru/project/iakov/configserver/ConfigServerApplication.java b/config-server/src/main/java/ru/project/iakov/configserver/ConfigServerApplication.java new file mode 100644 index 0000000..e93fcc0 --- /dev/null +++ b/config-server/src/main/java/ru/project/iakov/configserver/ConfigServerApplication.java @@ -0,0 +1,13 @@ +package ru.project.iakov.configserver; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.config.server.EnableConfigServer; + +@SpringBootApplication +@EnableConfigServer +public class ConfigServerApplication { + public static void main(String[] args) { + SpringApplication.run(ConfigServerApplication.class, args); + } +} \ No newline at end of file diff --git a/config-server/src/main/resources/application.yml b/config-server/src/main/resources/application.yml new file mode 100644 index 0000000..693e7b3 --- /dev/null +++ b/config-server/src/main/resources/application.yml @@ -0,0 +1,15 @@ +server: + port: 8888 +spring: + application: + name: config-server + cloud: + config: + server: + git: + uri: file:../config-repo + +eureka: + client: + service-url: + defaultZone: http://localhost:8761/eureka \ No newline at end of file diff --git a/discovery-server/pom.xml b/discovery-server/pom.xml new file mode 100644 index 0000000..2b2bcd8 --- /dev/null +++ b/discovery-server/pom.xml @@ -0,0 +1,41 @@ + + + 4.0.0 + + ru.project.iakov + discovery-server + 1.0-SNAPSHOT + + + org.springframework.boot + spring-boot-starter-parent + 3.2.5 + + + + + 21 + 2023.0.1 + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + + + + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-server + + + \ No newline at end of file diff --git a/discovery-server/src/main/java/ru/project/iakov/discoveryserver/DiscoveryServerApplication.java b/discovery-server/src/main/java/ru/project/iakov/discoveryserver/DiscoveryServerApplication.java new file mode 100644 index 0000000..dd7c274 --- /dev/null +++ b/discovery-server/src/main/java/ru/project/iakov/discoveryserver/DiscoveryServerApplication.java @@ -0,0 +1,13 @@ +package ru.project.iakov.discoveryserver; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; + +@SpringBootApplication +@EnableEurekaServer +public class DiscoveryServerApplication { + public static void main(String[] args) { + SpringApplication.run(DiscoveryServerApplication.class, args); + } +} \ No newline at end of file diff --git a/discovery-server/src/main/resources/application.yml b/discovery-server/src/main/resources/application.yml new file mode 100644 index 0000000..9652c74 --- /dev/null +++ b/discovery-server/src/main/resources/application.yml @@ -0,0 +1,9 @@ +server: + port: 8761 +spring: + application: + name: discovery-server +eureka: + client: + register-with-eureka: false + fetch-registry: false \ No newline at end of file diff --git a/notification-service/pom.xml b/notification-service/pom.xml index 3ee33e8..33a9651 100644 --- a/notification-service/pom.xml +++ b/notification-service/pom.xml @@ -4,52 +4,77 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 ru.project.iakov - user-service + notification-service 1.0-SNAPSHOT 24 24 UTF-8 + + + org.springframework.boot + spring-boot-starter-parent + 3.2.5 + + + + + + org.postgresql + postgresql + 42.7.5 + + + org.projectlombok lombok 1.18.38 provided + + org.springframework.boot spring-boot-starter-web - 3.5.0 + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot spring-boot-starter-test - 3.5.0 test + + org.springframework.kafka spring-kafka - 3.3.6 + + org.springframework.boot spring-boot-starter-mail - 3.4.6 + + - ru.project.iakov - user-service - 1.0-SNAPSHOT - compile + org.springdoc + springdoc-openapi-starter-webmvc-ui + 2.5.0 + + - ru.project.iakov - user-service - 1.0-SNAPSHOT - compile + org.springframework.boot + spring-boot-starter-hateoas \ No newline at end of file diff --git a/user-service/pom.xml b/user-service/pom.xml index 45a14a8..2331c95 100644 --- a/user-service/pom.xml +++ b/user-service/pom.xml @@ -7,13 +7,11 @@ ru.project.iakov user-service 1.0-SNAPSHOT - - 21 - 21 + 24 + 24 UTF-8 - org.springframework.boot spring-boot-starter-parent @@ -22,12 +20,14 @@ + org.postgresql postgresql 42.7.5 + org.projectlombok lombok @@ -35,41 +35,46 @@ provided + org.springframework.boot spring-boot-starter-web - org.springframework.boot spring-boot-starter-data-jpa + org.springframework.boot spring-boot-starter-test test + org.springframework.kafka spring-kafka + org.springframework.boot spring-boot-starter-mail + org.springdoc springdoc-openapi-starter-webmvc-ui 2.5.0 + org.springframework.boot spring-boot-starter-hateoas - + \ No newline at end of file From 2738352f28ec3d651318dad3c8cdfed0ae363e82 Mon Sep 17 00:00:00 2001 From: iakovlysenko Date: Mon, 30 Jun 2025 00:41:56 +0300 Subject: [PATCH 2/7] =?UTF-8?q?=D0=BB=D0=BE=D0=BA=D0=B0=D0=BB=D0=B8=D0=B7?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D1=8F=20=D1=81=D0=B5=D1=80=D0=B2=D0=B5=D1=80?= =?UTF-8?q?=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- user-service/src/main/resources/application.yml | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/user-service/src/main/resources/application.yml b/user-service/src/main/resources/application.yml index 39d62b0..be49582 100644 --- a/user-service/src/main/resources/application.yml +++ b/user-service/src/main/resources/application.yml @@ -1,4 +1,8 @@ spring: + application: + name: user-service + config: + import: optional:configserver:http://localhost:8888 datasource: url: jdbc:postgresql://localhost:5432/postgres username: postgres @@ -34,4 +38,8 @@ spring: key-serializer: org.apache.kafka.common.serialization.StringSerializer value-serializer: org.apache.kafka.common.serialization.StringSerializer server: - port: 8080 \ No newline at end of file + port: 8080 + eureka: + client: + service-url: + defaultZone: http://localhost:8761/eureka \ No newline at end of file From cf2413aa8770e5e680cbeee7c435592102bfa8ff Mon Sep 17 00:00:00 2001 From: iakovlysenko Date: Mon, 30 Jun 2025 00:48:16 +0300 Subject: [PATCH 3/7] circuitBreaker --- user-service/pom.xml | 27 +++++++++++++++++++ .../service/KafkaProducerService.java | 6 +++++ 2 files changed, 33 insertions(+) diff --git a/user-service/pom.xml b/user-service/pom.xml index 2331c95..2aad879 100644 --- a/user-service/pom.xml +++ b/user-service/pom.xml @@ -11,6 +11,7 @@ 24 24 UTF-8 + 2023.0.1 org.springframework.boot @@ -19,6 +20,18 @@ + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + + @@ -76,5 +89,19 @@ org.springframework.boot spring-boot-starter-hateoas + + + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-client + + + org.springframework.cloud + spring-cloud-starter-config + + + org.springframework.cloud + spring-cloud-starter-circuitbreaker-resilience4j + \ No newline at end of file diff --git a/user-service/src/main/java/ru/project/iakov/homework2/service/KafkaProducerService.java b/user-service/src/main/java/ru/project/iakov/homework2/service/KafkaProducerService.java index 8671267..1ec5172 100644 --- a/user-service/src/main/java/ru/project/iakov/homework2/service/KafkaProducerService.java +++ b/user-service/src/main/java/ru/project/iakov/homework2/service/KafkaProducerService.java @@ -3,6 +3,7 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.kafka.core.KafkaTemplate; +import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker; import org.springframework.stereotype.Service; @Service @@ -13,8 +14,13 @@ public class KafkaProducerService { private final KafkaTemplate kafkaTemplate; private static final String TOPIC = "user-events"; + @CircuitBreaker(name = "kafkaProducer", fallbackMethod = "sendUserEventFallback") public void sendUserEvent(String message) { log.info("Отправка сообщения в Kafka: {}", message); kafkaTemplate.send(TOPIC, message); } + + public void sendUserEventFallback(String message, Throwable throwable) { + log.error("Ошибка отправки в Kafka: {}", throwable.getMessage()); + } } \ No newline at end of file From f021cb6b40e8b85918adf30bc81b40115cb45c30 Mon Sep 17 00:00:00 2001 From: iakovlysenko Date: Tue, 1 Jul 2025 00:23:03 +0300 Subject: [PATCH 4/7] api gateway --- api-gateway/target/classes/application.yml | 23 ++++++++++ .../apigateway/ApiGatewayApplication.class | Bin 0 -> 773 bytes notification-service/pom.xml | 43 ++++++++---------- .../src/main/resources/application.yml | 10 +++- 4 files changed, 52 insertions(+), 24 deletions(-) create mode 100644 api-gateway/target/classes/application.yml create mode 100644 api-gateway/target/classes/ru/project/iakov/apigateway/ApiGatewayApplication.class diff --git a/api-gateway/target/classes/application.yml b/api-gateway/target/classes/application.yml new file mode 100644 index 0000000..bb2fe75 --- /dev/null +++ b/api-gateway/target/classes/application.yml @@ -0,0 +1,23 @@ +server: + port: 8082 +spring: + application: + name: api-gateway + cloud: + gateway: + routes: + - id: user-service + uri: lb://user-service + predicates: + - Path=/users/** + - id: notification-service + uri: lb://notification-service + predicates: + - Path=/api/v1/email/** + config: + import: optional:configserver:http://localhost:8888 + +eureka: + client: + service-url: + defaultZone: http://localhost:8761/eureka \ No newline at end of file diff --git a/api-gateway/target/classes/ru/project/iakov/apigateway/ApiGatewayApplication.class b/api-gateway/target/classes/ru/project/iakov/apigateway/ApiGatewayApplication.class new file mode 100644 index 0000000000000000000000000000000000000000..64e4729ead545af4b70ca1645b95a84d2967252b GIT binary patch literal 773 zcmb7CO>fgc5Ph2_bs7RKX(_b8fm@nGR|ttqP^B~yT+D|E3I|SWck52;>{{z}3qOk! zhyy>MKdNHZt&|*m!IEcozTWJPaC%DM}gSnK7icCJ)%TUQx@WEd~6HqBL8j=!yVWK$LxQ?e}^e%)xusa_wkfnotification-service 1.0-SNAPSHOT - 24 - 24 - UTF-8 + 21 + 2023.0.1 @@ -19,14 +18,19 @@ - - - - org.postgresql - postgresql - 42.7.5 - + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + + org.projectlombok @@ -35,15 +39,11 @@ provided - + org.springframework.boot spring-boot-starter-web - - org.springframework.boot - spring-boot-starter-data-jpa - @@ -64,17 +64,14 @@ spring-boot-starter-mail - + - org.springdoc - springdoc-openapi-starter-webmvc-ui - 2.5.0 + org.springframework.cloud + spring-cloud-starter-netflix-eureka-client - - - org.springframework.boot - spring-boot-starter-hateoas + org.springframework.cloud + spring-cloud-starter-config \ No newline at end of file diff --git a/notification-service/src/main/resources/application.yml b/notification-service/src/main/resources/application.yml index be161d0..c457813 100644 --- a/notification-service/src/main/resources/application.yml +++ b/notification-service/src/main/resources/application.yml @@ -1,4 +1,8 @@ spring: + application: + name: notification-service + config: + import: optional:configserver:http://localhost:8888 kafka: bootstrap-servers: localhost:9092 consumer: @@ -21,4 +25,8 @@ logging: root: INFO server: - port: 8081 \ No newline at end of file + port: 8081 +eureka: + client: + service-url: + defaultZone: http://localhost:8761/eureka \ No newline at end of file From 41e306af619bb9a7ad37f5975351f9e10859fd27 Mon Sep 17 00:00:00 2001 From: iakovlysenko Date: Tue, 1 Jul 2025 00:24:02 +0300 Subject: [PATCH 5/7] =?UTF-8?q?config=20server=20=D0=B8=20discovery?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config-server/target/classes/application.yml | 15 +++++++++++++++ .../configserver/ConfigServerApplication.class | Bin 0 -> 850 bytes discovery-server/target/classes/application.yml | 9 +++++++++ .../DiscoveryServerApplication.class | Bin 0 -> 873 bytes 4 files changed, 24 insertions(+) create mode 100644 config-server/target/classes/application.yml create mode 100644 config-server/target/classes/ru/project/iakov/configserver/ConfigServerApplication.class create mode 100644 discovery-server/target/classes/application.yml create mode 100644 discovery-server/target/classes/ru/project/iakov/discoveryserver/DiscoveryServerApplication.class diff --git a/config-server/target/classes/application.yml b/config-server/target/classes/application.yml new file mode 100644 index 0000000..693e7b3 --- /dev/null +++ b/config-server/target/classes/application.yml @@ -0,0 +1,15 @@ +server: + port: 8888 +spring: + application: + name: config-server + cloud: + config: + server: + git: + uri: file:../config-repo + +eureka: + client: + service-url: + defaultZone: http://localhost:8761/eureka \ No newline at end of file diff --git a/config-server/target/classes/ru/project/iakov/configserver/ConfigServerApplication.class b/config-server/target/classes/ru/project/iakov/configserver/ConfigServerApplication.class new file mode 100644 index 0000000000000000000000000000000000000000..8ab5ee14f473db330079f442c2d0fd79fc64fe76 GIT binary patch literal 850 zcmbVLO>fgc5Ph2_b($Jl(o!fCByMRA>Z4xHA`*4@_Gjn=z?zr_i} zp+A5hg_t##>Vvsp$upXnosWHQ_Sf&9*8tA&cz_NzB6MT)u*tA{Db|7)Lg)NldMPu< zuz8}ia;FR%qvLsmKEpHX`O2CQ=1N?dHP4JbS9vMzT3SA>j9F!ztyYD~gi}TjFu-<% zIL0m9W*8fr^KxaC&d;q_$}h%V@zfZ{XO**&Nv*Bd4CyGj8E;yMvYdQwcxEmXn;a)? zm1#mJzq)!jZ$(AS34NB-DFO2VRLG**AgbS-bi<07Vkv>&`C=N2Q%YsCSR*C^3gx2cp6}2 zIQ-zXQ%gBlr3!FpZCpha)j4fNvEoN~X9!Lr+*?_*^^%Bdz)2T7X)TZy#xLr1;q|_} z(gD!ss1rOvM2i`|4p>-Ck~?H^(u{O({2lCD72YKo!oU{pkc|Hxzz}@&Q5PeFMI!z5NDHO_Ahg+Hhx^XddrljR$K87&iz)5x#SB)cOWI2?d#R-N3 zKY$;_uyVuH2Xi7b*`3|hzLnlv|N8yoCx84T5T*%Zh zY(7_7xnqWn!OIxC?>=?w8&@hR7`y(9%|ywclF8v)YXlvA#y zo8{_~%hJlZ;OiYa)1Kk8DtEO;5uPBRef7Twf=n0NXS)tYLJqkGqt=NB<7VMXrT|%HGEV L^1kgM`A5JXBZ>2k literal 0 HcmV?d00001 From 51533b4a8aefa9314f1a70605c2c3d3b9b5598ae Mon Sep 17 00:00:00 2001 From: Iakov Lysenko Date: Tue, 8 Jul 2025 23:34:09 +0300 Subject: [PATCH 6/7] docker ready --- api-gateway/Dockerfile | 6 + api-gateway/pom.xml | 34 +++++ .../src/main/resources/application-docker.yml | 22 +++ config-server/Dockerfile | 6 + config-server/pom.xml | 34 +++++ .../src/main/resources/application-docker.yml | 17 +++ discovery-server/Dockerfile | 6 + discovery-server/pom.xml | 34 +++++ .../src/main/resources/application-docker.yml | 11 ++ docker-compose.yml | 113 +++++++++++++++ notification-service/Dockerfile | 6 + notification-service/pom.xml | 45 +++++- .../src/main/resources/application-docker.yml | 24 ++++ user-service/Dockerfile | 6 + user-service/pom.xml | 44 +++++- .../project/iakov/homework2/dto/UserDto.java | 2 +- .../src/main/resources/application-docker.yml | 22 +++ .../src/main/resources/application.yml | 8 +- .../src/main/resources/hibernate.cfg.xml | 25 ---- .../src/main/resources/log4j.properties | 7 - .../controller/UserControllerTest.java | 132 ------------------ 21 files changed, 427 insertions(+), 177 deletions(-) create mode 100644 api-gateway/Dockerfile create mode 100644 api-gateway/src/main/resources/application-docker.yml create mode 100644 config-server/Dockerfile create mode 100644 config-server/src/main/resources/application-docker.yml create mode 100644 discovery-server/Dockerfile create mode 100644 discovery-server/src/main/resources/application-docker.yml create mode 100644 docker-compose.yml create mode 100644 notification-service/Dockerfile create mode 100644 notification-service/src/main/resources/application-docker.yml create mode 100644 user-service/Dockerfile create mode 100644 user-service/src/main/resources/application-docker.yml delete mode 100644 user-service/src/main/resources/hibernate.cfg.xml delete mode 100644 user-service/src/main/resources/log4j.properties delete mode 100644 user-service/src/test/java/ru/project/iakov/homework2/controller/UserControllerTest.java diff --git a/api-gateway/Dockerfile b/api-gateway/Dockerfile new file mode 100644 index 0000000..3b78cf0 --- /dev/null +++ b/api-gateway/Dockerfile @@ -0,0 +1,6 @@ +FROM eclipse-temurin:21-jdk-alpine +LABEL authors="iakovlysenko" +VOLUME /tmp +ARG JAR_FILE=target/api-gateway-1.0-SNAPSHOT.jar +COPY ${JAR_FILE} app.jar +ENTRYPOINT ["java","-jar","/app.jar"] \ No newline at end of file diff --git a/api-gateway/pom.xml b/api-gateway/pom.xml index 8aaa85e..ef025ff 100644 --- a/api-gateway/pom.xml +++ b/api-gateway/pom.xml @@ -20,6 +20,40 @@ 2023.0.1 + + + + + org.springframework.boot + spring-boot-maven-plugin + 3.2.5 + + + + repackage + + + + + + + maven-compiler-plugin + 3.11.0 + + 21 + 21 + + + org.projectlombok + lombok + 1.18.38 + + + + + + + diff --git a/api-gateway/src/main/resources/application-docker.yml b/api-gateway/src/main/resources/application-docker.yml new file mode 100644 index 0000000..f2b5859 --- /dev/null +++ b/api-gateway/src/main/resources/application-docker.yml @@ -0,0 +1,22 @@ +server: + port: 8080 + +spring: + application: + name: api-gateway + cloud: + gateway: + routes: + - id: user-service + uri: lb://user-service + predicates: + - Path=/users/** + - id: notification-service + uri: lb://notification-service + predicates: + - Path=/api/v1/email/** + +eureka: + client: + service-url: + defaultZone: http://discovery-server:8761/eureka \ No newline at end of file diff --git a/config-server/Dockerfile b/config-server/Dockerfile new file mode 100644 index 0000000..a2e8c68 --- /dev/null +++ b/config-server/Dockerfile @@ -0,0 +1,6 @@ +FROM eclipse-temurin:21-jdk-alpine +LABEL authors="iakovlysenko" +VOLUME /tmp +ARG JAR_FILE=target/config-server-1.0-SNAPSHOT.jar +COPY ${JAR_FILE} app.jar +ENTRYPOINT ["java","-jar","/app.jar"] \ No newline at end of file diff --git a/config-server/pom.xml b/config-server/pom.xml index b3d8605..d777305 100644 --- a/config-server/pom.xml +++ b/config-server/pom.xml @@ -20,6 +20,40 @@ 2023.0.1 + + + + + org.springframework.boot + spring-boot-maven-plugin + 3.2.5 + + + + repackage + + + + + + + maven-compiler-plugin + 3.11.0 + + 21 + 21 + + + org.projectlombok + lombok + 1.18.38 + + + + + + + diff --git a/config-server/src/main/resources/application-docker.yml b/config-server/src/main/resources/application-docker.yml new file mode 100644 index 0000000..cc9e035 --- /dev/null +++ b/config-server/src/main/resources/application-docker.yml @@ -0,0 +1,17 @@ +server: + port: 8888 + +spring: + profiles: + active: native + cloud: + config: + server: + native: + search-locations: file:/config + + +eureka: + client: + service-url: + defaultZone: http://discovery-server:8761/eureka/ \ No newline at end of file diff --git a/discovery-server/Dockerfile b/discovery-server/Dockerfile new file mode 100644 index 0000000..3d01bf0 --- /dev/null +++ b/discovery-server/Dockerfile @@ -0,0 +1,6 @@ +FROM eclipse-temurin:21-jdk-alpine +LABEL authors="iakovlysenko" +VOLUME /tmp +ARG JAR_FILE=target/discovery-server-1.0-SNAPSHOT.jar +COPY ${JAR_FILE} app.jar +ENTRYPOINT ["java","-jar","/app.jar"] \ No newline at end of file diff --git a/discovery-server/pom.xml b/discovery-server/pom.xml index 2b2bcd8..e6f040c 100644 --- a/discovery-server/pom.xml +++ b/discovery-server/pom.xml @@ -20,6 +20,40 @@ 2023.0.1 + + + + + org.springframework.boot + spring-boot-maven-plugin + 3.2.5 + + + + repackage + + + + + + + maven-compiler-plugin + 3.11.0 + + 21 + 21 + + + org.projectlombok + lombok + 1.18.38 + + + + + + + diff --git a/discovery-server/src/main/resources/application-docker.yml b/discovery-server/src/main/resources/application-docker.yml new file mode 100644 index 0000000..40de21b --- /dev/null +++ b/discovery-server/src/main/resources/application-docker.yml @@ -0,0 +1,11 @@ +server: + port: 8761 + +spring: + application: + name: discovery-server + +eureka: + client: + register-with-eureka: false + fetch-registry: false \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..3c686cd --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,113 @@ +services: + + postgres: + image: postgres:15 + container_name: postgres + restart: unless-stopped + user: postgres + environment: + POSTGRES_DB: postgres + PGUSER: postgres + POSTGRES_PASSWORD: postgres + env_file: + - .env + ports: + - "5432:5432" + volumes: + - pgdata:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U $$POSTGRES_USER $$POSTGRES_DB"] + interval: 10s + timeout: 5s + retries: 5 + + zookeeper: + image: confluentinc/cp-zookeeper:7.4.0 + container_name: zookeeper + environment: + ZOOKEEPER_CLIENT_PORT: 2181 + ports: + - "2181:2181" + + kafka: + image: confluentinc/cp-kafka:7.4.0 + container_name: kafka + depends_on: + - zookeeper + ports: + - "9092:9092" + environment: + KAFKA_BROKER_ID: 1 + KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181 + KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092 + KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1 + + discovery-server: + container_name: discovery-server + image: discovery-server:latest + build: + context: ./discovery-server + ports: + - "8761:8761" + + config-server: + image: config-server:latest + build: + context: ./config-server + ports: + - "8888:8888" + volumes: + - ./config:/config + depends_on: + - discovery-server + + user-service: + image: user-service:latest + build: + context: ./user-service + ports: + - "8080:8080" + depends_on: + - discovery-server + - kafka + - postgres + - config-server + environment: + - SPRING_PROFILES_ACTIVE=docker + - EMAIL_USERNAME=${EMAIL_USERNAME} + - EMAIL_PASSWORD=${EMAIL_PASSWORD} + - DB_USERNAME=${DB_USERNAME} + - DB_PASSWORD=${DB_PASSWORD} + env_file: + - .env + + notification-service: + image: notification-service:latest + build: + context: ./notification-service + ports: + - "8081:8081" + depends_on: + - kafka + - config-server + environment: + - SPRING_PROFILES_ACTIVE=docker + - EMAIL_USERNAME=${EMAIL_USERNAME} + - EMAIL_PASSWORD=${EMAIL_PASSWORD} + env_file: + - .env + + api-gateway: + image: api-gateway:latest + build: + context: ./api-gateway + ports: + - "8765:8765" + depends_on: + - discovery-server + - config-server + environment: + - SPRING_PROFILES_ACTIVE=docker +volumes: + pgdata: + diff --git a/notification-service/Dockerfile b/notification-service/Dockerfile new file mode 100644 index 0000000..85e14b1 --- /dev/null +++ b/notification-service/Dockerfile @@ -0,0 +1,6 @@ +FROM eclipse-temurin:21-jdk-alpine +LABEL authors="iakovlysenko" +VOLUME /tmp +ARG JAR_FILE=target/notification-service-1.0-SNAPSHOT.jar +COPY ${JAR_FILE} app.jar +ENTRYPOINT ["java","-jar","/app.jar"] \ No newline at end of file diff --git a/notification-service/pom.xml b/notification-service/pom.xml index 65b9760..0580104 100644 --- a/notification-service/pom.xml +++ b/notification-service/pom.xml @@ -7,10 +7,11 @@ notification-service 1.0-SNAPSHOT - 21 + 21 + 21 + UTF-8 2023.0.1 - org.springframework.boot spring-boot-starter-parent @@ -18,6 +19,40 @@ + + + + + org.springframework.boot + spring-boot-maven-plugin + 3.2.5 + + + + repackage + + + + + + + maven-compiler-plugin + 3.11.0 + + 21 + 21 + + + org.projectlombok + lombok + 1.18.38 + + + + + + + @@ -39,7 +74,7 @@ provided - + org.springframework.boot spring-boot-starter-web @@ -64,11 +99,13 @@ spring-boot-starter-mail - + org.springframework.cloud spring-cloud-starter-netflix-eureka-client + + org.springframework.cloud spring-cloud-starter-config diff --git a/notification-service/src/main/resources/application-docker.yml b/notification-service/src/main/resources/application-docker.yml new file mode 100644 index 0000000..1d13a69 --- /dev/null +++ b/notification-service/src/main/resources/application-docker.yml @@ -0,0 +1,24 @@ +server: + port: 8082 + +spring: + application: + name: notification-service + + kafka: + bootstrap-servers: kafka:9092 + + mail: + host: smtp.mail.ru + port: 465 + username: ${EMAIL_USERNAME} + password: ${EMAIL_PASSWORD} + protocol: smtps + properties: + mail.smtp.auth: true + mail.smtp.starttls.enable: true + +eureka: + client: + service-url: + defaultZone: http://discovery-server:8761/eureka \ No newline at end of file diff --git a/user-service/Dockerfile b/user-service/Dockerfile new file mode 100644 index 0000000..740935f --- /dev/null +++ b/user-service/Dockerfile @@ -0,0 +1,6 @@ +FROM eclipse-temurin:21-jdk-alpine +LABEL authors="iakovlysenko" +VOLUME /tmp +ARG JAR_FILE=target/user-service-1.0-SNAPSHOT.jar +COPY ${JAR_FILE} app.jar +ENTRYPOINT ["java","-jar","/app.jar"] \ No newline at end of file diff --git a/user-service/pom.xml b/user-service/pom.xml index 2aad879..dd877c1 100644 --- a/user-service/pom.xml +++ b/user-service/pom.xml @@ -4,12 +4,12 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - ru.project.iakov + ru.project.iakov.homework2 user-service 1.0-SNAPSHOT - 24 - 24 + 21 + 21 UTF-8 2023.0.1 @@ -20,6 +20,40 @@ + + + + + org.springframework.boot + spring-boot-maven-plugin + 3.2.5 + + + + repackage + + + + + + + maven-compiler-plugin + 3.11.0 + + 21 + 21 + + + org.projectlombok + lombok + 1.18.38 + + + + + + + @@ -90,11 +124,13 @@ spring-boot-starter-hateoas - + org.springframework.cloud spring-cloud-starter-netflix-eureka-client + + org.springframework.cloud spring-cloud-starter-config diff --git a/user-service/src/main/java/ru/project/iakov/homework2/dto/UserDto.java b/user-service/src/main/java/ru/project/iakov/homework2/dto/UserDto.java index 7660dce..00156a2 100644 --- a/user-service/src/main/java/ru/project/iakov/homework2/dto/UserDto.java +++ b/user-service/src/main/java/ru/project/iakov/homework2/dto/UserDto.java @@ -1,6 +1,6 @@ package ru.project.iakov.homework2.dto; -import lombok.*; +import lombok.*; import java.time.LocalDateTime; @Data diff --git a/user-service/src/main/resources/application-docker.yml b/user-service/src/main/resources/application-docker.yml new file mode 100644 index 0000000..878444c --- /dev/null +++ b/user-service/src/main/resources/application-docker.yml @@ -0,0 +1,22 @@ +server: + port: 8081 + +spring: + application: + name: user-service + datasource: + url: jdbc:postgresql://postgres:5432/postgres + username: ${DB_USERNAME} + password: ${DB_PASSWORD} + jpa: + hibernate: + ddl-auto: update + show-sql: true + + kafka: + bootstrap-servers: kafka:9092 + +eureka: + client: + service-url: + defaultZone: http://discovery-server:8761/eureka \ No newline at end of file diff --git a/user-service/src/main/resources/application.yml b/user-service/src/main/resources/application.yml index be49582..e93f89c 100644 --- a/user-service/src/main/resources/application.yml +++ b/user-service/src/main/resources/application.yml @@ -2,11 +2,11 @@ spring: application: name: user-service config: - import: optional:configserver:http://localhost:8888 + import: optional:configserver:http://configserver:8888 datasource: - url: jdbc:postgresql://localhost:5432/postgres - username: postgres - password: postgres + url: jdbc:postgresql://postgres:5432/postgres + username: ${DB_USERNAME} + password: ${DB_PASSWORD} jpa: hibernate: ddl-auto: update diff --git a/user-service/src/main/resources/hibernate.cfg.xml b/user-service/src/main/resources/hibernate.cfg.xml deleted file mode 100644 index 1515a41..0000000 --- a/user-service/src/main/resources/hibernate.cfg.xml +++ /dev/null @@ -1,25 +0,0 @@ - \ No newline at end of file diff --git a/user-service/src/main/resources/log4j.properties b/user-service/src/main/resources/log4j.properties deleted file mode 100644 index 7aabc61..0000000 --- a/user-service/src/main/resources/log4j.properties +++ /dev/null @@ -1,7 +0,0 @@ -log4j.rootLogger=INFO, stdout -log4j.appender.stdout=org.apache.log4j.ConsoleAppender -log4j.appender.stdout.Target=System.out -log4j.appender.stdout.layout=org.apache.log4j.PatternLayout -log4j.appender.stdout.layout.ConversionPattern=%d{HH:mm:ss} %-5p [%c] - %m%n -log4j.logger.org.hibernate.SQL=DEBUG -log4j.logger.org.hibernate.type.descriptor.sql.BasicBinder=TRACE \ No newline at end of file diff --git a/user-service/src/test/java/ru/project/iakov/homework2/controller/UserControllerTest.java b/user-service/src/test/java/ru/project/iakov/homework2/controller/UserControllerTest.java deleted file mode 100644 index 5fea5ee..0000000 --- a/user-service/src/test/java/ru/project/iakov/homework2/controller/UserControllerTest.java +++ /dev/null @@ -1,132 +0,0 @@ -package ru.project.iakov.homework2.controller; - -import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; -import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.http.MediaType; -import org.springframework.test.web.servlet.MockMvc; -import ru.project.iakov.homework2.dto.UserDto; -import ru.project.iakov.homework2.service.UserService; - -import java.time.LocalDateTime; -import java.util.List; - -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.BDDMockito.given; -import static org.mockito.Mockito.doNothing; -import static org.mockito.Mockito.doThrow; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; - -@WebMvcTest(UserController.class) -class UserControllerTest { - - @Autowired - private MockMvc mockMvc; - - @MockBean - private UserService userService; - - @Autowired - private ObjectMapper objectMapper; - - @DisplayName("Пользователь найден по ID") - @Test - void getUserById_whenUserExist() throws Exception { - UserDto userDto = new UserDto(1L, "Ivan", "ivan@mail.ru", 30, LocalDateTime.now()); - given(userService.findById(1L)).willReturn(userDto); - - mockMvc.perform(get("/users/1")) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.name").value("Ivan")) - .andExpect(jsonPath("$.email").value("ivan@mail.ru")); - } - - @DisplayName("Такого пользователя нет") - @Test - void getUserById_whenUserNotExist() throws Exception { - given(userService.findById(999L)).willThrow(new IllegalArgumentException()); - - mockMvc.perform(get("/users/999")) - .andExpect(result -> assertTrue(result.getResolvedException() instanceof IllegalArgumentException)); - } - - @DisplayName("Список пользователей") - @Test - void getAllUsers() throws Exception { - List users = List.of( - new UserDto(1L, "Ivan", "ivan@mail.ru", 30, LocalDateTime.now()), - new UserDto(2L, "Petr", "petr@mail.ru", 28, LocalDateTime.now()) - ); - given(userService.findAll()).willReturn(users); - - mockMvc.perform(get("/users")) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.length()").value(2)); - } - - @DisplayName("Создание пользователя") - @Test - void createUser() throws Exception { - UserDto toCreate = new UserDto(null, "Ivan", "ivan@mail.ru", 30, LocalDateTime.now()); - UserDto created = new UserDto(10L, "Petr", "petr@mail.ru", 28, LocalDateTime.now()); - - given(userService.createUser(any(UserDto.class))).willReturn(created); - - mockMvc.perform(post("/users") - .contentType(MediaType.APPLICATION_JSON) - .content(objectMapper.writeValueAsString(toCreate))) - .andExpect(status().isCreated()) - .andExpect(jsonPath("$.id").value(10)); - } - - @DisplayName("Обновление пользователя") - @Test - void updateUser_whenUserExist() throws Exception { - UserDto update = new UserDto(null, "Ivan", "ivan@mail.ru", 30, LocalDateTime.now()); - UserDto updated = new UserDto(1L, "Petr", "petr@mail.ru", 28, LocalDateTime.now()); - - given(userService.updateUser(eq(1L), any(UserDto.class))).willReturn(updated); - - mockMvc.perform(put("/users/1") - .contentType(MediaType.APPLICATION_JSON) - .content(objectMapper.writeValueAsString(update))) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.name").value("Petr")); - } - - @DisplayName("При обновлении пользователь не найден") - @Test - void updateUser_whenUserIsNotExist() throws Exception { - given(userService.updateUser(eq(999L), any(UserDto.class))) - .willThrow(new IllegalArgumentException()); - - mockMvc.perform(put("/users/999") - .contentType(MediaType.APPLICATION_JSON) - .content(objectMapper.writeValueAsString(new UserDto(1L, "Ivan", "ivan@mail.ru", 30, LocalDateTime.now())))) - .andExpect(result -> assertTrue(result.getResolvedException() instanceof IllegalArgumentException)); - } - - @DisplayName("Успешное удаление пользователя") - @Test - void deleteUserSuccess_whenUserExist() throws Exception { - doNothing().when(userService).deleteUser(1L); - - mockMvc.perform(delete("/users/1")) - .andExpect(status().isNoContent()); - } - - @DisplayName("При удалении пользователь не найден") - @Test - void deleteUser_whenUserIsNotExist() throws Exception { - doThrow(new IllegalArgumentException()).when(userService).deleteUser(999L); - - mockMvc.perform(delete("/users/999")) - .andExpect(result -> assertTrue(result.getResolvedException() instanceof IllegalArgumentException)); - } -} \ No newline at end of file From 8c7e2a42c54a6ee68961047fe7e62281037eaa0c Mon Sep 17 00:00:00 2001 From: Iakov Lysenko Date: Wed, 9 Jul 2025 07:12:28 +0300 Subject: [PATCH 7/7] fix#1 --- .../src/main/resources/application-docker.yml | 3 ++- config-repo/application.yml | 10 ++++++++++ config-repo/config-server.yml | 6 ++++++ config-repo/notification-service.yml | 20 +++++++++++++++++++ config-repo/user-service.yml | 17 ++++++++++++++++ .../src/main/resources/application-docker.yml | 2 -- .../src/main/resources/application.yml | 3 +-- config-server/target/classes/application.yml | 3 +-- docker-compose.yml | 8 +++++--- .../src/main/resources/application-docker.yml | 2 +- .../src/main/resources/application-docker.yml | 2 +- 11 files changed, 64 insertions(+), 12 deletions(-) create mode 100644 config-repo/application.yml create mode 100644 config-repo/config-server.yml create mode 100644 config-repo/notification-service.yml create mode 100644 config-repo/user-service.yml diff --git a/api-gateway/src/main/resources/application-docker.yml b/api-gateway/src/main/resources/application-docker.yml index f2b5859..f038d5a 100644 --- a/api-gateway/src/main/resources/application-docker.yml +++ b/api-gateway/src/main/resources/application-docker.yml @@ -15,7 +15,8 @@ spring: uri: lb://notification-service predicates: - Path=/api/v1/email/** - + config: + import: optional:configserver:http://config-server:8888 eureka: client: service-url: diff --git a/config-repo/application.yml b/config-repo/application.yml new file mode 100644 index 0000000..8756ed0 --- /dev/null +++ b/config-repo/application.yml @@ -0,0 +1,10 @@ +eureka: + client: + register-with-eureka: true + fetch-registry: true + service-url: + defaultZone: http://discovery-server:8761/eureka/ + +spring: + application: + name: default \ No newline at end of file diff --git a/config-repo/config-server.yml b/config-repo/config-server.yml new file mode 100644 index 0000000..1bba46d --- /dev/null +++ b/config-repo/config-server.yml @@ -0,0 +1,6 @@ +server: + port: 8888 + +spring: + application: + name: config-server \ No newline at end of file diff --git a/config-repo/notification-service.yml b/config-repo/notification-service.yml new file mode 100644 index 0000000..faf13b1 --- /dev/null +++ b/config-repo/notification-service.yml @@ -0,0 +1,20 @@ +server: + port: 8082 + +spring: + application: + name: notification-service + mail: + host: smtp.mail.ru + port: 465 + username: ${EMAIL_USERNAME} + password: ${EMAIL_PASSWORD} + properties: + mail: + smtp: + auth: true + starttls: + enable: true + +kafka: + topic: user-events \ No newline at end of file diff --git a/config-repo/user-service.yml b/config-repo/user-service.yml new file mode 100644 index 0000000..ca8ba01 --- /dev/null +++ b/config-repo/user-service.yml @@ -0,0 +1,17 @@ +server: + port: 8081 + +spring: + application: + name: user-service + datasource: + url: jdbc:postgresql://postgres:5432/postgres + username: postgres + password: postgres + jpa: + hibernate: + ddl-auto: update + show-sql: true + properties: + hibernate: + format_sql: true \ No newline at end of file diff --git a/config-server/src/main/resources/application-docker.yml b/config-server/src/main/resources/application-docker.yml index cc9e035..d7768f3 100644 --- a/config-server/src/main/resources/application-docker.yml +++ b/config-server/src/main/resources/application-docker.yml @@ -2,8 +2,6 @@ server: port: 8888 spring: - profiles: - active: native cloud: config: server: diff --git a/config-server/src/main/resources/application.yml b/config-server/src/main/resources/application.yml index 693e7b3..6020c3a 100644 --- a/config-server/src/main/resources/application.yml +++ b/config-server/src/main/resources/application.yml @@ -7,8 +7,7 @@ spring: config: server: git: - uri: file:../config-repo - + uri: file:/config eureka: client: service-url: diff --git a/config-server/target/classes/application.yml b/config-server/target/classes/application.yml index 693e7b3..6020c3a 100644 --- a/config-server/target/classes/application.yml +++ b/config-server/target/classes/application.yml @@ -7,8 +7,7 @@ spring: config: server: git: - uri: file:../config-repo - + uri: file:/config eureka: client: service-url: diff --git a/docker-compose.yml b/docker-compose.yml index 3c686cd..4a330a5 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -57,16 +57,18 @@ services: ports: - "8888:8888" volumes: - - ./config:/config + - ./config-repo:/config depends_on: - discovery-server + environment: + - SPRING_PROFILES_ACTIVE=docker user-service: image: user-service:latest build: context: ./user-service ports: - - "8080:8080" + - "8082:8082" depends_on: - discovery-server - kafka @@ -102,7 +104,7 @@ services: build: context: ./api-gateway ports: - - "8765:8765" + - "8080:8080" depends_on: - discovery-server - config-server diff --git a/notification-service/src/main/resources/application-docker.yml b/notification-service/src/main/resources/application-docker.yml index 1d13a69..8bef37d 100644 --- a/notification-service/src/main/resources/application-docker.yml +++ b/notification-service/src/main/resources/application-docker.yml @@ -1,5 +1,5 @@ server: - port: 8082 + port: 8081 spring: application: diff --git a/user-service/src/main/resources/application-docker.yml b/user-service/src/main/resources/application-docker.yml index 878444c..e957962 100644 --- a/user-service/src/main/resources/application-docker.yml +++ b/user-service/src/main/resources/application-docker.yml @@ -1,5 +1,5 @@ server: - port: 8081 + port: 8082 spring: application: