我是靠谱客的博主 积极毛豆,最近开发中收集的这篇文章主要介绍Hystrix + influxdb + Grafana 监控的搭建,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Hystrix 的使用

  1. 引入maven依赖
      <!--hystrix 依赖开始-->
        <dependency>
            <groupId>com.netflix.hystrix</groupId>
            <artifactId>hystrix-core</artifactId>
            <version>1.5.18</version>
        </dependency>
        <dependency>
            <groupId>com.netflix.hystrix</groupId>
            <artifactId>hystrix-metrics-event-stream</artifactId>
            <version>1.5.18</version>
        </dependency>
        <dependency>
            <groupId>com.netflix.hystrix</groupId>
            <artifactId>hystrix-javanica</artifactId>
            <version>1.5.18</version>
        </dependency>
        <!--hystrix 依赖结束-->
  1. 使用Hystrix 监控接口数据
   @HystrixCommand(commandKey = "nlpHys",threadPoolKey = "nlpHysPool",fallbackMethod = "nlpHysFb",
            commandProperties = {
                    @HystrixProperty(name = "execution.isolation.strategy", value = "THREAD"),
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000"),
                    @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),
                    @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "2")},
            threadPoolProperties = {
                    @HystrixProperty(name = "coreSize", value = "5"),
                    @HystrixProperty(name = "maxQueueSize", value = "10")
            })

HystrixCommand 没有生效加入下面的bean
@Bean
public HystrixCommandAspect hystrixCommandAspect() {
return new HystrixCommandAspect();
}

  1. 获取Hystrix 内存中的监控数据并上报数据到influxdb
    @Scheduled(cron = "*/1 * * * * ?")
    public void hystrixTask() {
        // 获取指标数据
        Collection<HystrixCommandMetrics> instances = HystrixCommandMetrics.getInstances();
        instances.forEach(metrixs -> {
            // 创建tag值
            Map<String, String> tags = tag(metrixs);
            // 创建fields值
            Map<String, Object> fields = field(metrixs);
            // 写入数据
            Point.Builder builder = Point.measurement("hystrix_install_test");
            builder.time(System.currentTimeMillis(), TimeUnit.MILLISECONDS);
            builder.tag(tags);
            builder.fields(fields);
            influxDB.write("pic", "autogen", builder.build());
            log.info("hystrixTask builder :{}", JSONObject.toJSONString(builder.build()));

        });
    }

    public Map<String, String> tag(HystrixCommandMetrics metrixs) {
        // 获取commandKey
        String commandKey = metrixs.getCommandKey().name();
        Map<String, String> tags = Maps.newHashMap();
        tags.put("key", commandKey);
        tags.put("env", "test");
        return tags;
    }

    public Map<String, Object> field(HystrixCommandMetrics metrixs) {
        // 获取请求总数
        long totalRequests = metrixs.getHealthCounts().getTotalRequests();
        // 获取错误数
        long errorCount = metrixs.getHealthCounts().getErrorCount();
        // 获取错误百分比
        int errorPercentage = metrixs.getHealthCounts().getErrorPercentage();
        // 获取成功数
        long successCount = metrixs.getRollingCount(HystrixRollingNumberEvent.SUCCESS);
        // 获取超时数
        long timeoutCount = metrixs.getRollingCount(HystrixRollingNumberEvent.TIMEOUT);
        int p90 = metrixs.getTotalTimePercentile(90);
        int p50 = metrixs.getTotalTimePercentile(50);
        int p99 = metrixs.getTotalTimePercentile(99);
        int p100 = metrixs.getTotalTimePercentile(100);
        Map<String, Object> fields = Maps.newHashMap();
        fields.put("totalRequests", totalRequests);
        fields.put("errorCount", errorCount);
        fields.put("errorPercentage", errorPercentage);
        fields.put("successCount", successCount);
        fields.put("timeoutCount", timeoutCount);
        fields.put("p90", p90);
        fields.put("p50", p50);
        fields.put("p99", p99);
        fields.put("p100", p100);
        return fields;
    }

influxdb 配置

influxdb 安装
https://blog.csdn.net/pys52055/article/details/123965581

  1. 引入依赖
   <!--  influxdb数据上报 -->
        <dependency>
            <groupId>org.influxdb</groupId>
            <artifactId>influxdb-java</artifactId>
            <version>2.15</version>
        </dependency>
  1. influxdb配置
@Configuration
public class InfluxDbConfig {

    @Value("${influxDB.serverAddr}")
    private String serverAddr;
    @Value("${influxDB.username}")
    private String username;
    @Value("${influxDB.password}")
    private String password;

    @Bean
    public InfluxDB influxDB() {
        // 连接influxDB数据库
        InfluxDB influxDB = InfluxDBFactory.connect(serverAddr, username, password);
        return influxDB;
    }
}

Grafana监控的使用

grafana 安装
https://blog.csdn.net/pys52055/article/details/124226441

最后

以上就是积极毛豆为你收集整理的Hystrix + influxdb + Grafana 监控的搭建的全部内容,希望文章能够帮你解决Hystrix + influxdb + Grafana 监控的搭建所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(49)

评论列表共有 0 条评论

立即
投稿
返回
顶部