From a4cd60cb8252adb0dd8fa3d4f1d1ec0b1fcb07c3 Mon Sep 17 00:00:00 2001 From: "erik.petzold" Date: Fri, 13 Dec 2024 09:55:17 +0100 Subject: [PATCH] start example for consul --- consul/README.md | 28 ++++++ consul/apps/hello-world/.gitignore | 5 ++ consul/apps/hello-world/Dockerfile | 8 ++ consul/apps/hello-world/build.sh | 6 ++ consul/apps/hello-world/pom.xml | 84 ++++++++++++++++++ .../de/codecentric/helloworld/HelloWorld.java | 14 +++ .../helloworld/HelloWorldApplication.java | 17 ++++ .../src/main/resources/application.yml | 47 ++++++++++ consul/apps/spring-boot-admin/.gitignore | 5 ++ consul/apps/spring-boot-admin/Dockerfile | 8 ++ consul/apps/spring-boot-admin/build.sh | 6 ++ consul/apps/spring-boot-admin/pom.xml | 86 +++++++++++++++++++ .../SpringBootAdminApplication.java | 18 ++++ .../src/main/resources/application.yml | 51 +++++++++++ consul/buildAndRunAll.sh | 11 +++ consul/docker-compose.yml | 26 ++++++ eureka/README.md | 6 +- eureka/buildAndRunAll.sh | 2 +- nacos/README.md | 2 +- nacos/buildAndRunAll.sh | 2 +- nginx/README.md | 2 +- nginx/buildAndRunAll.sh | 2 +- 22 files changed, 428 insertions(+), 8 deletions(-) create mode 100644 consul/README.md create mode 100644 consul/apps/hello-world/.gitignore create mode 100644 consul/apps/hello-world/Dockerfile create mode 100755 consul/apps/hello-world/build.sh create mode 100644 consul/apps/hello-world/pom.xml create mode 100644 consul/apps/hello-world/src/main/java/de/codecentric/helloworld/HelloWorld.java create mode 100644 consul/apps/hello-world/src/main/java/de/codecentric/helloworld/HelloWorldApplication.java create mode 100644 consul/apps/hello-world/src/main/resources/application.yml create mode 100644 consul/apps/spring-boot-admin/.gitignore create mode 100644 consul/apps/spring-boot-admin/Dockerfile create mode 100755 consul/apps/spring-boot-admin/build.sh create mode 100644 consul/apps/spring-boot-admin/pom.xml create mode 100644 consul/apps/spring-boot-admin/src/main/java/de/codecentric/springbootadmin/SpringBootAdminApplication.java create mode 100644 consul/apps/spring-boot-admin/src/main/resources/application.yml create mode 100755 consul/buildAndRunAll.sh create mode 100644 consul/docker-compose.yml diff --git a/consul/README.md b/consul/README.md new file mode 100644 index 0000000..729985b --- /dev/null +++ b/consul/README.md @@ -0,0 +1,28 @@ +## Spring Boot Admin - Consul Example + +Allows to run Spring Boot Admin and a sample app (hello world) with service discovery via consul. + +This Readme will guide you through all setup steps for the infrastructure. + +TODO admin cannot yet call the services, some networking issue needs to be solved + +## 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. +```bash +chmod u+x buildAndRunAll.sh +./buildAndRunAll.sh +``` +Consul UI: http://localhost:8500/ + +Admin UI: http://localhost:8080/ + +## Stop Everything +```bash +docker compose down -v +``` \ No newline at end of file diff --git a/consul/apps/hello-world/.gitignore b/consul/apps/hello-world/.gitignore new file mode 100644 index 0000000..b18abdf --- /dev/null +++ b/consul/apps/hello-world/.gitignore @@ -0,0 +1,5 @@ +target +.idea +*.iml +*.log +*.gz diff --git a/consul/apps/hello-world/Dockerfile b/consul/apps/hello-world/Dockerfile new file mode 100644 index 0000000..0f5052e --- /dev/null +++ b/consul/apps/hello-world/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/consul/apps/hello-world/build.sh b/consul/apps/hello-world/build.sh new file mode 100755 index 0000000..b6c3ad4 --- /dev/null +++ b/consul/apps/hello-world/build.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +# Build App +mvn package +# Build Docker Image +docker build --tag hello-world-consul . diff --git a/consul/apps/hello-world/pom.xml b/consul/apps/hello-world/pom.xml new file mode 100644 index 0000000..f831390 --- /dev/null +++ b/consul/apps/hello-world/pom.xml @@ -0,0 +1,84 @@ + + + 4.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 3.2.1 + + + de.codecentric + hello-world-consul + jar + ${project.artifactId} + 1.0.0-SNAPSHOT + Hello World + + + 17 + UTF-8 + 2023.0.0 + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + + + + + org.springframework.boot + spring-boot-starter-web + + + + + org.springframework.boot + spring-boot-starter-actuator + + + + org.springframework.cloud + spring-cloud-starter-consul-discovery + + + + + app + + + org.springframework.boot + spring-boot-maven-plugin + + + repackage + + repackage + + build-info + + + + + + + io.github.git-commit-id + git-commit-id-maven-plugin + + true + + + + + + diff --git a/consul/apps/hello-world/src/main/java/de/codecentric/helloworld/HelloWorld.java b/consul/apps/hello-world/src/main/java/de/codecentric/helloworld/HelloWorld.java new file mode 100644 index 0000000..d3eeb2b --- /dev/null +++ b/consul/apps/hello-world/src/main/java/de/codecentric/helloworld/HelloWorld.java @@ -0,0 +1,14 @@ +package de.codecentric.helloworld; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class HelloWorld { + + @GetMapping("/") + public String hello() { + return "Hello World!"; + } + +} diff --git a/consul/apps/hello-world/src/main/java/de/codecentric/helloworld/HelloWorldApplication.java b/consul/apps/hello-world/src/main/java/de/codecentric/helloworld/HelloWorldApplication.java new file mode 100644 index 0000000..954eb00 --- /dev/null +++ b/consul/apps/hello-world/src/main/java/de/codecentric/helloworld/HelloWorldApplication.java @@ -0,0 +1,17 @@ +package de.codecentric.helloworld; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.metrics.buffering.BufferingApplicationStartup; + +@SpringBootApplication +public class HelloWorldApplication { + + public static void main(String... args) { + var app = new SpringApplication(HelloWorldApplication.class); + // Buffering for Startup-Actuator + app.setApplicationStartup(new BufferingApplicationStartup(2048)); + app.run(); + } + +} diff --git a/consul/apps/hello-world/src/main/resources/application.yml b/consul/apps/hello-world/src/main/resources/application.yml new file mode 100644 index 0000000..92f2abe --- /dev/null +++ b/consul/apps/hello-world/src/main/resources/application.yml @@ -0,0 +1,47 @@ +server: + port: 8080 +spring: + application: # Application-Info for the Info-Actuator + name: "@pom.artifactId@" + cloud: + consul: + host: consul-server +management: # Actuator Configuration + server: + port: 8081 + endpoints: + web: + exposure: + include: "*" + endpoint: # Health-Actuator + health: + show-details: always + probes: + enabled: true + env: # Environment-Actuator + show-values: always # caution: can reveal passwords + configprops: # Configuration-Actuator + show-values: always # caution: can reveal passwords + info: # Info-Actuator + java: + enabled: true + os: + enabled: true + build: + enabled: true + env: + enabled: true + git: + enabled: true +info: # Application-Info for the Info-Actuator + group: "@pom.groupId@" + artifact: "@pom.artifactId@" + description: "@pom.description@" + version: "@pom.version@" + spring-boot: "@pom.parent.version@" + # Tags for the Spring Boot Admin UI + tags: + spring-boot: "@pom.parent.version@" +logging: # Logging-File for the Logfile-Actuator + file: + name: "hello-world.log" diff --git a/consul/apps/spring-boot-admin/.gitignore b/consul/apps/spring-boot-admin/.gitignore new file mode 100644 index 0000000..b18abdf --- /dev/null +++ b/consul/apps/spring-boot-admin/.gitignore @@ -0,0 +1,5 @@ +target +.idea +*.iml +*.log +*.gz diff --git a/consul/apps/spring-boot-admin/Dockerfile b/consul/apps/spring-boot-admin/Dockerfile new file mode 100644 index 0000000..0f5052e --- /dev/null +++ b/consul/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/consul/apps/spring-boot-admin/build.sh b/consul/apps/spring-boot-admin/build.sh new file mode 100755 index 0000000..7d1ba74 --- /dev/null +++ b/consul/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-consul . diff --git a/consul/apps/spring-boot-admin/pom.xml b/consul/apps/spring-boot-admin/pom.xml new file mode 100644 index 0000000..c5650bb --- /dev/null +++ b/consul/apps/spring-boot-admin/pom.xml @@ -0,0 +1,86 @@ + + + 4.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 3.2.1 + + + de.codecentric + spring-boot-admin-consul + jar + ${project.artifactId} + 1.0.0-SNAPSHOT + Spring Boot Admin + + + 17 + UTF-8 + 3.2.1 + 2023.0.0 + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + + + + + org.springframework.boot + spring-boot-starter-web + + + + + de.codecentric + spring-boot-admin-starter-server + ${spring-boot-admin.version} + + + + org.springframework.cloud + spring-cloud-starter-consul-discovery + + + + + app + + + org.springframework.boot + spring-boot-maven-plugin + + + repackage + + repackage + + build-info + + + + + + + io.github.git-commit-id + git-commit-id-maven-plugin + + true + + + + + + diff --git a/consul/apps/spring-boot-admin/src/main/java/de/codecentric/springbootadmin/SpringBootAdminApplication.java b/consul/apps/spring-boot-admin/src/main/java/de/codecentric/springbootadmin/SpringBootAdminApplication.java new file mode 100644 index 0000000..d16b7ab --- /dev/null +++ b/consul/apps/spring-boot-admin/src/main/java/de/codecentric/springbootadmin/SpringBootAdminApplication.java @@ -0,0 +1,18 @@ +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) { + var app = new SpringApplication(SpringBootAdminApplication.class); + // Buffering for Startup-Actuator + app.setApplicationStartup(new BufferingApplicationStartup(2048)); + app.run(); + } +} diff --git a/consul/apps/spring-boot-admin/src/main/resources/application.yml b/consul/apps/spring-boot-admin/src/main/resources/application.yml new file mode 100644 index 0000000..f6accca --- /dev/null +++ b/consul/apps/spring-boot-admin/src/main/resources/application.yml @@ -0,0 +1,51 @@ +server: + port: 8080 +spring: + application: # Application-Info for the Info-Actuator + name: "@pom.artifactId@" + cloud: + consul: + host: consul-server +management: # Actuator Configuration + server: + port: 8081 + endpoints: + web: + exposure: + include: "*" + endpoint: # Health-Actuator + health: + show-details: always + probes: + enabled: true + env: # Environment-Actuator + show-values: always # caution: can reveal passwords + configprops: # Configuration-Actuator + show-values: always # caution: can reveal passwords + info: # Info-Actuator + java: + enabled: true + os: + enabled: true + build: + enabled: true + env: + enabled: true + git: + enabled: true +info: # Application-Info for the Info-Actuator + group: "@pom.groupId@" + artifact: "@pom.artifactId@" + description: "@pom.description@" + version: "@pom.version@" + spring-boot: "@pom.parent.version@" + spring-boot-admin: "@spring-boot-admin.version@" + spring-cloud: "@spring-cloud.version@" + # Tags for the Spring Boot Admin UI + tags: + spring-boot: "@pom.parent.version@" + spring-boot-admin: "@spring-boot-admin.version@" + spring-cloud: "@spring-cloud.version@" +logging: # Logging-File for the Logfile-Actuator + file: + name: "spring-boot-admin.log" diff --git a/consul/buildAndRunAll.sh b/consul/buildAndRunAll.sh new file mode 100755 index 0000000..df59a3e --- /dev/null +++ b/consul/buildAndRunAll.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +cd apps/spring-boot-admin +./build.sh +cd ../.. + +cd apps/hello-world +./build.sh +cd ../.. + +docker compose up -d diff --git a/consul/docker-compose.yml b/consul/docker-compose.yml new file mode 100644 index 0000000..6c2151f --- /dev/null +++ b/consul/docker-compose.yml @@ -0,0 +1,26 @@ +services: + hello-world: + container_name: hello-world-consul + image: hello-world-consul + ports: + - "8180:8080" + - "8181:8081" + + spring-boot-admin: + container_name: spring-boot-admin-consul + image: spring-boot-admin-consul + ports: + - "8080:8080" + - "8081:8081" + + +# consul-agent: +# image: hashicorp/consul:latest +# command: "agent -retry-join consul-server -client 0.0.0.0" + + + consul-server: + image: hashicorp/consul:latest + command: "agent -server -ui -node=server-1 -bootstrap-expect=1 -client 0.0.0.0" + ports: + - "8500:8500" diff --git a/eureka/README.md b/eureka/README.md index 7fc3076..787acc2 100644 --- a/eureka/README.md +++ b/eureka/README.md @@ -1,6 +1,6 @@ ## Spring Boot Admin - Eureka 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 a sample app (hello world) with service discovery via eureka. This Readme will guide you through all setup steps for the infrastructure. @@ -11,7 +11,7 @@ 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 @@ -22,5 +22,5 @@ Admin UI: http://localhost:8080/ ## Stop Everything ```bash -docker-compose down -v +docker compose down -v ``` diff --git a/eureka/buildAndRunAll.sh b/eureka/buildAndRunAll.sh index bb8efa6..d100cec 100755 --- a/eureka/buildAndRunAll.sh +++ b/eureka/buildAndRunAll.sh @@ -12,4 +12,4 @@ cd apps/hello-world ./build.sh cd ../.. -docker-compose up -d +docker compose up -d diff --git a/nacos/README.md b/nacos/README.md index b8ca632..d580ed3 100644 --- a/nacos/README.md +++ b/nacos/README.md @@ -21,7 +21,7 @@ http://localhost:8080/ ## Stop Everything ```bash -docker-compose down -v +docker compose down -v ``` ## Step-by-step Guide diff --git a/nacos/buildAndRunAll.sh b/nacos/buildAndRunAll.sh index 11fb59e..41960b1 100755 --- a/nacos/buildAndRunAll.sh +++ b/nacos/buildAndRunAll.sh @@ -12,4 +12,4 @@ cd apps/health-simulator ./build.sh cd ../.. -docker-compose up -d +docker compose up -d diff --git a/nginx/README.md b/nginx/README.md index 18c02f5..495b72a 100644 --- a/nginx/README.md +++ b/nginx/README.md @@ -23,5 +23,5 @@ chmod u+x buildAndRunAll.sh ## Stop Everything ```bash -docker-compose down -v +docker compose down -v ``` \ No newline at end of file diff --git a/nginx/buildAndRunAll.sh b/nginx/buildAndRunAll.sh index 3588ffb..df59a3e 100755 --- a/nginx/buildAndRunAll.sh +++ b/nginx/buildAndRunAll.sh @@ -8,4 +8,4 @@ cd apps/hello-world ./build.sh cd ../.. -docker-compose up -d +docker compose up -d