From 07f47187f6643714bc2a566a4e28941e0a69cbbf Mon Sep 17 00:00:00 2001 From: RouxAntoine Date: Sun, 16 Apr 2023 15:07:40 +0200 Subject: [PATCH] feature: client server implementation with spring cloud config --- Makefile | 9 +++++ build.gradle.kts | 33 +++++++++++++++++++ client/build.gradle.kts | 31 +---------------- .../application/Endpoint.java | 23 +++++++++++++ .../src/main/resources/application.properties | 4 ++- server/build.gradle.kts | 25 ++------------ .../client/development/application.properties | 1 + .../SpringCloudConfigServerApplication.java | 4 +++ .../src/main/resources/application.properties | 10 ++++++ 9 files changed, 86 insertions(+), 54 deletions(-) create mode 100644 build.gradle.kts create mode 100644 client/src/main/java/tk/antoine/roux/springcloudconfig/application/Endpoint.java create mode 100644 server/config-repo/client/development/application.properties create mode 100644 server/src/main/resources/application.properties diff --git a/Makefile b/Makefile index 8b767bb..f831b2f 100644 --- a/Makefile +++ b/Makefile @@ -11,3 +11,12 @@ test: run-client: $(GRADLE_BIN) client:bootRun + +run-server: + $(GRADLE_BIN) server:bootRun + +curl-server: + curl -s -u "user:b3956c50-2e1e-4426-aaca-6b09f7cc4808" localhost:8090/client/development |jq + +curl-client: + curl -s localhost:8080/v1/message diff --git a/build.gradle.kts b/build.gradle.kts new file mode 100644 index 0000000..3e2bbf8 --- /dev/null +++ b/build.gradle.kts @@ -0,0 +1,33 @@ +plugins { + java + id("org.springframework.boot") version "3.0.5" apply false + id("io.spring.dependency-management") version "1.1.0" apply false +} + +extra["springCloudVersion"] = "2022.0.2" + +subprojects { + apply(plugin = "java") + apply(plugin = "org.springframework.boot") + java.sourceCompatibility = JavaVersion.VERSION_17 + + repositories { + mavenCentral() + } + + dependencies { + implementation( + platform("org.springframework.cloud:spring-cloud-dependencies:${property("springCloudVersion")}") + ) + + testImplementation("org.springframework.boot:spring-boot-starter-test") + testImplementation("org.junit.jupiter:junit-jupiter-api:5.9.2") + testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.9.2") + } + + tasks.getByName("test") { + useJUnitPlatform() + } + + apply(plugin = "io.spring.dependency-management") +} diff --git a/client/build.gradle.kts b/client/build.gradle.kts index 96af041..26fc15e 100644 --- a/client/build.gradle.kts +++ b/client/build.gradle.kts @@ -1,39 +1,10 @@ -plugins { - java - id("org.springframework.boot") version "3.0.5" - id("io.spring.dependency-management") version "1.1.0" -} - group = "tk.antoine.roux" version = "0.0.1-SNAPSHOT" -java.sourceCompatibility = JavaVersion.VERSION_17 - -repositories { - mavenCentral() -} - -extra["springCloudVersion"] = "2022.0.2" dependencies { + implementation("org.springframework.cloud:spring-cloud-starter-config") implementation("org.springframework.boot:spring-boot-starter-actuator") implementation("org.springframework.boot:spring-boot-starter-web") - implementation("org.springframework.cloud:spring-cloud-starter-config") developmentOnly("org.springframework.boot:spring-boot-devtools") - - testImplementation("org.springframework.boot:spring-boot-starter-test") - testImplementation("org.junit.jupiter:junit-jupiter-api:5.9.2") - testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.9.2") -} - -dependencyManagement { - imports { - mavenBom( - "org.springframework.cloud:spring-cloud-dependencies:${property("springCloudVersion")}" - ) - } -} - -tasks.getByName("test") { - useJUnitPlatform() } diff --git a/client/src/main/java/tk/antoine/roux/springcloudconfig/application/Endpoint.java b/client/src/main/java/tk/antoine/roux/springcloudconfig/application/Endpoint.java new file mode 100644 index 0000000..6d70176 --- /dev/null +++ b/client/src/main/java/tk/antoine/roux/springcloudconfig/application/Endpoint.java @@ -0,0 +1,23 @@ +package tk.antoine.roux.springcloudconfig.application; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/v1") +public class Endpoint { + + private final String message; + + public Endpoint(@Value("${application.message}") String message) { + this.message = message; + } + + @GetMapping("/message") + public String getMessage() { + return message; + } + +} diff --git a/client/src/main/resources/application.properties b/client/src/main/resources/application.properties index 8b13789..90ae793 100644 --- a/client/src/main/resources/application.properties +++ b/client/src/main/resources/application.properties @@ -1 +1,3 @@ - +spring.application.name=client +spring.profiles.active=development +spring.config.import=configserver:http://user:b3956c50-2e1e-4426-aaca-6b09f7cc4808@localhost:8090 diff --git a/server/build.gradle.kts b/server/build.gradle.kts index e67b28c..c905b98 100644 --- a/server/build.gradle.kts +++ b/server/build.gradle.kts @@ -1,29 +1,8 @@ -plugins { - java - id("org.springframework.boot") version "3.0.5" - id("io.spring.dependency-management") version "1.1.0" -} - group = "tk.antoine.roux" version = "0.0.1-SNAPSHOT" -java.sourceCompatibility = JavaVersion.VERSION_17 - -repositories { - mavenCentral() -} - -extra["springCloudVersion"] = "2022.0.2" - dependencies { implementation("org.springframework.cloud:spring-cloud-config-server") - implementation("org.springframework.cloud:spring-cloud-starter-config") - - testImplementation("org.springframework.boot:spring-boot-starter-test") - testImplementation("org.junit.jupiter:junit-jupiter-api:5.9.2") - testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.9.2") -} - -tasks.getByName("test") { - useJUnitPlatform() + implementation("org.springframework.boot:spring-boot-starter-web") + implementation("org.springframework.boot:spring-boot-starter-security") } diff --git a/server/config-repo/client/development/application.properties b/server/config-repo/client/development/application.properties new file mode 100644 index 0000000..4059b82 --- /dev/null +++ b/server/config-repo/client/development/application.properties @@ -0,0 +1 @@ +application.message=Hello world diff --git a/server/src/main/java/tk/antoine/roux/springcloudconfig/SpringCloudConfigServerApplication.java b/server/src/main/java/tk/antoine/roux/springcloudconfig/SpringCloudConfigServerApplication.java index 4ee00b8..512fbb7 100644 --- a/server/src/main/java/tk/antoine/roux/springcloudconfig/SpringCloudConfigServerApplication.java +++ b/server/src/main/java/tk/antoine/roux/springcloudconfig/SpringCloudConfigServerApplication.java @@ -1,5 +1,9 @@ package tk.antoine.roux.springcloudconfig; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.config.server.EnableConfigServer; + @EnableConfigServer @SpringBootApplication public class SpringCloudConfigServerApplication { diff --git a/server/src/main/resources/application.properties b/server/src/main/resources/application.properties new file mode 100644 index 0000000..842d2da --- /dev/null +++ b/server/src/main/resources/application.properties @@ -0,0 +1,10 @@ +server.port=8090 +spring.profiles.active=native +spring.cloud.config.server.native.search-locations=file:./config-repo/{application}/{profile} + +spring.security.user.name=user +spring.security.user.password=b3956c50-2e1e-4426-aaca-6b09f7cc4808 + +#spring.cloud.config.server.git.uri=file://${user.home}/config-repo +#spring.cloud.config.server.git.search-paths='{application}/{profile}' +#spring.cloud.config.server.git.refresh-rate=10