Skip to content

Commit 14e4278

Browse files
authored
Merge pull request aballaci#2 from aballaci/feature/custom_metrics
liveness and rediness with actuator and instances in metrics
2 parents 3a9c308 + 1ed5973 commit 14e4278

File tree

7 files changed

+146
-23
lines changed

7 files changed

+146
-23
lines changed

Dockerfile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ COPY settings.xml "/root/.m2/settings.xml"
88

99
COPY src src
1010

11-
RUN mvn dependency:go-offline -B
12-
1311
RUN mvn clean package -DskipTests
1412

1513
RUN mkdir -p target/dependency && (cd target/dependency; jar -xf ../*.jar)
@@ -26,4 +24,4 @@ COPY --from=MAVEN_BUILD ${DEPENDENCY}/BOOT-INF/lib /app/lib
2624
COPY --from=MAVEN_BUILD ${DEPENDENCY}/META-INF /app/META-INF
2725
COPY --from=MAVEN_BUILD ${DEPENDENCY}/BOOT-INF/classes /app
2826
EXPOSE 8080
29-
ENTRYPOINT ["java","-cp","app:app/lib/*","de.ballaci.jpa.SakilaApplication"]
27+
ENTRYPOINT ["java","-cp","app:app/lib/*","de.ballaci.SakilaApplication"]

pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@
6565
<groupId>io.micrometer</groupId>
6666
<artifactId>micrometer-registry-prometheus</artifactId>
6767
</dependency>
68+
<dependency>
69+
<groupId>io.projectreactor</groupId>
70+
<artifactId>reactor-core</artifactId>
71+
</dependency>
6872
</dependencies>
6973
<build>
7074
<plugins>

springboot-jpa.yaml

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ metadata:
1414
spec:
1515
type: LoadBalancer
1616
ports:
17-
- port: 8080
17+
- port: 8080
18+
name: http
1819
selector:
1920
app: springboot
2021
---
@@ -34,7 +35,23 @@ spec:
3435
spec:
3536
containers:
3637
- name: springboot-jpa
37-
image: aballaci/springboot-sakila-jpa:v3
38+
image: aballaci/springboot-sakila-jpa:v4
39+
readinessProbe:
40+
httpGet:
41+
path: /actuator/health
42+
port: 8080
43+
initialDelaySeconds: 10
44+
timeoutSeconds: 2
45+
periodSeconds: 3
46+
failureThreshold: 1
47+
livenessProbe:
48+
httpGet:
49+
path: /actuator/health
50+
port: 8080
51+
initialDelaySeconds: 20
52+
timeoutSeconds: 2
53+
periodSeconds: 8
54+
failureThreshold: 1
3855
resources:
3956
requests:
4057
cpu: 100m
@@ -46,6 +63,10 @@ spec:
4663
value: "postgres"
4764
- name: PGSQL_PORT
4865
value: "5432"
66+
- name: INSTANCE_NAME
67+
valueFrom:
68+
fieldRef:
69+
fieldPath: metadata.name
4970
- name: PGSQL_PASSWORD
5071
valueFrom:
5172
secretKeyRef:
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package de.ballaci;
2+
3+
import de.ballaci.jpa.domain.Film;
4+
import de.ballaci.services.FilmRentalService;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.SpringApplication;
7+
import org.springframework.boot.autoconfigure.SpringBootApplication;
8+
import org.springframework.boot.context.event.ApplicationReadyEvent;
9+
import org.springframework.context.event.EventListener;
10+
import org.springframework.core.env.Environment;
11+
import org.springframework.scheduling.annotation.EnableScheduling;
12+
import reactor.core.publisher.Flux;
13+
14+
import java.time.Duration;
15+
16+
/**
17+
* @author Armand.Ballaci
18+
*/
19+
20+
@SpringBootApplication
21+
@EnableScheduling
22+
public class SakilaApplication {
23+
24+
@Autowired
25+
private FilmRentalService rentalService;
26+
27+
public static void main(String[] args) {
28+
SpringApplication.run(SakilaApplication.class, args);
29+
}
30+
31+
@EventListener(ApplicationReadyEvent.class)
32+
public void bookFilms() {
33+
34+
Flux.interval(Duration.ofSeconds(2))
35+
.map(SakilaApplication::toFilm)
36+
.doOnEach(f -> rentalService.bookFilm(f.get()))
37+
.subscribe();
38+
}
39+
40+
private static Film toFilm(Long l) {
41+
return new Film();
42+
}
43+
}

src/main/java/de/ballaci/jpa/SakilaApplication.java

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package de.ballaci.services;
2+
3+
import de.ballaci.jpa.domain.Film;
4+
import io.micrometer.core.annotation.Timed;
5+
import io.micrometer.core.instrument.Counter;
6+
import io.micrometer.core.instrument.Gauge;
7+
import io.micrometer.core.instrument.MeterRegistry;
8+
import io.micrometer.core.instrument.Tag;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.beans.factory.annotation.Value;
11+
import org.springframework.context.annotation.PropertySource;
12+
import org.springframework.core.env.Environment;
13+
import org.springframework.scheduling.annotation.Scheduled;
14+
import org.springframework.stereotype.Component;
15+
16+
import java.util.ArrayList;
17+
import java.util.Collection;
18+
import java.util.List;
19+
import java.util.Random;
20+
21+
@Component
22+
@PropertySource("classpath:application.properties")
23+
public class FilmRentalService {
24+
25+
private MeterRegistry meterRegistry;
26+
private Counter requestedFilmCounter;
27+
28+
private List<Film> filmList;
29+
30+
private String instanceName;
31+
32+
private Environment env;
33+
34+
Random rand = new Random();
35+
36+
public FilmRentalService(MeterRegistry meterRegistry, Environment env) {
37+
this.meterRegistry = meterRegistry;
38+
this.env = env;
39+
this.filmList = new ArrayList<>();
40+
this.instanceName = env.getProperty("INSTANCE_NAME");
41+
initOrderCounters();
42+
Gauge.builder("film.bookings.in.queue", filmList, Collection::size)
43+
.tag("isntance", instanceName)
44+
.description("Number of unserved Films")
45+
.register(meterRegistry);
46+
}
47+
48+
49+
private void initOrderCounters() {
50+
requestedFilmCounter = Counter.builder("film.bookings")
51+
.tag("isntance", instanceName)
52+
.register(meterRegistry);
53+
}
54+
55+
public void bookFilm(Film film) {
56+
filmList.add(film);
57+
requestedFilmCounter.increment();
58+
}
59+
60+
@Scheduled(fixedRate = 3000)
61+
@Timed(description = "Do some work", longTask = true)
62+
public void serveFilm() throws InterruptedException {
63+
if(!filmList.isEmpty()) {
64+
filmList.remove(0);
65+
Thread.sleep((rand.nextInt(3 - 1) + 1) * 1000);
66+
}
67+
}
68+
}

src/main/resources/application.properties

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,10 @@ spring.datasource.password=${PGSQL_PASSWORD:s3hfUNDagb}
66
management.endpoint.metrics.enabled=true
77
management.endpoints.web.exposure.include=*
88
management.endpoint.prometheus.enabled=true
9-
management.metrics.export.prometheus.enabled=true
9+
management.metrics.export.prometheus.enabled=true
10+
11+
instance.name=${INSTANCE_NAME:SPRINGBOOT}
12+
13+
#server.port=8888
14+
15+
debug=true

0 commit comments

Comments
 (0)