Skip to content

Commit

Permalink
Merge pull request #494 from pascalgrimaud/spring-cloud-consul-config
Browse files Browse the repository at this point in the history
Spring Cloud Consul: add central config
  • Loading branch information
pascalgrimaud authored Jan 6, 2022
2 parents fa928bb + c3ee1bd commit 2c76f1b
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"
);
}
}
Original file line number Diff line number Diff line change
@@ -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}}}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,22 @@
#====================================================================

spring.application.name={{baseName}}

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][email protected]@
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:}

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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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][email protected]@",
"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:}"
)
);

Expand All @@ -85,7 +87,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");
}
}

0 comments on commit 2c76f1b

Please sign in to comment.