在 SpringBoot 1.x 時期 網路上 Google 會有各式各樣整合套件跟方法, 到了 2.0 官方就直接幫你整併進 actuator , 你唯一要做的事情就只有增加 Gradle 的配置就好了
SpringBoot部分
build.gradle
buildscript {
ext {
springBootVersion = '2.0.1.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.boot:spring-boot-starter-webflux')
compileOnly('org.projectlombok:lombok')
runtimeOnly('io.micrometer:micrometer-registry-prometheus')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('io.projectreactor:reactor-test')
}
這邊注意要加上 runtimeOnly('io.micrometer:micrometer-registry-prometheus') 就可以了
配置檔
management:
endpoint:
prometheus:
enabled: true
endpoints:
web:
exposure:
include: health,info,prometheus
management.endpoint.prometheus.enabled 預設是 true 啦...如果沒資料記得來看一下是不是不小心關了
management.endpoints.web.exposure.include 因為預設對外暴露只有 health,info , 記得要再加上 prometheus
啟動 SpringBoot
2018-04-10 14:28:57.212 INFO 2197 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication on zhushanglide-MacBook-Air.local with PID 2197 (/Users/sam/IdeaProjects/test-prometheus/out/production/classes started by sam in /Users/sam/IdeaProjects/test-prometheus)
2018-04-10 14:28:57.223 INFO 2197 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2018-04-10 14:28:57.621 INFO 2197 --- [ main] onfigReactiveWebServerApplicationContext : Refreshing org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext@795cd85e: startup date [Tue Apr 10 14:28:57 CST 2018]; root of context hierarchy
2018-04-10 14:29:03.331 INFO 2197 --- [ main] o.s.w.r.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.reactive.resource.ResourceWebHandler]
2018-04-10 14:29:03.331 INFO 2197 --- [ main] o.s.w.r.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.reactive.resource.ResourceWebHandler]
2018-04-10 14:29:04.026 INFO 2197 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 3 endpoint(s) beneath base path '/actuator'
2018-04-10 14:29:04.044 INFO 2197 --- [ main] .b.a.e.w.r.WebFluxEndpointHandlerMapping : Mapped "{[/actuator/health],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public org.reactivestreams.Publisher<org.springframework.http.ResponseEntity<java.lang.Object>> org.springframework.boot.actuate.endpoint.web.reactive.AbstractWebFluxEndpointHandlerMapping$ReadOperationHandler.handle(org.springframework.web.server.ServerWebExchange)
2018-04-10 14:29:04.045 INFO 2197 --- [ main] .b.a.e.w.r.WebFluxEndpointHandlerMapping : Mapped "{[/actuator/info],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public org.reactivestreams.Publisher<org.springframework.http.ResponseEntity<java.lang.Object>> org.springframework.boot.actuate.endpoint.web.reactive.AbstractWebFluxEndpointHandlerMapping$ReadOperationHandler.handle(org.springframework.web.server.ServerWebExchange)
2018-04-10 14:29:04.046 INFO 2197 --- [ main] .b.a.e.w.r.WebFluxEndpointHandlerMapping : Mapped "{[/actuator/prometheus],methods=[GET],produces=[text/plain;version=0.0.4;charset=utf-8]}" onto public org.reactivestreams.Publisher<org.springframework.http.ResponseEntity<java.lang.Object>> org.springframework.boot.actuate.endpoint.web.reactive.AbstractWebFluxEndpointHandlerMapping$ReadOperationHandler.handle(org.springframework.web.server.ServerWebExchange)
2018-04-10 14:29:04.047 INFO 2197 --- [ main] .b.a.e.w.r.WebFluxEndpointHandlerMapping : Mapped "{[/actuator],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto protected java.util.Map<java.lang.String, java.util.Map<java.lang.String, org.springframework.boot.actuate.endpoint.web.Link>> org.springframework.boot.actuate.endpoint.web.reactive.WebFluxEndpointHandlerMapping.links(org.springframework.web.server.ServerWebExchange)
2018-04-10 14:29:04.148 INFO 2197 --- [ main] o.s.w.r.r.m.a.ControllerMethodResolver : Looking for @ControllerAdvice: org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext@795cd85e: startup date [Tue Apr 10 14:28:57 CST 2018]; root of context hierarchy
2018-04-10 14:29:04.678 INFO 2197 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-04-10 14:29:04.802 INFO 2197 --- [ctor-http-nio-1] r.ipc.netty.tcp.BlockingNettyContext : Started HttpServer on /0:0:0:0:0:0:0:0:8080
2018-04-10 14:29:04.803 INFO 2197 --- [ main] o.s.b.web.embedded.netty.NettyWebServer : Netty started on port(s): 8080
2018-04-10 14:29:04.810 INFO 2197 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 9.803 seconds (JVM running for 12.868)
啟動 log 中注意看一下 Mapped 應該要有 Mapped "{[/actuator/prometheus],methods=[GET],produces=[text/plain;version=0.0.4;charset=utf-8]}"....大致上就沒問題
實際抓一下資料
curl -X GET \
http://192.168.31.240:8080/actuator/prometheus
資料大概長這樣
# HELP jvm_buffer_count An estimate of the number of buffers in the pool
# TYPE jvm_buffer_count gauge
jvm_buffer_count{id="direct",} 1.0
jvm_buffer_count{id="mapped",} 0.0
# HELP http_server_requests_seconds
# TYPE http_server_requests_seconds summary
.....
Prometheus
練習就使用 Docker
首先你必須準備個 prometheus.yml 配置檔
範例你可以參考這個來改 https://github.com/prometheus/prometheus/blob/master/documentation/examples/prometheus.yml
這是我的範例檔
prometheus.yml
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'spring'
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['192.168.31.240:8080']
job_name: 'spring' 部分是參考 Spring 官方範例 https://docs.spring.io/spring-boot/docs/2.0.1.RELEASE/reference/htmlsingle/#production-ready-metrics-export-prometheus
啟動 Docker
docker run -p 9090:9090 -v /Users/sam/temp/prometheus.yml:/etc/prometheus/prometheus.yml \
prom/prometheus
或是 可以持久化紀錄
sudo chown -R 65534 /prometheus/data
docker run -d -p 9090:9090 \
-v /prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \
-v /prometheus/data:/prometheus \
prom/prometheus --web.enable-lifecycle --config.file=/etc/prometheus/prometheus.yml
--web.enable-lifecycle
是當設定檔改變時, 可以進行 Reload
curl -s -XPOST localhost:9090/-/reload
啟動的訊息
level=info ts=2018-04-10T06:07:38.902551377Z caller=main.go:220 msg="Starting Prometheus" version="(version=2.2.1, branch=HEAD, revision=bc6058c81272a8d938c05e75607371284236aadc)"
level=info ts=2018-04-10T06:07:38.902640564Z caller=main.go:221 build_context="(go=go1.10, user=root@149e5b3f0829, date=20180314-14:15:45)"
level=info ts=2018-04-10T06:07:38.902675177Z caller=main.go:222 host_details="(Linux 4.9.87-linuxkit-aufs #1 SMP Wed Mar 14 15:12:16 UTC 2018 x86_64 d2213421ad8e (none))"
level=info ts=2018-04-10T06:07:38.902768624Z caller=main.go:223 fd_limits="(soft=1048576, hard=1048576)"
level=info ts=2018-04-10T06:07:38.910036457Z caller=main.go:504 msg="Starting TSDB ..."
level=info ts=2018-04-10T06:07:38.910262914Z caller=web.go:382 component=web msg="Start listening for connections" address=0.0.0.0:9090
level=info ts=2018-04-10T06:07:38.919798151Z caller=main.go:514 msg="TSDB started"
level=info ts=2018-04-10T06:07:38.919897524Z caller=main.go:588 msg="Loading configuration file" filename=/etc/prometheus/prometheus.yml
level=info ts=2018-04-10T06:07:38.927176265Z caller=main.go:491 msg="Server is ready to receive web requests."
看到這句 Server is ready to receive web requests. 就可以開網頁來看看啦
http://localhost:9090/graph
大概就這樣
其他部分等要弄到監控再來試看看吧
其他參考
https://prometheus.io/docs/prometheus/latest/installation/