...
 
Commits (20)
*.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
# Java Spring Boot 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).
Additionally, you can spin up a minimal monitoring setup with preconfigured [Prometheus](https://prometheus.io/) and [Grafana](https://grafana.com/) services
> There is also a branch for the spring-boot 1.4 release (not fully equivalet)
## Requirements
- Java JDK >1.8
- Maven
- Docker
- Docker-compose
- default port 8080
- port 3000 and 9090 for Grafana and Prometheus
## Build Docker Image
Clone or download thist repository and cd into this folder.
```bash
mvn package dockerfile:build
```
## Run Docker Image
```bash
docker-copose up [-d]
```
## Accessing the demo application
- Application output of different external configuration options: `curl localhost:8080`
- Actuator security is disabled: `curl http://localhost:8080/env`
- All actuators are exposed and accessible: `curl http://localhost:8080/actuator/`
## External configuration showcased
1. Systemenvironment variables from .env file
2. Systemenvironment variables from docker-compose file
3. Config variables from .yaml file
4. Config variable from .properties file including nesting and arrays
5. Environment-based spring profile configuration
## Monitoring
```bash
docker-compose -f docker-compose.prom.yml up [-d]
```
Access [Grafana](localhost:3000) (admin:admin) and [Prometheus](localhost:9090) and find a configured datasource as well as dashboards for showing prometheus itself and a basic java Micrometer dashboard.
## Clean up containers
```bash
docker-compose -f docker-compose.prom.yml -f docker-compose.yml down --remove-orphans
```
## 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.1'
volumes:
prometheus_data: {}
grafana_data: {}
networks:
front-tier:
back-tier:
services:
prometheus:
image: prom/prometheus:v2.2.0
container_name: prometheus
user: "0" # runs as root!
volumes:
- ./prometheus/:/etc/prometheus/
- prometheus_data:/prometheus
command:
- '--config.file=/etc/prometheus/prometheus.yml'
- '--storage.tsdb.path=/prometheus'
- '--web.console.libraries=/usr/share/prometheus/console_libraries'
- '--web.console.templates=/usr/share/prometheus/consoles'
ports:
- 9090:9090
networks:
- back-tier
restart: always
grafana:
image: grafana/grafana:5.2.2
container_name: grafana
user: "1000"
depends_on:
- prometheus
ports:
- 3000:3000
volumes:
- grafana_data:/var/lib/grafana:rw
- ./grafana/provisioning/:/etc/grafana/provisioning/
environment:
- GF_SECURITY_ADMIN_PASSWORD=admin
- GF_USERS_ALLOW_SIGN_UP=false
networks:
- back-tier
- front-tier
restart: always
\ No newline at end of file
version: "3.1"
services:
spring-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
- MANAGEMENT_ENDPOINTS_WEB_EXPOSURE_INCLUDE=*
- MANAGEMENT_ENDPOINTS_JMX_EXPOSURE_INCLUDE=*
- MANAGEMENT_ENDPOINT_METRICS_ENABLED=true
- MANAGEMENT_ENDPOINT_PROMETHEUS_ENABLED=true
env_file:
- ./conf/data.env
restart: always
networks:
- back-tier
ports:
- 8080:8080
\ No newline at end of file
apiVersion: 1
providers:
- name: 'Prometheus'
orgId: 1
folder: ''
type: file
disableDeletion: false
editable: true
options:
path: /etc/grafana/provisioning/dashboards
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
# config file version
apiVersion: 1
# list of datasources that should be deleted from the database
deleteDatasources:
- name: Prometheus
orgId: 1
# list of datasources to insert/update depending
# whats available in the database
datasources:
# <string, required> name of the datasource. Required
- name: Prometheus
# <string, required> datasource type. Required
type: prometheus
# <string, required> access mode. direct or proxy. Required
access: proxy
# <int> org id. will default to orgId 1 if not specified
orgId: 1
# <string> url
url: http://prometheus:9090
# <string> database password, if used
password:
# <string> database user, if used
user:
# <string> database name, if used
database:
# <bool> enable/disable basic auth
basicAuth: true
# <string> basic auth username
basicAuthUser: admin
# <string> basic auth password
basicAuthPassword: admin
# <bool> enable/disable with credentials headers
withCredentials:
# <bool> mark as default datasource. Max one per org
isDefault: true
# <map> fields that will be converted to json and stored in json_data
jsonData:
graphiteVersion: "1.1"
tlsAuth: false
tlsAuthWithCACert: false
# <string> json object of data that will be encrypted.
secureJsonData:
tlsCACert: "..."
tlsClientCert: "..."
tlsClientKey: "..."
version: 1
# <bool> allow users to edit datasources from the UI.
editable: true
\ 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>2.0.4.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>2.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
<scope>provided</scope>
</dependency>
<!-- Micormeter core dependecy -->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-core</artifactId>
<version>1.0.6</version>
</dependency>
<!-- Micrometer Prometheus registry -->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<version>1.0.6</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>projectlombok.org</id>
<url>http://projectlombok.org/mavenrepo</url>
</repository>
</repositories>
</project>
\ No newline at end of file
# my global config
global:
scrape_interval: 15s # By default, scrape targets every 15 seconds.
evaluation_interval: 15s # By default, scrape targets every 15 seconds.
# scrape_timeout is set to the global default (10s).
# Attach these labels to any time series or alerts when communicating with
# external systems (federation, remote storage, Alertmanager).
external_labels:
monitor: 'spring-boot-demo'
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 5s
static_configs:
- targets: ['localhost:9090']
- job_name: 'spring-boot'
scrape_interval: 5s
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['spring-demo:8080']
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.web.server.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());
}
}