《Apache Dubbo微服务开发从入门到精通》——可视化监测服务状态——二、 微服务集群监控(4) https://developer.aliyun.com/article/1224283
指标推送
指标推送只有用户在设置了配置且配置protocol参数后才开启,若只开启指标聚合,则默认不推送指标。
a) Promehteus Pull ServiceDiscovery
使用dubbo-admin等类似的中间层,启动时根据配置将本机IP、Port、MetricsURL推送地址信息至dubbo-admin(或任意中间层)的方式,暴露HTTP ServiceDiscovery供prometheus读取,配置方式如,其中在pull模式下address为可选参数,若不填则需用户手动在Prometheus配置文件中配置地址。
b) Prometheus Push Pushgateway
用户直接在Dubbo配置文件中配置Prometheus Pushgateway的地址即可,如,其中interval代表推送间隔。
private void schedulePushJob() { boolean pushEnabled = url.getParameter(PROMETHEUS_PUSHGATEWAY_ENABLED_KEY, false); if (pushEnabled) { String baseUrl = url.getParameter(PROMETHEUS_PUSHGATEWAY_BASE_URL_KEY); String job = url.getParameter(PROMETHEUS_PUSHGATEWAY_JOB_KEY, PROMETHEUS_DEFAULT_JOB_NAME); int pushInterval = url.getParameter(PROMETHEUS_PUSHGATEWAY_PUSH_INTERVAL_KEY, PROMETHEUS_DEFAULT_PUSH_INTERVAL); String username = url.getParameter(PROMETHEUS_PUSHGATEWAY_USERNAME_KEY); String password = url.getParameter(PROMETHEUS_PUSHGATEWAY_PASSWORD_KEY); NamedThreadFactory threadFactory = new NamedThreadFactory("prometheus-push-job", true); pushJobExecutor = Executors.newScheduledThreadPool(1, threadFactory); PushGateway pushGateway = new PushGateway(baseUrl); if (!StringUtils.isBlank(username)) { pushGateway.setConnectionFactory(new BasicAuthHttpConnectionFactory(username, password)); } pushJobExecutor.scheduleWithFixedDelay(() -> push(pushGateway, job), pushInterval, pushInterval, TimeUnit.SECONDS); } } protected void push(PushGateway pushGateway, String job) { try { pushGateway.pushAdd(prometheusRegistry.getPrometheusRegistry(), job); } catch (IOException e) { logger.error("Error occurred when pushing metrics to prometheus: ", e); } }
可视化展示
目前推荐使用Prometheus 来进行服务监控,Grafana来展示指标数据。可以通过案例来快速入门Dubbo可视化监控。
《Apache Dubbo微服务开发从入门到精通》——可视化监测服务状态——二、 微服务集群监控(6) https://developer.aliyun.com/article/1224280