prometheus 集成dubbo

23次阅读

共计 1972 个字符,预计需要花费 5 分钟才能阅读完成。

dubbo 自身的监控使用了 dubbo 的拦截器,这里我们也使用 dubbo 的拦截器来添加 prometheus 监控首先需要 dubbo 项目提供 http 的接口, 为 dubbo 项目添加 web 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
加入 micrometer prometheus
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-core</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
实现 dubbo 的 filter 接口, 添加 @Activate(group = Constants.PROVIDER) 注解,声明拦截所有服务提供者
@Activate(group = Constants.PROVIDER)
public class PrometheusFilter implements Filter {

private Logger logger = LoggerFactory.getLogger(PrometheusFilter.class);

@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
logger.info(“—————-prometheus filter—————“);
RequestTimeCollector requestTimeCollector = (RequestTimeCollector) ServiceBean.getSpringContext().
getBean(“dubboRequestTimeCollector”);
RpcContext context = RpcContext.getContext();
boolean isProvider = context.isProviderSide();
String serviceName = invoker.getInterface().getName();
String methodName = RpcUtils.getMethodName(invocation);
long start = System.currentTimeMillis();
try {
// proceed invocation chain
Result result = invoker.invoke(invocation);
long duration = System.currentTimeMillis() – start;
String status = “success”;
if(result.getException()!=null){
status = result.getException().getClass().getSimpleName();
}
requestTimeCollector.setValue(duration,serviceName,methodName,status);
return result;
} catch (RpcException e) {
long duration = System.currentTimeMillis() – start;
String result = “error”;
if (e.isTimeout()) {
result = “timeoutError”;
}
if (e.isBiz()) {
result = “bisError”;
}
if (e.isNetwork()) {
result = “networkError”;
}
if (e.isSerialization()) {
result = “serializationError”;
}
requestTimeCollector.setValue(duration,serviceName,methodName,result);
throw e;
}
}
}
配置拦截器扩展
在 resourceMETA-INFdubbo 文件夹下创建 com.alibaba.dubbo.rpc.Filter 文本文件添加 prometheus=com.rcplatform.livechat.dubbo.filter.PrometheusFilter 文本启动项目访问 /actuator/prometheus,即可看到监控项

正文完
 0