...
 
Commits (11)
# Spring Demo Application
# 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
......@@ -9,33 +13,55 @@ This is an rather minimalistic Java Spring Boot Demo application illustrating ex
- Docker
- Docker-compose
- default port 8080
- port 3000 and 9090 for Grafana and Prometheus
## Build Docker Image
Clone or download thist repository and change into this folder.
Clone or download thist repository and cd into this folder.
- mvn package docker:build
```bash
mvn package dockerfile:build
```
## Run Docker Image
- docker-copose up [-d]
```bash
docker-copose up [-d]
```
## Access
## Accessing the demo application
- localhost:8080
- Actuator security is disabled: curl http://localhost:8080/env
- 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/`
## Showcased
## External configuration 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
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)
\ No newline at end of file
- [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
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"
version: "3.1"
services:
demo:
spring-demo:
container_name: spring-demo
image: rootknecht.net/spring-demo:latest
environment:
......@@ -11,7 +11,14 @@ services:
- 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
......@@ -12,7 +12,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<version>2.0.4.RELEASE</version>
<relativePath />
</parent>
<properties>
......@@ -73,7 +73,7 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>1.4.2.RELEASE</version>
<version>2.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
......@@ -81,6 +81,18 @@
<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>
......
# 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']
......@@ -20,7 +20,7 @@ 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.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;
......