Spring Boot and Micrometer With InlfuxDB Part 1: The Base Project
In this article, take a look at how to integrate Spring with Micrometer and InfluxDB.
Join the DZone community and get the full member experience.
Join For FreeTo those who follow this blog, it’s no wonder that I tend to use InfluxDB a lot. I like the fact that it is a real single purpose database (time series) with many features and also comes with enterprise support.
Spring is also one of the tools of my choice.
Thus in this blog, we shall integrate Spring with Micrometer and InfluxDB.
Our application will be a RET API for jobs.
Initially, it will fetch the Jobs from GitHub’s job API as shown here.
Let’s start with a pom
<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>DevJobsApi</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-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Let’s add the Job Repository for GitHub.
xxxxxxxxxx
package com.gkatzioura.jobs.repository;
import java.util.List;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Repository;
import org.springframework.web.reactive.function.client.WebClient;
import com.gkatzioura.jobs.model.Job;
import reactor.core.publisher.Mono;
public class GitHubJobRepository {
private WebClient githubClient;
public GitHubJobRepository() {
this.githubClient = WebClient.create("https://jobs.github.com");
}
public Mono<List<Job>> getJobsFromPage(int page) {
return githubClient.method(HttpMethod.GET)
.uri("/positions.json?page=" + page)
.retrieve()
.bodyToFlux(Job.class)
.collectList();
}
}
The Job model
xxxxxxxxxx
package com.gkatzioura.jobs.model;
import lombok.Data;
public class Job {
private String id;
private String type;
private String url;
private String createdAt;
private String company;
private String companyUrl;
private String location;
private String title;
private String description;
}
The controller
xxxxxxxxxx
package com.gkatzioura.jobs.controller;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.gkatzioura.jobs.model.Job;
import com.gkatzioura.jobs.repository.GitHubJobRepository;
import reactor.core.publisher.Mono;
"/jobs") (
public class JobsController {
private final GitHubJobRepository gitHubJobRepository;
public JobsController(GitHubJobRepository gitHubJobRepository) {
this.gitHubJobRepository = gitHubJobRepository;
}
"/github/{page}") (
private Mono<List<Job>> getEmployeeById( int page) {
return gitHubJobRepository.getJobsFromPage(page);
}
}
And last but not least the main application.
xxxxxxxxxx
package com.gkatzioura;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration;
exclude = { (
ReactiveSecurityAutoConfiguration.class
})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
On the next blog, we are going to integrate with InfluxDB and micrometer.
Published at DZone with permission of Emmanouil Gkatziouras, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments