From 948fc10335baef784f48da8cb3fa2f6845a22f76 Mon Sep 17 00:00:00 2001 From: dksifoua Date: Thu, 12 Sep 2024 22:09:47 -0400 Subject: [PATCH] Setup netflix eureka service discovery --- .github/workflows/eureka.yaml | 31 +++++++++++++++ README.md | 3 +- Taskfile.yaml | 6 ++- api-gateway/build.gradle | 1 + .../src/main/resources/application.yaml | 5 +++ .../gateway/ApiGatewayApplicationTests.java | 12 ++++-- catalog-service/build.gradle | 11 ++++++ .../eshop/catalog/handler/IndexHandler.java | 3 +- .../src/main/resources/application.yaml | 5 +++ .../CatalogServiceApplicationTests.java | 13 +++++-- eureka-server/.gitignore | 37 ++++++++++++++++++ eureka-server/Taskfile.yaml | 19 ++++++++++ eureka-server/build.gradle | 38 +++++++++++++++++++ eureka-server/settings.gradle | 1 + .../eshop/eureka/EurekaServerApplication.java | 15 ++++++++ .../src/main/resources/application.yaml | 11 ++++++ .../eureka/EurekaServerApplicationTests.java | 14 +++++++ settings.gradle | 3 +- 18 files changed, 218 insertions(+), 10 deletions(-) create mode 100644 .github/workflows/eureka.yaml create mode 100644 eureka-server/.gitignore create mode 100644 eureka-server/Taskfile.yaml create mode 100644 eureka-server/build.gradle create mode 100644 eureka-server/settings.gradle create mode 100644 eureka-server/src/main/java/io/dksifoua/eshop/eureka/EurekaServerApplication.java create mode 100644 eureka-server/src/main/resources/application.yaml create mode 100644 eureka-server/src/test/java/io/dksifoua/eshop/eureka/EurekaServerApplicationTests.java diff --git a/.github/workflows/eureka.yaml b/.github/workflows/eureka.yaml new file mode 100644 index 0000000..2ace342 --- /dev/null +++ b/.github/workflows/eureka.yaml @@ -0,0 +1,31 @@ +name: eureka + +on: + push: + branches: + - main + - develop + - feature/** + pull_request: + branches: + - main + - develop + - feature/** + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Setup Java + uses: actions/setup-java@v4 + with: + distribution: oracle + java-version: 21 + - name: Setup Task + uses: arduino/setup-task@v2 + with: + version: 3.x + repo-token: ${{ secrets.GH_TOKEN }} + - name: Test eureka server + run: task eureka:test \ No newline at end of file diff --git a/README.md b/README.md index e53f6b8..a9bdaa6 100644 --- a/README.md +++ b/README.md @@ -4,5 +4,6 @@ Event driven microservice-based c2c ecommerce platform [![](https://github.com/dksifoua/eshop/actions/workflows/eshop.yaml/badge.svg)](https://github.com/dksifoua/eshop/actions/workflows/eshop.yaml) -[![](https://github.com/dksifoua/eshop/actions/workflows/catalog.yaml/badge.svg)](https://github.com/dksifoua/eshop/actions/workflows/catalog.yaml) [![](https://github.com/dksifoua/eshop/actions/workflows/gateway.yaml/badge.svg)](https://github.com/dksifoua/eshop/actions/workflows/gateway.yaml) +[![](https://github.com/dksifoua/eshop/actions/workflows/catalog.yaml/badge.svg)](https://github.com/dksifoua/eshop/actions/workflows/catalog.yaml) +[![](https://github.com/dksifoua/eshop/actions/workflows/eureka.yaml/badge.svg)](https://github.com/dksifoua/eshop/actions/workflows/eureka.yaml) diff --git a/Taskfile.yaml b/Taskfile.yaml index 8084e2e..4031d04 100644 --- a/Taskfile.yaml +++ b/Taskfile.yaml @@ -3,6 +3,8 @@ version: 3 includes: catalog: taskfile: ./catalog-service/Taskfile.yaml + eureka: + taskfile: ./eureka-server/Taskfile.yaml gateway: taskfile: ./api-gateway/Taskfile.yaml @@ -17,7 +19,9 @@ tasks: silent: true test: desc: Test all projects - cmd: ./gradlew test --parallel + cmds: + - task: clean + - ./gradlew test --parallel silent: true gradle:stop: diff --git a/api-gateway/build.gradle b/api-gateway/build.gradle index 0c93ee0..e26d159 100644 --- a/api-gateway/build.gradle +++ b/api-gateway/build.gradle @@ -27,6 +27,7 @@ dependencies { } implementation 'org.springframework.cloud:spring-cloud-starter-gateway' + implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client' developmentOnly 'org.springframework.boot:spring-boot-devtools' testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'io.projectreactor:reactor-test' diff --git a/api-gateway/src/main/resources/application.yaml b/api-gateway/src/main/resources/application.yaml index f187970..056c708 100644 --- a/api-gateway/src/main/resources/application.yaml +++ b/api-gateway/src/main/resources/application.yaml @@ -1,6 +1,11 @@ server: port: 8080 +eureka: + client: + service-url: + defaultZone: ${EUREKA_URI:http://localhost:8761/eureka} + spring: application: name: api-gateway diff --git a/api-gateway/src/test/java/io/dksifoua/eshop/gateway/ApiGatewayApplicationTests.java b/api-gateway/src/test/java/io/dksifoua/eshop/gateway/ApiGatewayApplicationTests.java index 484241a..bc96107 100644 --- a/api-gateway/src/test/java/io/dksifoua/eshop/gateway/ApiGatewayApplicationTests.java +++ b/api-gateway/src/test/java/io/dksifoua/eshop/gateway/ApiGatewayApplicationTests.java @@ -2,12 +2,18 @@ import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.TestPropertySource; @SpringBootTest +@TestPropertySource(properties = { + "eureka.client.enabled=false", + "eureka.client.register-with-eureka=false", + "eureka.client.fetch-registry=false" +}) class ApiGatewayApplicationTests { - @Test - void contextLoads() { - } + @Test + void contextLoads() { + } } diff --git a/catalog-service/build.gradle b/catalog-service/build.gradle index 7899001..7ffee82 100644 --- a/catalog-service/build.gradle +++ b/catalog-service/build.gradle @@ -4,6 +4,10 @@ plugins { id 'io.spring.dependency-management' version '1.1.6' } +ext { + springCloudVersion = "2023.0.3" +} + group = 'io.dksifoua.eshop' version = '0.0.1-SNAPSHOT' @@ -26,6 +30,7 @@ repositories { dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-mongodb' implementation 'org.springframework.boot:spring-boot-starter-webflux' + implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client' compileOnly 'org.projectlombok:lombok' developmentOnly 'org.springframework.boot:spring-boot-devtools' developmentOnly 'org.springframework.boot:spring-boot-docker-compose' @@ -38,6 +43,12 @@ dependencies { testRuntimeOnly 'org.junit.platform:junit-platform-launcher' } +dependencyManagement { + imports { + mavenBom "org.springframework.cloud:spring-cloud-dependencies:$springCloudVersion" + } +} + tasks.named('test') { useJUnitPlatform() } diff --git a/catalog-service/src/main/java/io/dksifoua/eshop/catalog/handler/IndexHandler.java b/catalog-service/src/main/java/io/dksifoua/eshop/catalog/handler/IndexHandler.java index 5bf3cdf..e8d742d 100644 --- a/catalog-service/src/main/java/io/dksifoua/eshop/catalog/handler/IndexHandler.java +++ b/catalog-service/src/main/java/io/dksifoua/eshop/catalog/handler/IndexHandler.java @@ -8,6 +8,7 @@ public class IndexHandler { public Mono index() { - return ServerResponse.ok().body(Mono.just("Welcome to eshop product category service!"), String.class); + String responseData = "Welcome to eshop product category service!"; + return ServerResponse.ok().body(Mono.just(responseData), String.class); } } diff --git a/catalog-service/src/main/resources/application.yaml b/catalog-service/src/main/resources/application.yaml index b4c55ce..cb29fb4 100644 --- a/catalog-service/src/main/resources/application.yaml +++ b/catalog-service/src/main/resources/application.yaml @@ -1,6 +1,11 @@ app: base-path: /api/v1/catalog +eureka: + client: + service-url: + defaultZone: ${EUREKA_URI:http://localhost:8761/eureka} + server: port: 8081 diff --git a/catalog-service/src/test/java/io/dksifoua/eshop/catalog/CatalogServiceApplicationTests.java b/catalog-service/src/test/java/io/dksifoua/eshop/catalog/CatalogServiceApplicationTests.java index 0268ad2..c2afec9 100644 --- a/catalog-service/src/test/java/io/dksifoua/eshop/catalog/CatalogServiceApplicationTests.java +++ b/catalog-service/src/test/java/io/dksifoua/eshop/catalog/CatalogServiceApplicationTests.java @@ -3,13 +3,20 @@ import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.Import; +import org.springframework.test.context.TestPropertySource; @Import(TestcontainersConfiguration.class) @SpringBootTest +@TestPropertySource(properties = { + "eureka.client.enabled=false", + "eureka.client.register-with-eureka=false", + "eureka.client.fetch-registry=false" +}) class CatalogServiceApplicationTests { - @Test - void contextLoads() { - } + @Test + void contextLoads() { + assert true; + } } diff --git a/eureka-server/.gitignore b/eureka-server/.gitignore new file mode 100644 index 0000000..c2065bc --- /dev/null +++ b/eureka-server/.gitignore @@ -0,0 +1,37 @@ +HELP.md +.gradle +build/ +!gradle/wrapper/gradle-wrapper.jar +!**/src/main/**/build/ +!**/src/test/**/build/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ diff --git a/eureka-server/Taskfile.yaml b/eureka-server/Taskfile.yaml new file mode 100644 index 0000000..e609aca --- /dev/null +++ b/eureka-server/Taskfile.yaml @@ -0,0 +1,19 @@ +version: 3 + +tasks: + clean: + desc: Clean eureka server build + cmd: ./gradlew :eureka-server:clean + silent: true + + run: + desc: Run netflix eureka server + cmd: ./gradlew :eureka-server:bootRun + silent: true + + test: + desc: Test netflix eureka server + cmds: + - task: clean + - ./gradlew :eureka-server:test + silent: true \ No newline at end of file diff --git a/eureka-server/build.gradle b/eureka-server/build.gradle new file mode 100644 index 0000000..3c4461c --- /dev/null +++ b/eureka-server/build.gradle @@ -0,0 +1,38 @@ +plugins { + id 'java' + id 'org.springframework.boot' version '3.3.3' + id 'io.spring.dependency-management' version '1.1.6' +} + +group = 'io.dksifoua.eshop' +version = '0.0.1-SNAPSHOT' + +java { + toolchain { + languageVersion = JavaLanguageVersion.of(21) + } +} + +repositories { + mavenCentral() +} + +ext { + set('springCloudVersion', "2023.0.3") +} + +dependencies { + implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server' + testImplementation 'org.springframework.boot:spring-boot-starter-test' + testRuntimeOnly 'org.junit.platform:junit-platform-launcher' +} + +dependencyManagement { + imports { + mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" + } +} + +tasks.named('test') { + useJUnitPlatform() +} diff --git a/eureka-server/settings.gradle b/eureka-server/settings.gradle new file mode 100644 index 0000000..b12c2b7 --- /dev/null +++ b/eureka-server/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'eureka' diff --git a/eureka-server/src/main/java/io/dksifoua/eshop/eureka/EurekaServerApplication.java b/eureka-server/src/main/java/io/dksifoua/eshop/eureka/EurekaServerApplication.java new file mode 100644 index 0000000..1ff1e85 --- /dev/null +++ b/eureka-server/src/main/java/io/dksifoua/eshop/eureka/EurekaServerApplication.java @@ -0,0 +1,15 @@ +package io.dksifoua.eshop.eureka; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; + +@SpringBootApplication +@EnableEurekaServer +public class EurekaServerApplication { + + public static void main(String[] args) { + SpringApplication.run(EurekaServerApplication.class, args); + } + +} diff --git a/eureka-server/src/main/resources/application.yaml b/eureka-server/src/main/resources/application.yaml new file mode 100644 index 0000000..7bd3068 --- /dev/null +++ b/eureka-server/src/main/resources/application.yaml @@ -0,0 +1,11 @@ +eureka: + client: + fetch-registry: false + register-with-eureka: false + +server: + port: 8761 + +spring: + application: + name: eureka-server diff --git a/eureka-server/src/test/java/io/dksifoua/eshop/eureka/EurekaServerApplicationTests.java b/eureka-server/src/test/java/io/dksifoua/eshop/eureka/EurekaServerApplicationTests.java new file mode 100644 index 0000000..76c459b --- /dev/null +++ b/eureka-server/src/test/java/io/dksifoua/eshop/eureka/EurekaServerApplicationTests.java @@ -0,0 +1,14 @@ +package io.dksifoua.eshop.eureka; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class EurekaServerApplicationTests { + + @Test + void contextLoads() { + assert true; + } + +} diff --git a/settings.gradle b/settings.gradle index 6b4881c..834be30 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,4 +1,5 @@ rootProject.name = 'eshop' include('api-gateway') -include('catalog-service') \ No newline at end of file +include('catalog-service') +include('eureka-server') \ No newline at end of file