You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Dropwizard框架,如何将Hystrix指标上报至New Relic?

实现Dropwizard应用Hystrix指标上报至New Relic的方案

1. 依赖配置

在Dropwizard项目的pom.xml(Maven)或build.gradle(Gradle)中添加核心依赖:

  • New Relic Java Agent:负责指标采集与上报
  • Hystrix核心及CodaHale集成依赖:用于对接Dropwizard的Metrics体系

Maven示例:

<!-- New Relic Java Agent -->
<dependency>
    <groupId>com.newrelic.agent.java</groupId>
    <artifactId>newrelic-agent</artifactId>
    <version>最新稳定版</version>
    <scope>runtime</scope>
</dependency>
<!-- Hystrix核心依赖 -->
<dependency>
    <groupId>com.netflix.hystrix</groupId>
    <artifactId>hystrix-core</artifactId>
    <version>1.5.18</version>
</dependency>
<!-- Hystrix与Dropwizard Metrics集成依赖 -->
<dependency>
    <groupId>com.netflix.hystrix</groupId>
    <artifactId>hystrix-contrib-codahale-metrics-publisher</artifactId>
    <version>1.5.18</version>
</dependency>

2. 配置New Relic Agent

在项目根目录创建newrelic.yml配置文件,关键配置如下:

license_key: "你的New Relic许可证密钥"
app_name: "你的Dropwizard应用名称"
class_transformer:
  com.newrelic.instrumentation.hystrix-1.5:
    enabled: true

注意:必须启用hystrix-1.5的instrumentation,这是New Relic自动采集Hystrix指标的核心开关。

3. 对接Hystrix与Dropwizard Metrics

在Dropwizard应用启动类的run方法中,将Hystrix指标接入Dropwizard的Metrics注册表:

import com.codahale.metrics.MetricRegistry;
import com.netflix.hystrix.strategy.HystrixPlugins;
import com.netflix.hystrix.contrib.codahalemetrics.HystrixCodaHaleMetricsPublisher;

@Override
public void run(YourAppConfig config, Environment env) {
    MetricRegistry metricRegistry = env.metrics();
    // 替换Hystrix默认的Metrics发布器为CodaHale实现
    HystrixCodaHaleMetricsPublisher publisher = new HystrixCodaHaleMetricsPublisher(metricRegistry);
    HystrixPlugins.getInstance().registerMetricsPublisher(publisher);
    
    // 其他应用初始化逻辑
}

4. 启动应用并验证指标

启动Dropwizard应用时,指定New Relic Agent参数:

java -javaagent:./newrelic.jar -jar your-app.jar server config.yml

启动完成后,登录New Relic控制台,进入你的应用页面,在指标模块搜索hystrix前缀的指标,比如hystrix.command.[CommandName].latency.mean(平均延迟)、hystrix.command.[CommandName].error.percentage(错误率),即可看到采集到的实时数据。

5. 自定义监控仪表盘

在New Relic中创建自定义仪表盘,按需添加可视化组件:

  • 折线图:展示指定Hystrix命令的延迟趋势
  • 数值卡片:展示全局或单命令的错误率
  • 分组图表:对比不同外部服务的Hystrix指标表现

内容的提问来源于stack exchange,提问作者shady2k

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 23:31:19