目标
学习Soul网关monitor插件的使用。
Soul Admin
首先在Soul控制台 -> 系统管理 -> 插件管理,将"monitor"设置为开启状态
配置参数:
- metricsName: 监控指标名称
- host: 为暴露给 prometheus服务来拉取的地址,不添默认是Soul网关的地址
- port:为暴露给 prometheus服务来拉取的端口
- async:是否异步pull监控指标数据
Soul-web网关
首先,在pom.xml加上如下配置:
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-plugin-monitor</artifactId>
<version>${project.version}</version>
</dependency>
启动Soul网关成功后,我看一下prometheus埋点数据。访问http://localhost:9190,如下图:
request_total请求总数,在SoulWebHandler类的handle方法中加入该埋点统计,代码如下:
SoulWebHandler.java
public final class SoulWebHandler implements WebHandler {
public Mono<Void> handle(@NonNull final ServerWebExchange exchange) {
// 记录请求数并写入到prometheus
MetricsTrackerFacade.getInstance().counterInc(MetricsLabelEnum.REQUEST_TOTAL.getName());
Optional<HistogramMetricsTrackerDelegate> startTimer = MetricsTrackerFacade.getInstance().histogramStartTimer(MetricsLabelEnum.REQUEST_LATENCY.getName());
// 创建DefaultSoulPluginChain 并执行execute
return new DefaultSoulPluginChain(plugins).execute(exchange).subscribeOn(scheduler)
.doOnSuccess(t -> startTimer.ifPresent(time -> MetricsTrackerFacade.getInstance().histogramObserveDuration(time)));
}
}
对某个http请求做埋点
首先在Soul-Admin控制台进行设置,如下图:
-
先添加“选择器”
-
添加规则
配置好“选择器”和“规则”后,我们请求http://127.0.0.1:9195/http/order/findById?id=1,我们再看看prometheus:
这时,埋点数据已显示。
prometheus收集埋点数据
首先需要安装prometheus server。
安装之前需要golang运行环境,如已安装则跳过。golang安装说明
prometheus server:
安装说明:prometheus地址
通过源码安装,:
$ git clone https://github.com/prometheus/prometheus.git
$ cd prometheus
$ make build
$ ./prometheus --config.file=soul_config.yml
soul_conf.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:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['localhost:9090']
- job_name: 'soul'
# metrics_path: '/'
static_configs:
- targets: ['localhost:9190']
prometheus server 启动后,我们看一下prometheus监控,地址:http://localhost:9090
点击“Status" -> "Targets",如下:
up,证明收集metrics成功。
点击”Graph“,可以查看具体metrics的数据。
刚刚我们请求过http://127.0.0.1:9195/http/order/findById?id=1地址,在prometheus server可以看到数据,如下:
继续,我们可以压测一下网关。
wrk http://127.0.0.1:9195/http/order/findById\?id\=1
Running 10s test @ http://127.0.0.1:9195/http/order/findById?id=1
2 threads and 10 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 1.18ms 1.89ms 28.17ms 96.48%
Req/Sec 5.51k 0.93k 6.89k 77.00%
109773 requests in 10.01s, 3.98MB read
Requests/sec: 10970.61
Transfer/sec: 407.11KB
prometheus控制台
总结
今天主要是学习Soul网关如何使用monitor插件的。明天会对monitor插件进行源码分析。
有疑问加站长微信联系(非本文作者)