dubbo 发送指标到Prometheus 实例
时间: 2025-01-26 10:09:26 浏览: 27
Dubbo 是一款高性能的 Java RPC 框架,而 Prometheus 是一个开源的监控系统和报警工具。将 Dubbo 的指标发送到 Prometheus 可以帮助我们实时监控服务的运行状态。以下是将 Dubbo 指标发送到 Prometheus 的步骤:
1. **引入依赖**:
首先,需要在项目的 `pom.xml` 文件中添加 Dubbo 和 Prometheus 的相关依赖。
```xml
<dependencies>
<!-- Dubbo 依赖 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.8</version>
</dependency>
<!-- Prometheus Java客户端 -->
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient</artifactId>
<version>0.12.0</version>
</dependency>
<!-- Prometheus HTTP服务器 -->
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_httpserver</artifactId>
<version>0.12.0</version>
</dependency>
</dependencies>
```
2. **配置 Dubbo**:
在 Dubbo 的配置文件中,配置 Prometheus 的相关参数。
```properties
dubbo.application.name=dubbo-prometheus-example
dubbo.registry.address=zookeeper://localhost:2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
```
3. **编写 Prometheus 指标收集器**:
创建一个类来收集 Dubbo 的指标。
```java
import io.prometheus.client.Counter;
import io.prometheus.client.exporter.HTTPServer;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Result;
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.Filter;
import org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper;
public class DubboPrometheusExporter {
static final Counter requestCounter = Counter.build()
.name("dubbo_requests_total")
.help("Total number of Dubbo requests.")
.labelNames("service", "method", "status")
.register();
public static void main(String[] args) throws Exception {
// 启动 Prometheus HTTP服务器
new HTTPServer(1234);
// 添加过滤器
ProtocolFilterWrapper protocolFilterWrapper = new ProtocolFilterWrapper();
protocolFilterWrapper.setFilter(new PrometheusFilter());
}
static class PrometheusFilter implements Filter {
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
String service = invoker.getInterface().getName();
String method = invocation.getMethodName();
try {
Result result = invoker.invoke(invocation);
requestCounter.labels(service, method, "success").inc();
return result;
} catch (RpcException e) {
requestCounter.labels(service, method, "failure").inc();
throw e;
}
}
}
}
```
4. **启动应用**:
启动 Dubbo 应用后,Prometheus 可以通过 `http://localhost:1234` 地址访问指标。
通过上述步骤,Dubbo 的指标就可以发送到 Prometheus 进行监控了。
阅读全文
相关推荐


















