Spring Boot and Micrometer with InlfuxDB Part 3: Servlets and JDBC
In the previous blog we setup a reactive application with micrometer backed with an InfluxDB.
On this tutorial we shall use our old school blocking Servlet Based Spring Stack with JDBC. My database of choice would be postgresql. I shall use the same scripts of a previous blog post.

Thus we shall have the script that initializes the database
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #!/bin/bashset -epsql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL create schema spring_data_jpa_example; create table spring_data_jpa_example.employee( id SERIAL PRIMARY KEY, firstname TEXT NOT NULL, lastname TEXT NOT NULL, email TEXT not null, age INT NOT NULL, salary real, unique(email) ); insert into spring_data_jpa_example.employee (firstname,lastname,email,age,salary) values ('John','Doe 1','john1@doe.com',18,1234.23); insert into spring_data_jpa_example.employee (firstname,lastname,email,age,salary) values ('John','Doe 2','john2@doe.com',19,2234.23); insert into spring_data_jpa_example.employee (firstname,lastname,email,age,salary) values ('John','Doe 3','john3@doe.com',20,3234.23); insert into spring_data_jpa_example.employee (firstname,lastname,email,age,salary) values ('John','Doe 4','john4@doe.com',21,4234.23); insert into spring_data_jpa_example.employee (firstname,lastname,email,age,salary) values ('John','Doe 5','john5@doe.com',22,5234.23);EOSQL |
The we shall have a docker compose file that contains InfluxDB, Postgres and Grafana.
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | version: '3.5'services: influxdb: image: influxdb restart: always ports: - 8086:8086 grafana: image: grafana/grafana restart: always ports: - 3000:3000 postgres: image: postgres restart: always environment: POSTGRES_USER: db-user POSTGRES_PASSWORD: your-password POSTGRES_DB: postgres ports: - 5432:5432 volumes: - $PWD/init-db-script.sh:/docker-entrypoint-initdb.d/init-db-script.sh |
Now it’s time to build our spring application starting with our maven dependencies.
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | <?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.4.RELEASE</version> </parent> <groupId>com.gkatzioura</groupId> <artifactId>EmployeeApi</artifactId> <version>1.0-SNAPSHOT</version> <build> <defaultGoal>spring-boot:run</defaultGoal> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>8</source> <target>8</target> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </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-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.2.8</version> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-core</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-influx</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> <scope>provided</scope> </dependency> </dependencies></project> |
Since this is a JDBC backed dependency we shall create the entities and the repositories.
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | package com.gkatzioura.employee.model;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.Table;import lombok.Data;@Data@Entity@Table(name = "employee", schema="spring_data_jpa_example")public class Employee { @Id @Column(name = "id") @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "firstname") private String firstName; @Column(name = "lastname") private String lastname; @Column(name = "email") private String email; @Column(name = "age") private Integer age; @Column(name = "salary") private Integer salary;} |
Then let’s add the Repository
1 2 3 4 5 6 7 | package com.gkatzioura.employee.repository;import com.gkatzioura.employee.model.Employee;import org.springframework.data.jpa.repository.JpaRepository;public interface EmployeeRepository extends JpaRepository<Employee,Long> {} |
And the controller
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | package com.gkatzioura.employee.controller;import java.util.List;import com.gkatzioura.employee.model.Employee;import com.gkatzioura.employee.repository.EmployeeRepository;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class EmployeeController { private final EmployeeRepository employeeRepository; public EmployeeController(EmployeeRepository employeeRepository) { this.employeeRepository = employeeRepository; } @RequestMapping("/employee") public List<Employee> getEmployees() { return employeeRepository.findAll(); }} |
Last but not least the Application class
01 02 03 04 05 06 07 08 09 10 11 12 13 | package com.gkatzioura.employee;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }} |
As well the configuration
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 | spring: datasource: platform: postgres driverClassName: org.postgresql.Driver username: db-user password: your-password url: jdbc:postgresql://127.0.0.1:5432/postgresmanagement: metrics: export: influx: enabled: true db: employeeapi uri: http://127.0.0.1:8086 endpoints: web: expose: "*" |
Let’s try it
1 | curl http://localhost:8080/employee |
After some requests we can find the entries persisted.
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | docker exec -it influxdb-local influx> SHOW DATABASES;name: databasesname----_internalemployeeapi> use employeeapiUsing database employeeapi> SHOW MEASUREMENTSname: measurementsname----hikaricp_connectionshikaricp_connections_acquirehikaricp_connections_activehikaricp_connections_creationhikaricp_connections_idlehikaricp_connections_maxhikaricp_connections_minhikaricp_connections_pendinghikaricp_connections_timeouthikaricp_connections_usagehttp_server_requestsjdbc_connections_activejdbc_connections_idlejdbc_connections_maxjdbc_connections_minjvm_buffer_countjvm_buffer_memory_usedjvm_buffer_total_capacityjvm_classes_loadedjvm_classes_unloadedjvm_gc_live_data_sizejvm_gc_max_data_sizejvm_gc_memory_allocatedjvm_gc_memory_promotedjvm_gc_pausejvm_memory_committedjvm_memory_maxjvm_memory_usedjvm_threads_daemonjvm_threads_livejvm_threads_peakjvm_threads_stateslogback_eventsprocess_cpu_usageprocess_files_maxprocess_files_openprocess_start_timeprocess_uptimesystem_cpu_countsystem_cpu_usagesystem_load_average_1mtomcat_sessions_active_currenttomcat_sessions_active_maxtomcat_sessions_alive_maxtomcat_sessions_createdtomcat_sessions_expiredtomcat_sessions_rejected |
As you can see the metrics are a bit different from the previous example. We have jdbc connection metrics tomcat metrics and all metrics relevant to our application. You can find the sourcecode on github.
Published on Java Code Geeks with permission by Emmanouil Gkatziouras, partner at our JCG program. See the original article here: Spring Boot and Micrometer with InlfuxDB Part 3: Servlets and JDBC Opinions expressed by Java Code Geeks contributors are their own. |




