From ffdaecda7f5604a9693e79a10cfc7dc21b3e84b2 Mon Sep 17 00:00:00 2001 From: Andreas Fritz Date: Fri, 14 Mar 2025 11:05:18 +0100 Subject: [PATCH 1/3] added Cache and Database Example --- cache-and-db-metrics/README.md | 26 +++++++ .../apps/hello-cache/.gitignore | 5 ++ .../apps/hello-cache/Dockerfile | 8 ++ .../apps/hello-cache/README.md | 14 ++++ .../apps/hello-cache/build.sh | 6 ++ cache-and-db-metrics/apps/hello-cache/pom.xml | 73 +++++++++++++++++++ .../java/de/codecentric/hellocache/Cache.java | 18 +++++ .../de/codecentric/hellocache/HelloCache.java | 21 ++++++ .../hellocache/HelloCacheApplication.java | 22 ++++++ .../src/main/resources/application.properties | 28 +++++++ .../apps/spring-boot-admin/.gitignore | 5 ++ .../apps/spring-boot-admin/Dockerfile | 8 ++ .../apps/spring-boot-admin/build.sh | 6 ++ .../apps/spring-boot-admin/pom.xml | 67 +++++++++++++++++ .../SpringBootAdminApplication.java | 15 ++++ .../src/main/resources/application.yml | 18 +++++ cache-and-db-metrics/buildAndRunAll.sh | 11 +++ cache-and-db-metrics/docker-compose.yml | 19 +++++ 18 files changed, 370 insertions(+) create mode 100644 cache-and-db-metrics/README.md create mode 100644 cache-and-db-metrics/apps/hello-cache/.gitignore create mode 100644 cache-and-db-metrics/apps/hello-cache/Dockerfile create mode 100644 cache-and-db-metrics/apps/hello-cache/README.md create mode 100755 cache-and-db-metrics/apps/hello-cache/build.sh create mode 100644 cache-and-db-metrics/apps/hello-cache/pom.xml create mode 100644 cache-and-db-metrics/apps/hello-cache/src/main/java/de/codecentric/hellocache/Cache.java create mode 100644 cache-and-db-metrics/apps/hello-cache/src/main/java/de/codecentric/hellocache/HelloCache.java create mode 100644 cache-and-db-metrics/apps/hello-cache/src/main/java/de/codecentric/hellocache/HelloCacheApplication.java create mode 100644 cache-and-db-metrics/apps/hello-cache/src/main/resources/application.properties create mode 100644 cache-and-db-metrics/apps/spring-boot-admin/.gitignore create mode 100644 cache-and-db-metrics/apps/spring-boot-admin/Dockerfile create mode 100755 cache-and-db-metrics/apps/spring-boot-admin/build.sh create mode 100644 cache-and-db-metrics/apps/spring-boot-admin/pom.xml create mode 100644 cache-and-db-metrics/apps/spring-boot-admin/src/main/java/de/codecentric/springbootadmin/SpringBootAdminApplication.java create mode 100644 cache-and-db-metrics/apps/spring-boot-admin/src/main/resources/application.yml create mode 100755 cache-and-db-metrics/buildAndRunAll.sh create mode 100644 cache-and-db-metrics/docker-compose.yml diff --git a/cache-and-db-metrics/README.md b/cache-and-db-metrics/README.md new file mode 100644 index 0000000..77f19f9 --- /dev/null +++ b/cache-and-db-metrics/README.md @@ -0,0 +1,26 @@ +## Spring Boot Admin - Eureka Example + +Allows to run Spring Boot Admin and two sample apps (hello world, health simulator) with service discovery via eureka. + +This Readme will guide you through all setup steps for the infrastructure. + +## Prerequisites + +- Java +- Maven +- Docker and Docker Compose + +## Run Everything +You can run the whole build and start all apps in docker containers with the following script or follow the step-by-step guide below. +```bash +chmod u+x buildAndRunAll.sh +./buildAndRunAll.sh +``` +Cache Controller: http://localhost:8180/ + +Admin UI: http://localhost:8080/ + +## Stop Everything +```bash +docker-compose down -v +``` diff --git a/cache-and-db-metrics/apps/hello-cache/.gitignore b/cache-and-db-metrics/apps/hello-cache/.gitignore new file mode 100644 index 0000000..b18abdf --- /dev/null +++ b/cache-and-db-metrics/apps/hello-cache/.gitignore @@ -0,0 +1,5 @@ +target +.idea +*.iml +*.log +*.gz diff --git a/cache-and-db-metrics/apps/hello-cache/Dockerfile b/cache-and-db-metrics/apps/hello-cache/Dockerfile new file mode 100644 index 0000000..0f5052e --- /dev/null +++ b/cache-and-db-metrics/apps/hello-cache/Dockerfile @@ -0,0 +1,8 @@ +# https://hub.docker.com/_/eclipse-temurin/ +FROM eclipse-temurin:17 + +VOLUME /tmp + +COPY target/app.jar /opt/app/app.jar + +CMD ["bash", "-c", "java $JAVA_OPTS -jar /opt/app/app.jar"] diff --git a/cache-and-db-metrics/apps/hello-cache/README.md b/cache-and-db-metrics/apps/hello-cache/README.md new file mode 100644 index 0000000..ea3c60c --- /dev/null +++ b/cache-and-db-metrics/apps/hello-cache/README.md @@ -0,0 +1,14 @@ +# README + +## Start + + mvn spring-boot:run + +## URL + +- http://localhost:9090/ +- http://localhost:9091/actuator +- http://localhost:9091/actuator/metrics +- http://localhost:9091/actuator/metrics/cache.gets +- http://localhost:9091/actuator/health/owngroup +- http://localhost:9091/actuator/caches diff --git a/cache-and-db-metrics/apps/hello-cache/build.sh b/cache-and-db-metrics/apps/hello-cache/build.sh new file mode 100755 index 0000000..21a98c8 --- /dev/null +++ b/cache-and-db-metrics/apps/hello-cache/build.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +# Build App +mvn package +# Build Docker Image +docker build --tag hello-cache . diff --git a/cache-and-db-metrics/apps/hello-cache/pom.xml b/cache-and-db-metrics/apps/hello-cache/pom.xml new file mode 100644 index 0000000..ff6eda8 --- /dev/null +++ b/cache-and-db-metrics/apps/hello-cache/pom.xml @@ -0,0 +1,73 @@ + + + 4.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 3.4.0 + + + de.codecentric + hello-cache + jar + ${project.artifactId} + 3.4.0-SNAPSHOT + Hello Cache + + + 3.4.5 + 17 + UTF-8 + + + + + org.springframework.boot + spring-boot-starter-web + + + + de.codecentric + spring-boot-admin-starter-client + ${spring-boot-admin.version} + + + + org.springframework.boot + spring-boot-starter-cache + + + org.springframework.boot + spring-boot-starter-data-redis + + + + org.projectlombok + lombok + + + + + app + + + org.springframework.boot + spring-boot-maven-plugin + + + repackage + + repackage + + build-info + + + + + + + + diff --git a/cache-and-db-metrics/apps/hello-cache/src/main/java/de/codecentric/hellocache/Cache.java b/cache-and-db-metrics/apps/hello-cache/src/main/java/de/codecentric/hellocache/Cache.java new file mode 100644 index 0000000..22fba33 --- /dev/null +++ b/cache-and-db-metrics/apps/hello-cache/src/main/java/de/codecentric/hellocache/Cache.java @@ -0,0 +1,18 @@ +package de.codecentric.hellocache; + +import java.util.UUID; +import lombok.extern.slf4j.Slf4j; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.stereotype.Component; + +@Component +@Slf4j +public class Cache { + + @Cacheable("uuid") + public UUID getUuid() { + var uuid = UUID.randomUUID(); + log.info("New UUID: {}", uuid); + return uuid; + } +} diff --git a/cache-and-db-metrics/apps/hello-cache/src/main/java/de/codecentric/hellocache/HelloCache.java b/cache-and-db-metrics/apps/hello-cache/src/main/java/de/codecentric/hellocache/HelloCache.java new file mode 100644 index 0000000..4b6411d --- /dev/null +++ b/cache-and-db-metrics/apps/hello-cache/src/main/java/de/codecentric/hellocache/HelloCache.java @@ -0,0 +1,21 @@ +package de.codecentric.hellocache; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequiredArgsConstructor +@Slf4j +public class HelloCache { + + private final Cache cache; + + @GetMapping + public String hello() { + var uuid = cache.getUuid(); + log.info("Hello {}", uuid); + return uuid.toString(); + } +} diff --git a/cache-and-db-metrics/apps/hello-cache/src/main/java/de/codecentric/hellocache/HelloCacheApplication.java b/cache-and-db-metrics/apps/hello-cache/src/main/java/de/codecentric/hellocache/HelloCacheApplication.java new file mode 100644 index 0000000..7d889b3 --- /dev/null +++ b/cache-and-db-metrics/apps/hello-cache/src/main/java/de/codecentric/hellocache/HelloCacheApplication.java @@ -0,0 +1,22 @@ +package de.codecentric.hellocache; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.actuate.audit.AuditEventRepository; +import org.springframework.boot.actuate.audit.InMemoryAuditEventRepository; +import org.springframework.boot.actuate.web.exchanges.HttpExchangeRepository; +import org.springframework.boot.actuate.web.exchanges.InMemoryHttpExchangeRepository; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.metrics.buffering.BufferingApplicationStartup; +import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.context.annotation.Bean; + +@SpringBootApplication +@EnableCaching +public class HelloCacheApplication extends SpringBootServletInitializer { + + public static void main(String... args) { + SpringApplication.run(HelloCacheApplication.class); + } + +} diff --git a/cache-and-db-metrics/apps/hello-cache/src/main/resources/application.properties b/cache-and-db-metrics/apps/hello-cache/src/main/resources/application.properties new file mode 100644 index 0000000..cec6135 --- /dev/null +++ b/cache-and-db-metrics/apps/hello-cache/src/main/resources/application.properties @@ -0,0 +1,28 @@ +# +# Server Port +# +server.port=8080 +# +# Actuator Configuration +# +management.endpoints.web.exposure.include=* +# +# Health-Actuator +# +management.endpoint.health.show-details=always +management.endpoint.health.probes.enabled=true +# +# Self-Registration @Spring Boot Admin +# use docker-compose internal service name to access sba server +# +spring.boot.admin.client.url=http://spring-boot-admin:8080 +# +# Cache +# +spring.cache.cache-names=uuid +spring.data.redis.host=redis +spring.data.redis.port=6379 +spring.cache.redis.time-to-live=1m +spring.cache.redis.enable-statistics=true +spring.data.redis.connect-timeout=1000 +spring.data.redis.timeout=1000 diff --git a/cache-and-db-metrics/apps/spring-boot-admin/.gitignore b/cache-and-db-metrics/apps/spring-boot-admin/.gitignore new file mode 100644 index 0000000..b18abdf --- /dev/null +++ b/cache-and-db-metrics/apps/spring-boot-admin/.gitignore @@ -0,0 +1,5 @@ +target +.idea +*.iml +*.log +*.gz diff --git a/cache-and-db-metrics/apps/spring-boot-admin/Dockerfile b/cache-and-db-metrics/apps/spring-boot-admin/Dockerfile new file mode 100644 index 0000000..0f5052e --- /dev/null +++ b/cache-and-db-metrics/apps/spring-boot-admin/Dockerfile @@ -0,0 +1,8 @@ +# https://hub.docker.com/_/eclipse-temurin/ +FROM eclipse-temurin:17 + +VOLUME /tmp + +COPY target/app.jar /opt/app/app.jar + +CMD ["bash", "-c", "java $JAVA_OPTS -jar /opt/app/app.jar"] diff --git a/cache-and-db-metrics/apps/spring-boot-admin/build.sh b/cache-and-db-metrics/apps/spring-boot-admin/build.sh new file mode 100755 index 0000000..270be60 --- /dev/null +++ b/cache-and-db-metrics/apps/spring-boot-admin/build.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +# Build App +mvn package +# Build Docker Image +docker build --tag spring-boot-admin-metrics . diff --git a/cache-and-db-metrics/apps/spring-boot-admin/pom.xml b/cache-and-db-metrics/apps/spring-boot-admin/pom.xml new file mode 100644 index 0000000..993f496 --- /dev/null +++ b/cache-and-db-metrics/apps/spring-boot-admin/pom.xml @@ -0,0 +1,67 @@ + + + 4.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 3.4.0 + + + de.codecentric + spring-boot-admin-metrics + jar + ${project.artifactId} + 1.0.0-SNAPSHOT + Spring Boot Admin + + + 17 + UTF-8 + 3.4.5 + 2024.0.0 + + + + + org.springframework.boot + spring-boot-starter-web + + + + + de.codecentric + spring-boot-admin-starter-server + ${spring-boot-admin.version} + + + + de.codecentric + spring-boot-admin-starter-client + ${spring-boot-admin.version} + + + + + app + + + org.springframework.boot + spring-boot-maven-plugin + + + repackage + + repackage + + build-info + + + + + + + + diff --git a/cache-and-db-metrics/apps/spring-boot-admin/src/main/java/de/codecentric/springbootadmin/SpringBootAdminApplication.java b/cache-and-db-metrics/apps/spring-boot-admin/src/main/java/de/codecentric/springbootadmin/SpringBootAdminApplication.java new file mode 100644 index 0000000..0133fa7 --- /dev/null +++ b/cache-and-db-metrics/apps/spring-boot-admin/src/main/java/de/codecentric/springbootadmin/SpringBootAdminApplication.java @@ -0,0 +1,15 @@ +package de.codecentric.springbootadmin; + +import de.codecentric.boot.admin.server.config.EnableAdminServer; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.metrics.buffering.BufferingApplicationStartup; + +@SpringBootApplication +@EnableAdminServer +public class SpringBootAdminApplication { + + public static void main(String... args) { + SpringApplication.run(SpringBootAdminApplication.class); + } +} diff --git a/cache-and-db-metrics/apps/spring-boot-admin/src/main/resources/application.yml b/cache-and-db-metrics/apps/spring-boot-admin/src/main/resources/application.yml new file mode 100644 index 0000000..bb643f1 --- /dev/null +++ b/cache-and-db-metrics/apps/spring-boot-admin/src/main/resources/application.yml @@ -0,0 +1,18 @@ +server: + port: 8080 +spring: + application: # Application-Info for the Info-Actuator + name: "@pom.artifactId@" + boot: + admin: + client: + # Self-Registration @itself + url: "http://localhost:8080/" +management: # Actuator Configuration + endpoints: + web: + exposure: + include: "*" + endpoint: # Health-Actuator + health: + show-details: always diff --git a/cache-and-db-metrics/buildAndRunAll.sh b/cache-and-db-metrics/buildAndRunAll.sh new file mode 100755 index 0000000..8d36ae4 --- /dev/null +++ b/cache-and-db-metrics/buildAndRunAll.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +cd apps/spring-boot-admin +./build.sh +cd ../.. + +cd apps/hello-cache +./build.sh +cd ../.. + +docker-compose up -d diff --git a/cache-and-db-metrics/docker-compose.yml b/cache-and-db-metrics/docker-compose.yml new file mode 100644 index 0000000..c060261 --- /dev/null +++ b/cache-and-db-metrics/docker-compose.yml @@ -0,0 +1,19 @@ +services: + + redis: + image: redis:alpine + restart: always + ports: + - "6379:6379" + + hello-cache: + container_name: hello-cache + image: hello-cache + ports: + - "8180:8080" + + spring-boot-admin: + container_name: spring-boot-admin-metrics + image: spring-boot-admin-metrics + ports: + - "8080:8080" From 4fdcc8160f82472fe73c6499a9ef37a15a8665f5 Mon Sep 17 00:00:00 2001 From: Andreas Fritz Date: Fri, 14 Mar 2025 11:23:27 +0100 Subject: [PATCH 2/3] added database --- .../apps/hello-cache/README.md | 14 ------------ .../hellocache/HelloCacheApplication.java | 22 ------------------- .../{hello-cache => hello-data}/.gitignore | 0 .../{hello-cache => hello-data}/Dockerfile | 0 .../apps/hello-data/README.md | 9 ++++++++ .../apps/{hello-cache => hello-data}/build.sh | 2 +- .../apps/{hello-cache => hello-data}/pom.xml | 14 ++++++++++-- .../java/de/codecentric/hellodata}/Cache.java | 2 +- .../de/codecentric/hellodata}/HelloCache.java | 2 +- .../hellodata/HelloDataApplication.java | 16 ++++++++++++++ .../src/main/resources/application.properties | 1 + cache-and-db-metrics/buildAndRunAll.sh | 2 +- cache-and-db-metrics/docker-compose.yml | 6 ++--- 13 files changed, 45 insertions(+), 45 deletions(-) delete mode 100644 cache-and-db-metrics/apps/hello-cache/README.md delete mode 100644 cache-and-db-metrics/apps/hello-cache/src/main/java/de/codecentric/hellocache/HelloCacheApplication.java rename cache-and-db-metrics/apps/{hello-cache => hello-data}/.gitignore (100%) rename cache-and-db-metrics/apps/{hello-cache => hello-data}/Dockerfile (100%) create mode 100644 cache-and-db-metrics/apps/hello-data/README.md rename cache-and-db-metrics/apps/{hello-cache => hello-data}/build.sh (62%) rename cache-and-db-metrics/apps/{hello-cache => hello-data}/pom.xml (85%) rename cache-and-db-metrics/apps/{hello-cache/src/main/java/de/codecentric/hellocache => hello-data/src/main/java/de/codecentric/hellodata}/Cache.java (90%) rename cache-and-db-metrics/apps/{hello-cache/src/main/java/de/codecentric/hellocache => hello-data/src/main/java/de/codecentric/hellodata}/HelloCache.java (92%) create mode 100644 cache-and-db-metrics/apps/hello-data/src/main/java/de/codecentric/hellodata/HelloDataApplication.java rename cache-and-db-metrics/apps/{hello-cache => hello-data}/src/main/resources/application.properties (94%) diff --git a/cache-and-db-metrics/apps/hello-cache/README.md b/cache-and-db-metrics/apps/hello-cache/README.md deleted file mode 100644 index ea3c60c..0000000 --- a/cache-and-db-metrics/apps/hello-cache/README.md +++ /dev/null @@ -1,14 +0,0 @@ -# README - -## Start - - mvn spring-boot:run - -## URL - -- http://localhost:9090/ -- http://localhost:9091/actuator -- http://localhost:9091/actuator/metrics -- http://localhost:9091/actuator/metrics/cache.gets -- http://localhost:9091/actuator/health/owngroup -- http://localhost:9091/actuator/caches diff --git a/cache-and-db-metrics/apps/hello-cache/src/main/java/de/codecentric/hellocache/HelloCacheApplication.java b/cache-and-db-metrics/apps/hello-cache/src/main/java/de/codecentric/hellocache/HelloCacheApplication.java deleted file mode 100644 index 7d889b3..0000000 --- a/cache-and-db-metrics/apps/hello-cache/src/main/java/de/codecentric/hellocache/HelloCacheApplication.java +++ /dev/null @@ -1,22 +0,0 @@ -package de.codecentric.hellocache; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.actuate.audit.AuditEventRepository; -import org.springframework.boot.actuate.audit.InMemoryAuditEventRepository; -import org.springframework.boot.actuate.web.exchanges.HttpExchangeRepository; -import org.springframework.boot.actuate.web.exchanges.InMemoryHttpExchangeRepository; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.context.metrics.buffering.BufferingApplicationStartup; -import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; -import org.springframework.cache.annotation.EnableCaching; -import org.springframework.context.annotation.Bean; - -@SpringBootApplication -@EnableCaching -public class HelloCacheApplication extends SpringBootServletInitializer { - - public static void main(String... args) { - SpringApplication.run(HelloCacheApplication.class); - } - -} diff --git a/cache-and-db-metrics/apps/hello-cache/.gitignore b/cache-and-db-metrics/apps/hello-data/.gitignore similarity index 100% rename from cache-and-db-metrics/apps/hello-cache/.gitignore rename to cache-and-db-metrics/apps/hello-data/.gitignore diff --git a/cache-and-db-metrics/apps/hello-cache/Dockerfile b/cache-and-db-metrics/apps/hello-data/Dockerfile similarity index 100% rename from cache-and-db-metrics/apps/hello-cache/Dockerfile rename to cache-and-db-metrics/apps/hello-data/Dockerfile diff --git a/cache-and-db-metrics/apps/hello-data/README.md b/cache-and-db-metrics/apps/hello-data/README.md new file mode 100644 index 0000000..5d3b547 --- /dev/null +++ b/cache-and-db-metrics/apps/hello-data/README.md @@ -0,0 +1,9 @@ +# README + +## URL + +- http://localhost:8180/ +- http://localhost:8180/actuator +- http://localhost:8180/actuator/metrics +- http://localhost:8180/actuator/metrics/cache.gets +- http://localhost:8180/actuator/caches diff --git a/cache-and-db-metrics/apps/hello-cache/build.sh b/cache-and-db-metrics/apps/hello-data/build.sh similarity index 62% rename from cache-and-db-metrics/apps/hello-cache/build.sh rename to cache-and-db-metrics/apps/hello-data/build.sh index 21a98c8..5559391 100755 --- a/cache-and-db-metrics/apps/hello-cache/build.sh +++ b/cache-and-db-metrics/apps/hello-data/build.sh @@ -3,4 +3,4 @@ # Build App mvn package # Build Docker Image -docker build --tag hello-cache . +docker build --tag hello-data . diff --git a/cache-and-db-metrics/apps/hello-cache/pom.xml b/cache-and-db-metrics/apps/hello-data/pom.xml similarity index 85% rename from cache-and-db-metrics/apps/hello-cache/pom.xml rename to cache-and-db-metrics/apps/hello-data/pom.xml index ff6eda8..97d72a8 100644 --- a/cache-and-db-metrics/apps/hello-cache/pom.xml +++ b/cache-and-db-metrics/apps/hello-data/pom.xml @@ -11,11 +11,11 @@ de.codecentric - hello-cache + hello-data jar ${project.artifactId} 3.4.0-SNAPSHOT - Hello Cache + Hello Data 3.4.5 @@ -43,6 +43,16 @@ org.springframework.boot spring-boot-starter-data-redis + + + org.springframework.boot + spring-boot-starter-data-jpa + + + com.h2database + h2 + + org.projectlombok diff --git a/cache-and-db-metrics/apps/hello-cache/src/main/java/de/codecentric/hellocache/Cache.java b/cache-and-db-metrics/apps/hello-data/src/main/java/de/codecentric/hellodata/Cache.java similarity index 90% rename from cache-and-db-metrics/apps/hello-cache/src/main/java/de/codecentric/hellocache/Cache.java rename to cache-and-db-metrics/apps/hello-data/src/main/java/de/codecentric/hellodata/Cache.java index 22fba33..949d45b 100644 --- a/cache-and-db-metrics/apps/hello-cache/src/main/java/de/codecentric/hellocache/Cache.java +++ b/cache-and-db-metrics/apps/hello-data/src/main/java/de/codecentric/hellodata/Cache.java @@ -1,4 +1,4 @@ -package de.codecentric.hellocache; +package de.codecentric.hellodata; import java.util.UUID; import lombok.extern.slf4j.Slf4j; diff --git a/cache-and-db-metrics/apps/hello-cache/src/main/java/de/codecentric/hellocache/HelloCache.java b/cache-and-db-metrics/apps/hello-data/src/main/java/de/codecentric/hellodata/HelloCache.java similarity index 92% rename from cache-and-db-metrics/apps/hello-cache/src/main/java/de/codecentric/hellocache/HelloCache.java rename to cache-and-db-metrics/apps/hello-data/src/main/java/de/codecentric/hellodata/HelloCache.java index 4b6411d..5d49f93 100644 --- a/cache-and-db-metrics/apps/hello-cache/src/main/java/de/codecentric/hellocache/HelloCache.java +++ b/cache-and-db-metrics/apps/hello-data/src/main/java/de/codecentric/hellodata/HelloCache.java @@ -1,4 +1,4 @@ -package de.codecentric.hellocache; +package de.codecentric.hellodata; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; diff --git a/cache-and-db-metrics/apps/hello-data/src/main/java/de/codecentric/hellodata/HelloDataApplication.java b/cache-and-db-metrics/apps/hello-data/src/main/java/de/codecentric/hellodata/HelloDataApplication.java new file mode 100644 index 0000000..54db502 --- /dev/null +++ b/cache-and-db-metrics/apps/hello-data/src/main/java/de/codecentric/hellodata/HelloDataApplication.java @@ -0,0 +1,16 @@ +package de.codecentric.hellodata; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; +import org.springframework.cache.annotation.EnableCaching; + +@SpringBootApplication +@EnableCaching +public class HelloDataApplication extends SpringBootServletInitializer { + + public static void main(String... args) { + SpringApplication.run(HelloDataApplication.class); + } + +} diff --git a/cache-and-db-metrics/apps/hello-cache/src/main/resources/application.properties b/cache-and-db-metrics/apps/hello-data/src/main/resources/application.properties similarity index 94% rename from cache-and-db-metrics/apps/hello-cache/src/main/resources/application.properties rename to cache-and-db-metrics/apps/hello-data/src/main/resources/application.properties index cec6135..fec8a44 100644 --- a/cache-and-db-metrics/apps/hello-cache/src/main/resources/application.properties +++ b/cache-and-db-metrics/apps/hello-data/src/main/resources/application.properties @@ -16,6 +16,7 @@ management.endpoint.health.probes.enabled=true # use docker-compose internal service name to access sba server # spring.boot.admin.client.url=http://spring-boot-admin:8080 +spring.application.name=@pom.name@ # # Cache # diff --git a/cache-and-db-metrics/buildAndRunAll.sh b/cache-and-db-metrics/buildAndRunAll.sh index 8d36ae4..948af1b 100755 --- a/cache-and-db-metrics/buildAndRunAll.sh +++ b/cache-and-db-metrics/buildAndRunAll.sh @@ -4,7 +4,7 @@ cd apps/spring-boot-admin ./build.sh cd ../.. -cd apps/hello-cache +cd apps/hello-data ./build.sh cd ../.. diff --git a/cache-and-db-metrics/docker-compose.yml b/cache-and-db-metrics/docker-compose.yml index c060261..cbc9e88 100644 --- a/cache-and-db-metrics/docker-compose.yml +++ b/cache-and-db-metrics/docker-compose.yml @@ -6,9 +6,9 @@ services: ports: - "6379:6379" - hello-cache: - container_name: hello-cache - image: hello-cache + hello-data: + container_name: hello-data + image: hello-data ports: - "8180:8080" From fa16bdf92db2e450f1d3fe18049be5cc76d8127c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20Ko=CC=88ninger?= Date: Fri, 14 Mar 2025 12:02:30 +0100 Subject: [PATCH 3/3] chore: rename HelloCache to HelloData and add database interaction; update README and scripts for new functionality --- README.md | 3 ++ cache-and-db-metrics/README.md | 12 +++--- cache-and-db-metrics/apps/hello-data/pom.xml | 2 +- .../de/codecentric/hellodata/HelloCache.java | 21 ---------- .../de/codecentric/hellodata/HelloData.java | 38 +++++++++++++++++++ .../apps/spring-boot-admin/pom.xml | 8 +--- .../src/main/resources/application.yml | 18 --------- cache-and-db-metrics/buildAndRunAll.sh | 2 +- 8 files changed, 51 insertions(+), 53 deletions(-) delete mode 100644 cache-and-db-metrics/apps/hello-data/src/main/java/de/codecentric/hellodata/HelloCache.java create mode 100644 cache-and-db-metrics/apps/hello-data/src/main/java/de/codecentric/hellodata/HelloData.java delete mode 100644 cache-and-db-metrics/apps/spring-boot-admin/src/main/resources/application.yml diff --git a/README.md b/README.md index b8e1867..bf66c08 100644 --- a/README.md +++ b/README.md @@ -13,3 +13,6 @@ Not intended for production usage, but to reproduce issues or to try out things. ## Proxy - [nginx](nginx) + +## Metrics +- [Cache and DB metrics](cache-and-db-metrics) \ No newline at end of file diff --git a/cache-and-db-metrics/README.md b/cache-and-db-metrics/README.md index 77f19f9..1004567 100644 --- a/cache-and-db-metrics/README.md +++ b/cache-and-db-metrics/README.md @@ -1,6 +1,7 @@ -## Spring Boot Admin - Eureka Example +## Spring Boot Admin - Cache and DB metrics Example -Allows to run Spring Boot Admin and two sample apps (hello world, health simulator) with service discovery via eureka. +Allows to run Spring Boot Admin and one sample app (hello-data) which is connected to redis and h2 database and provides +metrics for these data sources. This Readme will guide you through all setup steps for the infrastructure. @@ -11,16 +12,17 @@ This Readme will guide you through all setup steps for the infrastructure. - Docker and Docker Compose ## Run Everything -You can run the whole build and start all apps in docker containers with the following script or follow the step-by-step guide below. +You can run the whole build and start all apps in docker containers with the following script. ```bash chmod u+x buildAndRunAll.sh ./buildAndRunAll.sh ``` -Cache Controller: http://localhost:8180/ +Trigger Cache: http://localhost:8180/trigger-cache +Trigger DB: http://localhost:8180/trigger-db Admin UI: http://localhost:8080/ ## Stop Everything ```bash -docker-compose down -v +docker compose down -v ``` diff --git a/cache-and-db-metrics/apps/hello-data/pom.xml b/cache-and-db-metrics/apps/hello-data/pom.xml index 97d72a8..be54a42 100644 --- a/cache-and-db-metrics/apps/hello-data/pom.xml +++ b/cache-and-db-metrics/apps/hello-data/pom.xml @@ -14,7 +14,7 @@ hello-data jar ${project.artifactId} - 3.4.0-SNAPSHOT + 1.0.0-SNAPSHOT Hello Data diff --git a/cache-and-db-metrics/apps/hello-data/src/main/java/de/codecentric/hellodata/HelloCache.java b/cache-and-db-metrics/apps/hello-data/src/main/java/de/codecentric/hellodata/HelloCache.java deleted file mode 100644 index 5d49f93..0000000 --- a/cache-and-db-metrics/apps/hello-data/src/main/java/de/codecentric/hellodata/HelloCache.java +++ /dev/null @@ -1,21 +0,0 @@ -package de.codecentric.hellodata; - -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RestController; - -@RestController -@RequiredArgsConstructor -@Slf4j -public class HelloCache { - - private final Cache cache; - - @GetMapping - public String hello() { - var uuid = cache.getUuid(); - log.info("Hello {}", uuid); - return uuid.toString(); - } -} diff --git a/cache-and-db-metrics/apps/hello-data/src/main/java/de/codecentric/hellodata/HelloData.java b/cache-and-db-metrics/apps/hello-data/src/main/java/de/codecentric/hellodata/HelloData.java new file mode 100644 index 0000000..ffe6fd5 --- /dev/null +++ b/cache-and-db-metrics/apps/hello-data/src/main/java/de/codecentric/hellodata/HelloData.java @@ -0,0 +1,38 @@ +package de.codecentric.hellodata; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequiredArgsConstructor +@Slf4j +public class HelloData { + + private final Cache cache; + private final JdbcTemplate jdbcTemplate; + private final ExecutorService executorService = Executors.newFixedThreadPool(15); + + @GetMapping("/trigger-cache") + public String triggerCache() { + var uuid = cache.getUuid(); + log.info("Hello {}", uuid); + return uuid.toString(); + } + + @GetMapping("/trigger-db") + public void triggerDb() { + for (int i = 0; i < 10_000; i++) { + final int finalI = i; + executorService.submit(() -> { + jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS test" + finalI + " (id INT PRIMARY KEY)"); + jdbcTemplate.execute("INSERT INTO test" + finalI + " VALUES (" + finalI + ")"); + }); + } + } +} diff --git a/cache-and-db-metrics/apps/spring-boot-admin/pom.xml b/cache-and-db-metrics/apps/spring-boot-admin/pom.xml index 993f496..c04caf1 100644 --- a/cache-and-db-metrics/apps/spring-boot-admin/pom.xml +++ b/cache-and-db-metrics/apps/spring-boot-admin/pom.xml @@ -11,7 +11,7 @@ de.codecentric - spring-boot-admin-metrics + spring-boot-admin jar ${project.artifactId} 1.0.0-SNAPSHOT @@ -36,12 +36,6 @@ spring-boot-admin-starter-server ${spring-boot-admin.version} - - - de.codecentric - spring-boot-admin-starter-client - ${spring-boot-admin.version} - diff --git a/cache-and-db-metrics/apps/spring-boot-admin/src/main/resources/application.yml b/cache-and-db-metrics/apps/spring-boot-admin/src/main/resources/application.yml deleted file mode 100644 index bb643f1..0000000 --- a/cache-and-db-metrics/apps/spring-boot-admin/src/main/resources/application.yml +++ /dev/null @@ -1,18 +0,0 @@ -server: - port: 8080 -spring: - application: # Application-Info for the Info-Actuator - name: "@pom.artifactId@" - boot: - admin: - client: - # Self-Registration @itself - url: "http://localhost:8080/" -management: # Actuator Configuration - endpoints: - web: - exposure: - include: "*" - endpoint: # Health-Actuator - health: - show-details: always diff --git a/cache-and-db-metrics/buildAndRunAll.sh b/cache-and-db-metrics/buildAndRunAll.sh index 948af1b..41bd472 100755 --- a/cache-and-db-metrics/buildAndRunAll.sh +++ b/cache-and-db-metrics/buildAndRunAll.sh @@ -8,4 +8,4 @@ cd apps/hello-data ./build.sh cd ../.. -docker-compose up -d +docker compose up -d