赞
踩
通过sentinel统计 请求数,将数据暴露给 promtheus端点, 然后通过 prometheus gateway 推送给 prometheus
<properties> <java.version>1.8</java.version> <alibaba.version>2021.0.1.0</alibaba.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> <scope>runtime</scope> </dependency> <!-- https://mvnrepository.com/artifact/io.prometheus/simpleclient_pushgateway --> <dependency> <groupId>io.prometheus</groupId> <artifactId>simpleclient_pushgateway</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${alibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
spring.application.name=boot-senpro management.metrics.tags.application=${spring.application.name} management.endpoints.web.exposure.include=* management.endpoint.shutdown.enabled=true management.endpoint.metrics.enabled=true management.endpoint.prometheus.enabled=true management.metrics.export.prometheus.enabled=true # pushgateway 地址 management.metrics.export.prometheus.pushgateway.base-url=192.168.xx.xx:9091 management.metrics.export.prometheus.pushgateway.push-rate=15s management.metrics.export.prometheus.pushgateway.job=${spring.application.name} management.metrics.export.prometheus.pushgateway.enabled=true server.port=9997
sentinel 提供 了 MetricExtension 接口,通过 SPI 方式引入,PrometheusExtension ,从 Sentinel
项目拷贝,改造了一下 ,通过 boot actuator prometheus 暴露:
public class PrometheusExtension implements MetricExtension { private PrometheusSentinelRegistry prometheusSenRegistry; private PrometheusSentinelRegistry getRegistry() { if (prometheusSenRegistry != null) { return prometheusSenRegistry; } this.prometheusSenRegistry = SpringUtil.getBean(PrometheusSentinelRegistry.class); return prometheusSenRegistry; } @Override public void addPass(String resource, int n, Object... args) { getRegistry().getPassRequests().labels(resource).inc(n); } @Override public void addBlock(String resource, int n, String origin, BlockException ex, Object... args) { getRegistry().getBlockRequests().labels(resource, ex.getClass().getSimpleName(), ex.getRuleLimitApp(), origin).inc(n); } @Override public void addSuccess(String resource, int n, Object... args) { getRegistry().getSuccessRequests().labels(resource).inc(n); } @Override public void addException(String resource, int n, Throwable throwable) { getRegistry().getExceptionRequests().labels(resource).inc(n); } @Override public void addRt(String resource, long rt, Object... args) { // convert millisecond to second getRegistry().getRtHist().labels(resource).observe(((double) rt) / 1000); } @Override public void increaseThreadNum(String resource, Object... args) { getRegistry().getCurrentThreads().labels(resource).inc(); } @Override public void decreaseThreadNum(String resource, Object... args) { getRegistry().getCurrentThreads().labels(resource).dec(); } } public class SpringUtil { private static ApplicationContext act; public static void setApplicationContext(ApplicationContext applicationContext) { SpringUtil.act = applicationContext; } public static <T> T getBean(Class<T> bean) { return act.getBean(bean); } }
统计:
public class PrometheusSentinelRegistry { private Counter passRequests; private Counter blockRequests; private Counter successRequests; private Counter exceptionRequests; private Histogram rtHist; private Gauge currentThreads; public PrometheusSentinelRegistry(CollectorRegistry registry) { passRequests = Counter.build() .name("sentinel_pass_requests_total") .help("total pass requests.") .labelNames("resource") .register(registry); blockRequests = Counter.build() .name("sentinel_block_requests_total") .help("total block requests.") .labelNames("resource", "type", "ruleLimitApp", "limitApp") .register(registry); successRequests = Counter.build() .name("sentinel_success_requests_total") .help("total success requests.") .labelNames("resource") .register(registry); exceptionRequests = Counter.build() .name("sentinel_exception_requests_total") .help("total exception requests.") .labelNames("resource") .register(registry); currentThreads = Gauge.build() .name("sentinel_current_threads") .help("current thread count.") .labelNames("resource") .register(registry); rtHist = Histogram.build() .name("sentinel_requests_latency_seconds") .help("request latency in seconds.") .labelNames("resource") .register(registry); } public Counter getPassRequests() { return passRequests; } public Counter getBlockRequests() { return blockRequests; } public Counter getSuccessRequests() { return successRequests; } public Counter getExceptionRequests() { return exceptionRequests; } public Histogram getRtHist() { return rtHist; } public Gauge getCurrentThreads() { return currentThreads; } }
在启动类中注入:
@SpringBootApplication
public class BootSenproApplication {
public static void main(String[] args) {
ConfigurableApplicationContext act = SpringApplication.run(BootSenproApplication.class, args);
SpringUtil.setApplicationContext(act);
}
@Bean
public PrometheusSentinelRegistry prometheusSenRegistry(CollectorRegistry registry) {
return new PrometheusSentinelRegistry(registry);
}
}
访问 boot 应用 prometheus 端点:
sentinel_success_requests_total{resource="/error",} 4.0
sentinel_success_requests_total{resource="/index",} 5.0
sentinel_success_requests_total{resource="/**",} 1.0
...
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。