Commit 1dd48ec0 authored by Michael's avatar Michael

add complex properties config example

parent 905e1e23
......@@ -2,4 +2,4 @@ FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
ADD ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Dspring.profiles.active=${stage}", "-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
\ No newline at end of file
ENTRYPOINT ["java","-Dspring.profiles.active=${stage}", "-Dmanagement.security.enabled=false", "-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
\ No newline at end of file
# Spring Demo Application
This is an rather minimalistic Java Spring Boot Demo application illustrating external configuration using environment variables and yaml featuring different stages in a Docker deployment.
This is an rather minimalistic Java Spring Boot Demo application illustrating external configuration using different methods in a Docker deployment (see [Showcased](#Showcased) for more information).
## Requirements
......@@ -23,8 +23,19 @@ Clone or download thist repository and change into this folder.
## Access
- localhost:8080
- Actuator security is disabled: curl http://localhost:8080/env
## Showcased
1. Systemenvironment variables from .env file
1. Systemenvironment variables from docker-compose file
1. Config variables from .yaml file
1. Config variable from .properties file including nesting and arrays
1. Environment-based spring profile configuration
## Official Documentation
[spring.io getting started](https://spring.io/guides/gs/spring-boot/)
[spring.io external config](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html)
\ No newline at end of file
[spring.io external config](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html)
[spring.io endpoint security](https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-monitoring.html)
[spring.io spring application properties](https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html)
\ No newline at end of file
foo=bar
\ No newline at end of file
foo=barFROMfile
\ No newline at end of file
......@@ -2,10 +2,15 @@ version: "3"
services:
demo:
container_name: spring-demo
image: rootknecht.net/spring-demo:latest
environment:
- foo=bar
- stage=prod
- stage=dev
- DATABASE_USER=db
- DATABASE_PASSWORD=db
- DATABASE_NAME=db
- DATABASE_HOST=db
- DATABASE_PORT=1234
env_file:
- ./conf/data.env
ports:
......
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework</groupId>
<artifactId>spring-demo</artifactId>
<version>0.1.0</version>
<packaging>jar</packaging>
<name>Spring Boot Docker</name>
<description>Minimal Spring Boot Variable Injection Demo</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath />
</parent>
<properties>
<docker.image.prefix>rootknecht.net</docker.image.prefix>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<repository>${docker.image.prefix}/${project.artifactId}</repository>
<buildArgs>
<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
<buildArgs>
<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
......@@ -65,7 +60,6 @@
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
......@@ -76,6 +70,23 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>1.4.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
<repositories>
<repository>
<id>projectlombok.org</id>
<url>http://projectlombok.org/mavenrepo</url>
</repository>
</repositories>
</project>
\ No newline at end of file
......@@ -17,12 +17,14 @@ public class Application {
private String foo;
@Autowired
private ApplicationProperties applicationProperties;
private YamlProperties yamlProperties;
@Autowired
private Properties properties;
@RequestMapping("/")
public String home() {
return "Hello " + foo + "<br>" + applicationProperties;
return "Hello " + foo + "<br>---<br>" + yamlProperties + "<br>---<br>" + properties;
}
public static void main(String[] args) {
......
package demo;
import java.util.List;
import java.util.Arrays;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.boot.context.properties.ConfigurationProperties;
import javax.validation.constraints.Max;
@Component
@Configuration
@ConfigurationProperties("app")
public class Properties {
private Cache cache;
private Cors cors;
public Cache getCache() {
return cache;
}
public Cors getCors() {
return cors;
}
public void setCache(Cache cache) {
this.cache = cache;
}
public void setCors(Cors cors) {
this.cors = cors;
}
public static class Cache {
@Max(1000)
private Integer ttl;
@Max(3600)
private Long maxEntries;
public Integer getTtl() {
return ttl;
}
public Long getMaxEntries() {
return maxEntries;
}
public void setTtl(Integer ttl) {
this.ttl = ttl;
}
public void setMaxEntries(Long maxEntries) {
this.maxEntries = maxEntries;
}
@Override
public String toString() {
return "Cache{" +
"ttl=" + ttl +
", maxEntries=" + maxEntries +
'}';
}
}
public static class Cors {
private List<String> allowedOrigins;
private String[] allowedMethods;
private List<String> allowedHeaders;
private Boolean allowCredentials;
private Integer maxAge;
public List<String> getAllowedOrigins() {
return allowedOrigins;
}
public Boolean getAllowCredentials() {
return allowCredentials;
}
public List<String> getAllowedHeaders() {
return allowedHeaders;
}
public String[] getAllowedMethods() {
return allowedMethods;
}
public Integer getMaxAge() {
return maxAge;
}
public void setAllowCredentials(Boolean allowCredentials) {
this.allowCredentials = allowCredentials;
}
public void setAllowedHeaders(List<String> allowedHeaders) {
this.allowedHeaders = allowedHeaders;
}
public void setAllowedMethods(String[] allowedMethods) {
this.allowedMethods = allowedMethods;
}
public void setAllowedOrigins(List<String> allowedOrigins) {
this.allowedOrigins = allowedOrigins;
}
public void setMaxAge(Integer maxAge) {
this.maxAge = maxAge;
}
@Override
public String toString() {
return "Cors{" +
"allowedOrigins=" + allowedOrigins +
", allowedMethods=" + Arrays.toString(allowedMethods) +
", allowedHeaders=" + allowedHeaders +
", allowCredentials=" + allowCredentials +
", maxAge=" + maxAge +
'}';
}
}
@Override
public String toString() {
return "Properties{" +
"cache=" + cache +
", cors=" + cors +
'}';
}
}
\ No newline at end of file
......@@ -8,7 +8,7 @@ import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties("server")
public class ApplicationProperties {
public class YamlProperties {
private String email;
private List<Cluster> cluster = new ArrayList<>();
......@@ -60,7 +60,7 @@ public class ApplicationProperties {
@Override
public String toString() {
return "ServerProperties{" +
return "YamlProperties{" +
"email='" + email + '\'' +
", cluster=" + cluster +
'}';
......
BAR_FIRST_NAME=Mustermann
\ No newline at end of file
app.cache.ttl=86400
app.cache.max-entries=1000
app.cors.allowed-origins[0]=*
app.cors.allowed-methods[0]=GET
app.cors.allowed-methods[1]=PUT
app.cors.allowed-methods[2]=POST
app.cors.allowed-methods[3]=DELETE
app.cors.allowed-methods[4]=OPTIONS
app.cors.allowed-headers[0]=*
app.cors.allow-credentials=true
app.cors.max-age=3600
......@@ -6,6 +6,7 @@ logging:
spring:
profiles:
# wil be set by docker-compose file
# active: "dev"
# include: "prod"
main:
......@@ -18,6 +19,14 @@ server:
spring:
profiles: dev
datasource:
url: jdbc:mysql://localhost:3306/demo
username: root
password: p4SSW0rd
platform: mysql
initialize: false
jpa:
database-platform: org.hibernate.dialect.MySQLDialect
server:
email: dev@rootknecht.net
cluster:
......@@ -32,6 +41,11 @@ server:
spring:
profiles: prod
datasource:
url: jdbc:mysql://${DATABASE_HOST}:${DATABASE_PORT}/${DATABASE_NAME}
username: ${DATABASE_USER}
password: ${DATABASE_PASSWORD}
initialize: true
server:
email: prod@rootknecht.net
cluster:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment