Skip to content

Commit

Permalink
Spring Cloud Consul: add central config
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalgrimaud committed Jan 6, 2022
1 parent fa928bb commit bb006d1
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 1 deletion.
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 @@ -8,3 +8,5 @@ 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.config.format=yaml
spring.cloud.consul.config.profile-separator=-
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 @@ -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");
}
}

0 comments on commit bb006d1

Please sign in to comment.