Prometheus + Grafana 監(jiān)控 Silience4j
Silience4j指標(biāo)暴露
引入依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-spring-boot2</artifactId>
<version>0.17.0</version>
</dependency>
暴露接口
Silience4j其實(shí)是有默認(rèn)的Endpoint的焚廊,但是為了接口靈活甩苛,可以自己提供api讓Prometheus進(jìn)行拉取云石,一般使用默認(rèn)的"/metrics"箱残,這是Prometheus默認(rèn)的路徑狞膘,保持統(tǒng)一的暴露端口可以在結(jié)合服務(wù)發(fā)現(xiàn)時(shí)比較簡單揩懒。
@RestController
@RequestMapping
public class PrometheusController {
@Autowired
private CollectorRegistry collectorRegistry;
//這里produces必須這樣設(shè)置才能被Prometheus解析
@GetMapping(value = "/metrics", produces = "text/plain; version=0.0.4; charset=utf-8")
public String data() {
try {
Writer writer = new StringWriter();
TextFormat.write004(writer, collectorRegistry.metricFamilySamples());
return writer.toString();
}
catch (IOException ex) {
// This actually never happens since StringWriter::write() doesn't throw any
// IOException
throw new RuntimeException("Writing metrics failed", ex);
}
}
}
使用collectorRegistry.metricFamilySamples()方法就可以拿到metrics了,這些指標(biāo)也包括jvm等一系列的mertics客冈。啟動之后訪問http://localhost:8080/metrics就可以看到暴露出來的metrics了旭从。
Prometheus設(shè)置
默認(rèn)prometheus已經(jīng)安裝好,在linux下场仲,我的prometheus安裝在/usr/loacal/prometheus/目錄下:
cd /usr/local/prometheus/
vim prometheus.yml
關(guān)于具體的配置詳見prometheus官網(wǎng)和悦,這里主要配置以下項(xiàng):
scrape_configs:
- job_name: "resilience4j_exporter"
metrics_path: '/metrics' #這里可以設(shè)置暴露的api
static_configs:
- targets: ['127.0.0.1:8080']
labels:
application: resilience4j
后臺啟動prometheus:
./prometheus --config.file=prometheus.yml --web.enable-lifecycle &
--web.enable-lifecycle可以允許更改配置之后不用重啟prometheus,只需要執(zhí)行一遍:
curl -X POST http://localhost:9090/-/reload
訪問http://localhost:9090/targets:
可見prometheus已經(jīng)拿到metrics了渠缕。
以上是靜態(tài)的配置方法鸽素,如果服務(wù)很多的話,這樣一個(gè)一個(gè)配置必然很麻煩亦鳞,接下來介紹利用服務(wù)發(fā)現(xiàn)consul動態(tài)進(jìn)行配置馍忽,consul的安裝參見consul官網(wǎng)。我的consul路徑為/usr/local/consul/:
cd /usr/local/consul/
mkdir consul.d
cd consul.d
為簡單起見燕差,忽略服務(wù)發(fā)現(xiàn)的過程遭笋,我們在consul.d文件夾下手寫一下配置文件,表明有兩個(gè)服務(wù)注冊上consul徒探,一個(gè)resilience4j.json和web.json:
resilience4j.json
{
"service": {
"name": "resilience4j",
"address": "172.17.205.72",
"port": 8080
}
}
web.json
{
"service":{
"name":"web",
"address": "127.0.0.1"
"port":80
}
}
指定配置所在文件夾啟動consul:
cd /usr/local/consul/
consul agent -dev -client 0.0.0.0 -ui -config-dir=/web/cdo/consul/consul.d
服務(wù)已經(jīng)注冊上來了瓦呼,接下來在prometheus.yml配置下consule:
scrape_configs:
-job_name: "try"
consul_sd_configs:
- server: ['127.0.0.1:8500']
services: [] #需要拉取指標(biāo)的服務(wù)列表(填服務(wù)名),如果為空則為所有服務(wù)
relabel_configs:
- action: replace
source_labels: ['__meta_consul_service'] #源目標(biāo)label
regex: (.*) #正則测暗,匹配'__meta_consul_service'的值
replacement: $1 #選取匹配到的第一個(gè)分組央串,即resilience4j
target_label: application #替換目標(biāo)label
這里relabel了一下標(biāo)簽,讓服務(wù)中的一些信息可以作為prometheus的標(biāo)簽碗啄,這樣可以用來區(qū)分服務(wù)质和,關(guān)于relabel可以看:https://blog.rj-bai.com/post/158.html,講解的很詳細(xì)稚字。
接下來看一下prometheus:
在consul中注冊的服務(wù)都被prometheus拉取到了饲宿。
Grafana可視化
首先設(shè)置數(shù)據(jù)源,點(diǎn)擊Data Sources:
添加新數(shù)據(jù)源:
Type選擇Prometheus胆描,填寫URL褒傅,名字可以隨意
然后下載resilience4j用于grafna的json文件:grafana_dashboard.json:
"annotations": {
"list": [
{
"builtIn": 1,
"datasource": "-- Grafana --", #這里改成數(shù)據(jù)源的名稱
"enable": true,
"hide": true,
"iconColor": "rgba(0, 211, 255, 1)",
"name": "Annotations & Alerts",
"type": "dashboard"
}
]
}
點(diǎn)擊import導(dǎo)入;
點(diǎn)擊Uplosd .json File
就可以查看可視化的監(jiān)控界面了: