我是靠谱客的博主 积极毛豆,这篇文章主要介绍Hystrix + influxdb + Grafana 监控的搭建,现在分享给大家,希望可以做个参考。

Hystrix 的使用

  1. 引入maven依赖
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!--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 监控接口数据
复制代码
1
2
3
4
5
6
7
8
9
10
11
@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
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
@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. 引入依赖
复制代码
1
2
3
4
5
6
7
<!-- influxdb数据上报 --> <dependency> <groupId>org.influxdb</groupId> <artifactId>influxdb-java</artifactId> <version>2.15</version> </dependency>
  1. influxdb配置
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@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内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部