本文是《Docker下Prometheus和Grafana三部曲》的终篇,前面的文章中,我们体验了快速搭建监控环境,也揭示了如何编排Docker容器来简化环境搭建过程,在监控系统中有个业务web服务,它上报的数据也能在监控系统中通过图形化界面展现出来,如下图所示:
本文通过实战来介绍业务应用如何将业务数据上报到prometheus,再通过Grafana的图形化界面展现出来;
接下来详细讲述应用的编码过程,如果您不想自己写代码,也可以在GitHub下载完整的应用源码,地址和链接信息如下表所示:
名称 | 链接 | 备注 |
---|---|---|
项目主页 | https://github.com/zq2599/blog_demos | 该项目在GitHub上的主页 |
git仓库地址(https) | https://github.com/zq2599/blog_demos.git | 该项目源码的仓库地址,https协议 |
git仓库地址(ssh) | [email protected]:zq2599/blog_demos.git | 该项目源码的仓库地址,ssh协议 |
这个git项目中有多个文件夹,本章源码在prometheusdemo这个文件夹下,如下图红框所示:
<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.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>1.5.19.RELEASEversion>
<relativePath/>
parent>
<groupId>com.bolingcavalrygroupId>
<artifactId>prometheusdemoartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>prometheusdemoname>
<description>Demo project for Spring Bootdescription>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<prometheus.version>0.0.17prometheus.version>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-securityartifactId>
dependency>
<dependency>
<groupId>io.micrometergroupId>
<artifactId>micrometer-spring-legacyartifactId>
<version>1.0.6version>
dependency>
<dependency>
<groupId>io.micrometergroupId>
<artifactId>micrometer-registry-prometheusartifactId>
<version>1.0.6version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
<plugin>
<groupId>com.spotifygroupId>
<artifactId>docker-maven-pluginartifactId>
<version>0.4.12version>
<configuration>
<imageName>bolingcavalry/${project.artifactId}imageName>
<imageTags>
<imageTag>${project.version}imageTag>
imageTags>
<baseImage>java:8u111-jdkbaseImage>
<entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]entryPoint>
<resources>
<resource>
<targetPath>/targetPath>
<directory>${project.build.directory}directory>
<include>${project.build.finalName}.jarinclude>
resource>
resources>
configuration>
plugin>
plugins>
build>
project>
management:
endpoints:
web:
exposure:
include: '*'
jmx:
exposure:
include: '*'
shutdown:
enabled: true
metrics:
distribution:
percentiles-histogram[http.server.requests]: true
security:
enabled: false
spring:
metrics:
servo:
enabled: false
security:
basic:
enabled: false
package com.bolingcavalry.prometheusdemo;
import io.micrometer.core.instrument.Counter;
import io.micrometer.prometheus.PrometheusMeterRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class PromConfig {
@Autowired
PrometheusMeterRegistry registry;
@Bean
public Counter getCounter() {
Counter counter = Counter.builder("my_sample_counter")
.tags("status", "success")
.description("A simple Counter to illustrate custom Counters in Spring Boot and Prometheus")
.register(registry);
return counter;
}
}
从上述代码可见,Counter.builder方法会实例化一个Counter类,这个类就代表了一个prometheus的监控项,可以用来上报名为"my_sample_counter"的监控数据,另外这个监控数据还会带上名为"status=success"的tag(稍后展示的实际数据可以看到);
4. 做一个响应web请求的controller,每收到一次web请求,都通过调用counter.increment()方法上报一次监控数据:
package com.bolingcavalry.prometheusdemo;
import io.micrometer.core.instrument.Counter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
@RestController
class GreetingController {
@Autowired
Counter counter;
@GetMapping("/greet")
String greet(@RequestParam(defaultValue = "World") String name) {
counter.increment();
return "Hello: " + name + " " + LocalDateTime.now();
}
}
package com.bolingcavalry.prometheusdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class PrometheusdemoApplication {
public static void main(String[] args) {
SpringApplication.run(PrometheusdemoApplication.class, args);
}
}
mvn clean package -U -DskipTests docker:build
如果控制台输出以下内容代表构建成功:
[INFO] --- spring-boot-maven-plugin:1.5.19.RELEASE:repackage (default) @ prometheusdemo ---
[INFO]
[INFO] --- docker-maven-plugin:0.4.12:build (default-cli) @ prometheusdemo ---
[INFO] Copying /root/work/temp/201903/09/prometheusdemo/target/prometheusdemo-0.0.1-SNAPSHOT.jar -> /root/work/temp/201903/09/prometheusdemo/target/docker/prometheusdemo-0.0.1-SNAPSHOT.jar
[INFO] Building image bolingcavalry/prometheusdemo
Step 1/3 : FROM java:8u111-jdk
---> d23bdf5b1b1b
Step 2/3 : ADD /prometheusdemo-0.0.1-SNAPSHOT.jar //
---> 2c5819935f94
Removing intermediate container 7d42332c1da5
Step 3/3 : ENTRYPOINT java -jar /prometheusdemo-0.0.1-SNAPSHOT.jar
---> Running in 52407ee52ae4
---> c2e2c7027455
Removing intermediate container 52407ee52ae4
Successfully built c2e2c7027455
[INFO] Built bolingcavalry/prometheusdemo
[INFO] Tagging bolingcavalry/prometheusdemo with 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 25.294 s
[INFO] Finished at: 2019-03-10T11:42:25+08:00
[INFO] ------------------------------------------------------------------------
至此,自定义监控项从开发到配置我们都完成了实战,也到了《Docker下Prometheus和Grafana三部曲》系列和您说再见的时候了,在您搭建监控系统的时候,希望此系列文章能给您带来一些参考。