乐趣区

prometheus 集成dubbo

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,即可看到监控项

退出移动版