对于那些关注此博客的人来说,难怪我倾向于大量使用InfluxDB。 我喜欢这样一个事实,它是一个真正的单一用途的数据库(时间序列),具有许多功能,并且还带有企业支持。
Spring也是我选择的工具之一。 因此,在本博客中,我们将把spring与micrometer和InfluxDB集成在一起。
我们的应用程序将是用于工作的rest api。 最初,它会从GitHub的工作API如图所示取乔布斯在这里 。
让我们从pom开始
<?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>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>
让我们为GitHub添加Job Repository。
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; @Repository 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(); } }
工作模式
package com.gkatzioura.jobs.model; import lombok.Data; @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; }
控制器
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; @RestController @RequestMapping ( "/jobs" ) public class JobsController { private final GitHubJobRepository gitHubJobRepository; public JobsController(GitHubJobRepository gitHubJobRepository) { this .gitHubJobRepository = gitHubJobRepository; } @GetMapping ( "/github/{page}" ) private Mono<List<Job>> getEmployeeById( @PathVariable int page) { return gitHubJobRepository.getJobsFromPage(page); } }
最后但并非最不重要的主要应用程序。
com.gkatzioura; 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; @SpringBootApplication @EnableAutoConfiguration (exclude = { ReactiveSecurityAutoConfiguration. class }) public class Application { public static void main(String[] args) { SpringApplication.run(Application. class , args); } }
在下一个博客中,我们将与InfluxDB和测微仪集成。
翻译自: https://www.javacodegeeks.com/2020/02/spring-boot-and-micrometer-with-inlfuxdb-part-1-the-base-project.html