Update React / Spring / MySQL example (#99)

* Update README.md title from NodeJS to Spring

Signed-off-by: Friedrich Greiner <greinerfriedrich@gmail.com>

* Always restart spring backend service

Signed-off-by: Friedrich Greiner <greinerfriedrich@gmail.com>

* Also pass db-password secret to spring backend

Signed-off-by: Friedrich Greiner <greinerfriedrich@gmail.com>

* Add healthcheck to the mysql service

 - a start_period of 30s + 15s (interval * retries) should be long enough for mysql to initialize

Signed-off-by: Friedrich Greiner <greinerfriedrich@gmail.com>
This commit is contained in:
frgegr 2021-03-22 09:58:41 +01:00 committed by GitHub
parent a92c067f75
commit a2cf9cc9f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 2 deletions

View File

@ -1,5 +1,5 @@
## Compose sample application ## Compose sample application
### React application with a NodeJS backend and a MySQL database ### React application with a Spring backend and a MySQL database
Project structure: Project structure:
``` ```

View File

@ -0,0 +1,30 @@
package com.company.project.configuration;
import java.io.IOException;
import java.nio.charset.Charset;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.util.StreamUtils;
/**
* Read property from docker secret file.
*/
public class DockerSecretsProcessor implements EnvironmentPostProcessor {
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
Resource resource = new FileSystemResource("/run/secrets/db-password");
if (resource.exists() && System.getProperty("MYSQL_PASSWORD") == null) {
try {
String dbPassword = StreamUtils.copyToString(resource.getInputStream(), Charset.defaultCharset());
System.setProperty("MYSQL_PASSWORD", dbPassword);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}

View File

@ -0,0 +1 @@
org.springframework.boot.env.EnvironmentPostProcessor=com.company.project.configuration.DockerSecretsProcessor

View File

@ -2,19 +2,28 @@ version: "3.7"
services: services:
backend: backend:
build: backend build: backend
restart: always
secrets:
- db-password
environment: environment:
MYSQL_HOST: db MYSQL_HOST: db
networks: networks:
- react-spring - react-spring
- spring-mysql - spring-mysql
depends_on: depends_on:
- db db:
condition: service_healthy
db: db:
environment: environment:
MYSQL_DATABASE: example MYSQL_DATABASE: example
MYSQL_ROOT_PASSWORD_FILE: /run/secrets/db-password MYSQL_ROOT_PASSWORD_FILE: /run/secrets/db-password
image: mysql:8.0.19 image: mysql:8.0.19
restart: always restart: always
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "127.0.0.1", "--silent"]
interval: 3s
retries: 5
start_period: 30s
secrets: secrets:
- db-password - db-password
volumes: volumes: