...
 
Commits (19)
*.sw?
.#*
*#
*~
.classpath
.project
.jdk8
.settings/
bin
build
target
dependency-reduced-pom.xml
*.sublime-*
/scratch
.gradle
Guardfile
README.html
*.iml
.idea
.directory
\ No newline at end of file
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Java",
"type": "java",
"request": "launch",
"stopOnEntry": true,
"jdkPath": "${env:JAVA_HOME}/bin",
"cwd": "${fileDirname}",
"startupClass": "${fileBasenameNoExtension}",
"classpath": [
".",
"${fileDirname}"
]
},
{
"name": "Java Console App",
"type": "java",
"request": "launch",
"stopOnEntry": true,
"jdkPath": "${env:JAVA_HOME}/bin",
"cwd": "${fileDirname}",
"startupClass": "${fileBasenameNoExtension}",
"classpath": [
".",
"${fileDirname}"
],
"externalConsole": true
},
{
"type": "java",
"name": "Debug (Launch)-Application<spring-demo>",
"request": "launch",
"cwd": "${workspaceFolder}",
"mainClass": "demo.Application",
"projectName": "spring-demo",
"args": ""
},
{
"type": "java",
"name": "Debug (Attach)",
"request": "attach",
"hostName": "localhost",
"port": 0
}
]
}
\ No newline at end of file
{
"java.configuration.updateBuildConfiguration": "automatic"
}
\ No newline at end of file
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [{
"label": "verify",
"type": "shell",
"command": "mvn -B verify",
"group": "build"
},
{
"label": "install",
"type": "shell",
"command": "mvn -B install dockerfile:build",
"group": "build"
},
{
"label": "build",
"type": "shell",
"command": "mvn -B build",
"group": "build"
},
{
"label": "clean",
"type": "shell",
"command": "mvn -B clean",
"group": "build"
}
]
}
\ No newline at end of file
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
ADD ${JAR_FILE} app.jar
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 different methods in a Docker deployment (see [Showcased](#showcased) for more information).
## Requirements
- Java JDK >1.8
- Maven
- Docker
- Docker-compose
- default port 8080
## Build Docker Image
Clone or download thist repository and change into this folder.
- mvn package dockerfile:build
## Run Docker Image
- docker-copose up [-d]
## Access
- localhost:8080
- Actuator security is disabled: curl http://localhost:8080/env
- All actuators are exposed and accessible: http://localhost:8080/actuator/
## 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)
- [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)
- [Spring Boot Metric](https://spring.io/blog/2018/03/16/micrometer-spring-boot-2-s-new-application-metrics-collector)
- [Exposing Endpoints](https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready-endpoints-exposing-endpoints)
\ No newline at end of file
foo=barFROMfile
\ No newline at end of file
version: "3"
services:
demo:
container_name: spring-demo
image: rootknecht.net/spring-demo:latest
environment:
- stage=dev
- DATABASE_USER=db
- DATABASE_PASSWORD=db
- DATABASE_NAME=db
- DATABASE_HOST=db
- DATABASE_PORT=1234
- ENDPOINTS_ENABLED=true
- ENDPOINTS_SENSITIVE=true
- MANAGEMENT_CONTEXT-PATH=/actuator
env_file:
- ./conf/data.env
ports:
- 8080:8080
\ No newline at end of file
<?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">
<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.4.3.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>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<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>
<dependency>
<groupId>com.moelholm</groupId>
<artifactId>prometheus-spring-boot-starter</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.springframework.hateoas</groupId>
<artifactId>spring-hateoas</artifactId>
</dependency>
</dependencies>
<repositories>
<repository>
<id>projectlombok.org</id>
<url>http://projectlombok.org/mavenrepo</url>
</repository>
</repositories>
</project>
package demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class Application {
// Simple Environment-Value
// Can be overwritten in docker-compose
@Value("${foo:default_value}")
private String foo;
@Autowired
private YamlProperties yamlProperties;
@Autowired
private Properties properties;
@RequestMapping("/")
public String home() {
return "Hello " + foo + "<br>---<br>" + yamlProperties + "<br>---<br>" + properties;
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
package demo;
import java.util.List;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import lombok.Data;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import javax.validation.constraints.Max;
@Data
@ToString(includeFieldNames = false)
@Component
@Configuration
@ConfigurationProperties("app")
public class Properties {
private Cache cache;
private Cors cors;
@Data
@ToString(includeFieldNames = false)
public static class Cache {
@Max(1000)
private Integer ttl;
@Max(3600)
private Long maxEntries;
}
@Data
@ToString(includeFieldNames = true)
public static class Cors {
private List<String> allowedOrigins;
private String[] allowedMethods;
private List<String> allowedHeaders;
private Boolean allowCredentials;
private Integer maxAge;
}
}
\ No newline at end of file
package demo;
import java.util.ArrayList;
import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import lombok.Data;
import lombok.ToString;
@Data
@ToString(includeFieldNames = false)
@Component
@ConfigurationProperties("server")
public class YamlProperties {
private String email;
private List<Cluster> cluster = new ArrayList<>();
@Data
@ToString(includeFieldNames = false)
public static class Cluster {
private String ip;
private String path;
}
}
\ 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
logging:
level:
.: error
org.springframework: ERROR
com.mkyong: ERROR
spring:
profiles:
# wil be set by docker-compose file
# active: "dev"
# include: "prod"
main:
banner-mode: "off"
server:
email: default@rootknecht.net
---
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:
- ip: 127.0.0.1
path: /dev1
- ip: 127.0.0.2
path: /dev2
- ip: 127.0.0.3
path: /dev3
---
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:
- ip: 192.168.0.1
path: /app1
- ip: 192.168.0.2
path: /app2
- ip: 192.168.0.3
path: /app3
/*
* Copyright 2012-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package demo;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@DirtiesContext
public class HelloWorldConfigurationTests {
@LocalServerPort
private int port;
@Autowired
private TestRestTemplate restTemplate;
@Test
public void testGreeting() throws Exception {
ResponseEntity<String> entity = restTemplate
.getForEntity("http://localhost:" + this.port + "/", String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
}
}