From bb006d1dbf049b9916c773174ccfc4d8cab9223f Mon Sep 17 00:00:00 2001 From: Pascal Grimaud Date: Thu, 6 Jan 2022 18:22:02 +0100 Subject: [PATCH 1/3] Spring Cloud Consul: add central config --- .../springcloud/consul/domain/Consul.java | 5 +++++ .../consul/domain/ConsulDomainService.java | 12 ++++++++++++ .../consul/docker/application.config.yml.mustache | 12 ++++++++++++ .../consul/src/bootstrap.properties.mustache | 2 ++ .../springcloud/consul/src/consul.yml.mustache | 13 +++++++++++++ .../consul/application/ConsulAssert.java | 2 +- 6 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 src/main/resources/generator/server/springboot/springcloud/consul/docker/application.config.yml.mustache diff --git a/src/main/java/tech/jhipster/lite/generator/server/springboot/springcloud/consul/domain/Consul.java b/src/main/java/tech/jhipster/lite/generator/server/springboot/springcloud/consul/domain/Consul.java index 6901ca2d00a..7e78c4fb735 100644 --- a/src/main/java/tech/jhipster/lite/generator/server/springboot/springcloud/consul/domain/Consul.java +++ b/src/main/java/tech/jhipster/lite/generator/server/springboot/springcloud/consul/domain/Consul.java @@ -8,6 +8,7 @@ public class Consul { public static final String BOOTSTRAP_FAST_PROPERTIES = "bootstrap-fast.properties"; private static final String DOCKER_CONSUL_IMAGE = "consul:1.11.1"; + private static final String DOCKER_CONSUL_CONFIG_LOADER_IMAGE = "jhipster/consul-config-loader:v0.4.1"; private static final String SPRING_CLOUD = "org.springframework.cloud"; private static final String SPRING_CLOUD_VERSION = "2021.0.0"; @@ -21,6 +22,10 @@ public static String getSpringCloudVersion() { return SPRING_CLOUD_VERSION; } + public static String getDockerConsulConfigLoaderImage() { + return DOCKER_CONSUL_CONFIG_LOADER_IMAGE; + } + public static Dependency springCloudDependencyManagement() { return Dependency .builder() diff --git a/src/main/java/tech/jhipster/lite/generator/server/springboot/springcloud/consul/domain/ConsulDomainService.java b/src/main/java/tech/jhipster/lite/generator/server/springboot/springcloud/consul/domain/ConsulDomainService.java index 8384e91d7b7..69f74e3a239 100644 --- a/src/main/java/tech/jhipster/lite/generator/server/springboot/springcloud/consul/domain/ConsulDomainService.java +++ b/src/main/java/tech/jhipster/lite/generator/server/springboot/springcloud/consul/domain/ConsulDomainService.java @@ -5,6 +5,7 @@ import static tech.jhipster.lite.generator.project.domain.DefaultConfig.BASE_NAME; import static tech.jhipster.lite.generator.server.springboot.springcloud.consul.domain.Consul.*; +import tech.jhipster.lite.common.domain.Base64Utils; import tech.jhipster.lite.generator.buildtool.generic.domain.BuildToolService; import tech.jhipster.lite.generator.project.domain.Project; import tech.jhipster.lite.generator.project.domain.ProjectRepository; @@ -49,6 +50,17 @@ public void addProperties(Project project) { @Override public void addDockerConsul(Project project) { project.addConfig("dockerConsulImage", getDockerConsulImage()); + project.addConfig("dockerConsulConfigLoaderImage", getDockerConsulConfigLoaderImage()); + projectRepository.template(project, getPath(SOURCE, "src"), "consul.yml", "src/main/docker", "consul.yml"); + + project.addConfig("base64JwtSecret", Base64Utils.getBase64Secret()); + projectRepository.template( + project, + getPath(SOURCE, "docker"), + "application.config.yml", + "src/main/docker/central-server-config/", + "application.yml" + ); } } diff --git a/src/main/resources/generator/server/springboot/springcloud/consul/docker/application.config.yml.mustache b/src/main/resources/generator/server/springboot/springcloud/consul/docker/application.config.yml.mustache new file mode 100644 index 00000000000..4b753780630 --- /dev/null +++ b/src/main/resources/generator/server/springboot/springcloud/consul/docker/application.config.yml.mustache @@ -0,0 +1,12 @@ +# Common configuration shared between all applications +# This file is in format yaml as it is loaded by https://github.com/jhipster/consul-config-loader +configserver: + name: Docker Consul Service + status: Connected to Consul Server running in Docker + +jhipster: + security: + authentication: + jwt: + # secret key which should be base64 encoded and changed in production + base64-secret: {{{base64JwtSecret}}} diff --git a/src/main/resources/generator/server/springboot/springcloud/consul/src/bootstrap.properties.mustache b/src/main/resources/generator/server/springboot/springcloud/consul/src/bootstrap.properties.mustache index 2376cfdb764..c79986116b1 100644 --- a/src/main/resources/generator/server/springboot/springcloud/consul/src/bootstrap.properties.mustache +++ b/src/main/resources/generator/server/springboot/springcloud/consul/src/bootstrap.properties.mustache @@ -8,3 +8,5 @@ spring.cloud.consul.discovery.tags[0]=version=@project.version@ spring.cloud.consul.discovery.tags[1]=context-path=${server.servlet.context-path:} spring.cloud.consul.host=localhost spring.cloud.consul.port=8500 +spring.cloud.consul.config.format=yaml +spring.cloud.consul.config.profile-separator=- diff --git a/src/main/resources/generator/server/springboot/springcloud/consul/src/consul.yml.mustache b/src/main/resources/generator/server/springboot/springcloud/consul/src/consul.yml.mustache index dc16748cf5b..2d0d07f45de 100644 --- a/src/main/resources/generator/server/springboot/springcloud/consul/src/consul.yml.mustache +++ b/src/main/resources/generator/server/springboot/springcloud/consul/src/consul.yml.mustache @@ -10,3 +10,16 @@ services: - 127.0.0.1:8500:8500 - 127.0.0.1:8600:8600 command: consul agent -dev -ui -client 0.0.0.0 -log-level=INFO + + consul-config-loader: + image: {{dockerConsulConfigLoaderImage}} + volumes: + - ./central-server-config:/config + environment: + - INIT_SLEEP_SECONDS=5 + - CONSUL_URL=consul + - CONSUL_PORT=8500 + # Uncomment to load configuration into Consul from a Git repository + # as configured in central-server-config/git2consul.json + # Also set SPRING_CLOUD_CONSUL_CONFIG_FORMAT=files on your apps + # - CONFIG_MODE=git diff --git a/src/test/java/tech/jhipster/lite/generator/server/springboot/springcloud/consul/application/ConsulAssert.java b/src/test/java/tech/jhipster/lite/generator/server/springboot/springcloud/consul/application/ConsulAssert.java index ebce4055fa6..14e39561763 100644 --- a/src/test/java/tech/jhipster/lite/generator/server/springboot/springcloud/consul/application/ConsulAssert.java +++ b/src/test/java/tech/jhipster/lite/generator/server/springboot/springcloud/consul/application/ConsulAssert.java @@ -85,7 +85,7 @@ public static void assertProperties(Project project) { public static void assertDockerConsul(Project project) { assertFileExist(project, "src/main/docker/consul.yml"); - assertFileContent(project, "src/main/docker/consul.yml", getDockerConsulImage()); + assertFileExist(project, "src/main/docker/central-server-config/application.yml"); } } From 991f6bfbbdf2cf383421c00b35c0c26e1de5e377 Mon Sep 17 00:00:00 2001 From: Pascal Grimaud Date: Thu, 6 Jan 2022 18:49:50 +0100 Subject: [PATCH 2/3] Spring Cloud Consul: improve bootstrap properties --- .../consul/src/bootstrap.properties.mustache | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/main/resources/generator/server/springboot/springcloud/consul/src/bootstrap.properties.mustache b/src/main/resources/generator/server/springboot/springcloud/consul/src/bootstrap.properties.mustache index c79986116b1..4cf8c596527 100644 --- a/src/main/resources/generator/server/springboot/springcloud/consul/src/bootstrap.properties.mustache +++ b/src/main/resources/generator/server/springboot/springcloud/consul/src/bootstrap.properties.mustache @@ -3,10 +3,22 @@ #==================================================================== spring.application.name={{baseName}} -spring.cloud.consul.discovery.health-check-path=/management/health -spring.cloud.consul.discovery.tags[0]=version=@project.version@ -spring.cloud.consul.discovery.tags[1]=context-path=${server.servlet.context-path:} + spring.cloud.consul.host=localhost spring.cloud.consul.port=8500 + spring.cloud.consul.config.format=yaml spring.cloud.consul.config.profile-separator=- +spring.cloud.consul.config.watch.enabled=false + +spring.cloud.consul.discovery.health-check-path=/management/health +spring.cloud.consul.discovery.tags[0]=version=@project.version@ +spring.cloud.consul.discovery.tags[1]=context-path=${server.servlet.context-path:} +spring.cloud.consul.discovery.tags[2]=profile=${spring.profiles.active:} +spring.cloud.consul.discovery.tags[3]=git-version=${git.commit.id.describe:} +spring.cloud.consul.discovery.tags[4]=git-commit=${git.commit.id.abbrev:} +spring.cloud.consul.discovery.tags[5]=git-branch=${git.branch:} + +spring.cloud.consul.discovery.instance-id={{baseName}}:${spring.application.instance-id:${random.value}} +spring.cloud.consul.discovery.service-name={{baseName}} +spring.cloud.consul.discovery.prefer-ip-address=true From c3ee1bdfec982fbbca3abfd8a3ea1afb2c24c5fc Mon Sep 17 00:00:00 2001 From: Pascal Grimaud Date: Thu, 6 Jan 2022 18:54:57 +0100 Subject: [PATCH 3/3] Spring Cloud Consul: fix tests --- .../springcloud/consul/application/ConsulAssert.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/test/java/tech/jhipster/lite/generator/server/springboot/springcloud/consul/application/ConsulAssert.java b/src/test/java/tech/jhipster/lite/generator/server/springboot/springcloud/consul/application/ConsulAssert.java index 14e39561763..a662c3e9e16 100644 --- a/src/test/java/tech/jhipster/lite/generator/server/springboot/springcloud/consul/application/ConsulAssert.java +++ b/src/test/java/tech/jhipster/lite/generator/server/springboot/springcloud/consul/application/ConsulAssert.java @@ -73,8 +73,10 @@ public static void assertProperties(Project project) { "spring.cloud.consul.discovery.health-check-path=/management/health", "spring.cloud.consul.discovery.tags[0]=version=@project.version@", "spring.cloud.consul.discovery.tags[1]=context-path=${server.servlet.context-path:}", - "spring.cloud.consul.host=localhost", - "spring.cloud.consul.port=8500" + "spring.cloud.consul.discovery.tags[2]=profile=${spring.profiles.active:}", + "spring.cloud.consul.discovery.tags[3]=git-version=${git.commit.id.describe:}", + "spring.cloud.consul.discovery.tags[4]=git-commit=${git.commit.id.abbrev:}", + "spring.cloud.consul.discovery.tags[5]=git-branch=${git.branch:}" ) );