转载

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

原文  https://segmentfault.com/a/1190000018718740
正文到此结束
Loading...