SpringCloud之监控数据聚合Turbine
前言
SpringCloud 是微服务中的翘楚,最佳的落地方案。
使用 SpringCloud 的 Hystrix Dashboard 组件可以监控单个应用服务的调用情况,但如果是集群环境,可能就
不能满足需求了,这时就用到了 SpringCloud 另一个组件:Turbine。
Turbine 将每个应用服务的调用情况聚合在一起展示出来。
如果了解过 Hystrix Dashboard,那么可以简单认为 Turbine 就相当于另起了一个工程,把其他工程的监控情况
全部显示到了 Turbine 工程中。
源码
GitHub地址:https://github.com/intomylife/SpringCloud
环境
JDK 1.8.0 +
Maven 3.0 +
SpringBoot 2.0.3
SpringCloud Finchley.RELEASE
开发工具
IntelliJ IDEA
正文
commons 工程
commons 工程 - POM 文件
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.zwc
springcloud-turbine-commons
1.0
springcloud-turbine-commons
公用工程
jar
UTF-8
1.8
Cairo-SR3
Finchley.RELEASE
io.spring.platform
platform-bom
${platform-bom.version}
pom
import
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud-dependencies.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
配置一些共用依赖
commons 工程 - 项目结构
service 工程
① 此工程下有五个模块:一个注册中心,一个聚合监控中心以及服务 A、B、C
② A 提供服务并且调用服务 B、B 提供服务并且调用服务 C 以及 C 提供服务
registry-service(注册中心)
registry-service - POM 文件
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.zwc
springcloud-turbine-service
1.0
com.zwc
springcloud-turbine-registry-service
1.0
springcloud-turbine-registry-service
注册中心
jar
com.zwc
springcloud-turbine-commons
1.0
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-maven-plugin
主要是加入 spring-cloud-starter-netflix-eureka-server 依赖
registry-service - application.yml 配置文件
# 端口
server:
port: 8761
# 应用名称
spring:
application:
name: eureka-server
eureka:
instance:
# 使用 ip 代替实例名
prefer-ip-address: true
# 实例的主机名
hostname: ${spring.cloud.client.ip-address}
# 实例的 ID 规则
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
client:
# 是否向注册中心注册自己
registerWithEureka: false
# 是否向注册中心获取注册信息
fetchRegistry: false
serviceUrl:
# 注册中心地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
这里使用了默认的 8761 端口,当然也可以更改,不过在发现调用服务端的注册中心地址端口要与它一致
registry-service - 启动类
package com.zwc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class SpringcloudTurbineRegistryServiceApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudTurbineRegistryServiceApplication.class, args);
}
}
在启动类中添加 @EnableEurekaServer 注解表示此工程是注册中心
registry-service - 启动项目
1. 项目启动成功后访问 http://localhost:8761/ 即可看到 eureka-server 主页面
master-service(聚合监控中心)
master-service - POM 文件
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.zwc
springcloud-turbine-service
1.0
com.zwc
springcloud-turbine-master-service
1.0
springcloud-turbine-master-service
集群监控
jar
com.zwc
springcloud-turbine-commons
1.0
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
org.springframework.cloud
spring-cloud-starter-netflix-hystrix-dashboard
org.springframework.cloud
spring-cloud-netflix-turbine
org.springframework.boot
spring-boot-maven-plugin
加入 spring-cloud-starter-netflix-eureka-client 依赖:提供和注册服务
加入依赖 spring-boot-starter-actuator:开启并配置端点
加入依赖 spring-cloud-starter-netflix-hystrix:熔断器
加入依赖 spring-cloud-starter-netflix-hystrix-dashboard:熔断监控
加入依赖 spring-cloud-netflix-turbine:监控数据聚合
master-service - application.yml 配置文件
# 端口
server:
port: 8762
# 应用名称
spring:
application:
name: hystrix-dashboard-turbine
eureka:
instance:
# 使用 ip 代替实例名
prefer-ip-address: true
# 实例的主机名
hostname: ${spring.cloud.client.ip-address}
# 实例的 ID 规则
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
client:
serviceUrl:
# 注册中心地址
defaultZone: http://${eureka.instance.hostname}:8761/eureka/
turbine:
# 监控的应用名称,多个以逗号隔开
app-config: turbine-a,turbine-b
aggregator:
# 指定聚合哪些集群,默认为 default
clusterConfig: default
# 指定集群名称为 default
clusterNameExpression: new String("default")
注意此处配置注册中心地址的端口为 8761 也就是上面注册中心工程配置的端口
此工程需要和应用服务指定同一个注册中心地址
配置的 app-config 中的应用名称都将会被此工程监控
master-service - 启动类
package com.zwc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.turbine.EnableTurbine;
@SpringBootApplication
@EnableEurekaClient
@EnableHystrix
@EnableHystrixDashboard
@EnableTurbine
public class SpringcloudTurbineMasterServiceApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudTurbineMasterServiceApplication.class, args);
}
}
添加 @EnableEurekaClient 注解表示此工程可以向注册中心提供服务
添加 @EnableHystrix 注解表示开启熔断器
添加 @EnableHystrixDashboard 注解表示开启熔断监控
添加 @EnableTurbine 注解表示开启监控数据聚合
master-server - 启动项目
1. 项目启动成功后访问 http://localhost:8762/hystrix 可以看到
2. 在中间的输入框中输入:http://127.0.0.1:8762/turbine.stream ,点击 Monitor Stream 按钮可以看到
3. 此时还未调用服务,所以一直显示 'Loading ...'
服务工程 A(提供者和消费者)
服务工程 A - POM 文件
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.zwc
springcloud-turbine-a-service
1.0
com.zwc
springcloud-turbine-a-service-core
1.0
springcloud-turbine-a-service-core
服务工程 - A 核心
jar
com.zwc
springcloud-turbine-commons
1.0
com.zwc
springcloud-turbine-a-service-api
1.0
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-openfeign
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
org.springframework.cloud
spring-cloud-starter-netflix-hystrix-dashboard
org.springframework.boot
spring-boot-maven-plugin
加入 spring-cloud-starter-netflix-eureka-client 依赖:提供和注册服务
加入 Feign 的起步依赖 spring-cloud-starter-openfeign:消费服务
加入依赖 spring-boot-starter-actuator:开启并配置端点
加入依赖 spring-cloud-starter-netflix-hystrix:熔断器
加入依赖 spring-cloud-starter-netflix-hystrix-dashboard:熔断监控
服务工程 A - application.yml 配置文件
# 端口
server:
port: 8090
# 应用名称
spring:
application:
name: turbine-a
eureka:
instance:
# 使用 ip 代替实例名
prefer-ip-address: true
# 实例的主机名
hostname: ${spring.cloud.client.ip-address}
# 实例的 ID 规则
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
client:
serviceUrl:
# 注册中心地址
defaultZone: http://${eureka.instance.hostname}:8761/eureka/
management:
endpoints:
web:
exposure:
# 开启监控端点
include: hystrix.stream
注意此处配置注册中心地址的端口为 8761 也就是上面注册中心工程配置的端口
spring.application.name:应用名称,被消费者调用时需要用到,它在消费的同时也可以被消费
另一个服务工程 B 配置与此类似,只是端口不一样,不再赘述
在 2.0 后需要主动开启端点 hystrix.stream,否则 404
服务工程 A - application.properties(注意)
# 开启断路器
feign.hystrix.enabled=true
断路器要主动开启,服务调用失败时才会熔断
此处有一个坑,把此配置写到 yml 中熔断不会生效
application.properties 和 bootstrap.yml 二选一就行
服务工程 A - bootstrap.yml(注意)
feign:
hystrix:
# 开启断路器
enabled: true
断路器要主动开启,服务调用失败时才会熔断
此处有一个坑,把此配置写到 application.yml 中熔断不会生效
application.properties 和 bootstrap.yml 二选一就行
服务工程 A - controller 前端控制器(提供服务)
package com.zwc.a.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/*
* @ClassName ASayHelloController
* @Desc TODO Say Hello
* @Date 2019/5/20 23:24
* @Version 1.0
*/
@RestController
public class ASayHelloController {
/*
* @ClassName ASayHelloController
* @Desc TODO 读取配置文件中的端口
* @Date 2019/5/20 23:24
* @Version 1.0
*/
@Value("${server.port}")
private String port;
/*
* @ClassName ASayHelloController
* @Desc TODO Say Hello
* @Date 2019/5/20 23:24
* @Version 1.0
*/
@RequestMapping("/a")
public String a(){
return "Hello!I'm a. port:" + port;
}
}
提供一个服务:输出 Hello 和端口
服务工程 A - 服务调用
package com.zwc.a.api.feign;
import com.zwc.a.api.impl.FeignApiFallBack;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
/*
* @ClassName FeignApi
* @Desc TODO 使用 Feign 调用 b - 接口
* @Date 2019/5/20 23:21
* @Version 1.0
*/
@FeignClient(value = "turbine-b" , fallback = FeignApiFallBack.class)
public interface FeignApi {
/*
* @ClassName FeignApi
* @Desc TODO 通过 turbine-b 服务名调用 b() 方法
* @Date 2019/5/20 23:21
* @Version 1.0
*/
@RequestMapping("/b")
String b();
}
通过 @FeignClient 注解中 value = "turbine-b" 来指定调用哪个服务
turbine-b 就是提供者的 spring.application.name:应用名称
通过 @FeignClient 注解中 fallback = FeignApiFallBack.class 来指定熔断时调用的方法
FeignApiFallBack 就是此类(FeignApi)的实现类,对应的实现方法就是此类的熔断时调用的方法
b():此方法是 B 工程中提供的服务,在这里定义成接口
注意要与提供者具有相同返回值,相同方法名以及相同参数
服务工程 A - Fallback(FeignApiFallBack)
package com.zwc.a.api.impl;
import com.zwc.a.api.feign.FeignApi;
import org.springframework.stereotype.Component;
/*
* @ClassName FeignApi
* @Desc TODO fallback
* @Date 2019/5/20 23:21
* @Version 1.0
*/
@Component
public class FeignApiFallBack implements FeignApi {
/*无锡人流医院哪家好 http://www.wxbhnkyy120.com/
* @ClassName FeignApiFallBack
* @Desc TODO 调用 turbine-b 服务中的 b() 方法失败时执行
* @Date 2019/5/20 23:31
* @Version 1.0
*/
@Override
public String b() {
return "Hello!aUseB fail";
}
}
使用 @Component 注解把此类交给 Spring 管理
实现了 FeignApi 接口,提供熔断时对应的方法
服务工程 A - controller 前端控制器(消费服务)
package com.zwc.a.controller;
import com.zwc.a.api.feign.FeignApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/*
* @ClassName AUseBFeignController
* @Desc TODO 使用 Feign 调用 b - 前端控制器
* @Date 2019/5/20 23:23
* @Version 1.0
*/
@RestController
public class AUseBFeignController {
@Autowired(required = false)
private FeignApi feignApi;
/*
* @ClassName FeignController
* @Desc TODO 通过 turbine-b 服务名调用 b() 方法
* @Date 2019/5/20 23:13
* @Version 1.0
*/
@RequestMapping("/aUseB")
public String aUseB(){
return feignApi.b();
}
}
使用 @Autowired 注解装配 Bean,通过此 Bean 中的方法调用服务
此类对外暴露接口,调用的实则是提供者的服务
服务工程 A - 启动类
package com.zwc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableHystrix
@EnableHystrixDashboard
public class SpringcloudTurbineAServiceCoreApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudTurbineAServiceCoreApplication.class, args);
}
}
添加 @EnableEurekaClient 注解表示此工程可以向注册中心提供服务
添加 @EnableFeignClients 注解表示开启 Feign 功能进行远程调用
添加 @EnableHystrix 注解表示开启熔断器
添加 @EnableHystrixDashboard 注解表示开启熔断监控
服务工程 A - 启动项目
1. 项目启动成功后访问:http://localhost:8090/a (调用自己的服务)
2. 输出内容:'Hello!I'm a. port:8090'
3. 访问地址:http://localhost:8090/aUseB (调用 B 工程的服务)
4. 输出内容:'Hello!aUseB fail' (此时因为 B 工程还未启动,所以调用了 fallback 中的方法)
5. 这时再回到 master-server 工程的 'Loading ...' 页面,发现刚刚服务调用失败已经被监控到了
6. 如果还是显示的 'Loading ...' ,请再稍等一会
7. 启动服务工程 B,项目启动成功后再次访问:http://localhost:8090/aUseB (调用 B 工程的服务)
8. 输出内容:'Hello!I'm b. port:8091' (如果还未调用成功,等待一会再刷新试试)
9. 这时再回到 master-server 工程,可以看到
10. 另起页面,访问:http://localhost:8091/bUseC (调用 C 工程的服务)
11. 输出内容:'Hello!bUseC fail' (此时因为 C 工程还未启动,所以调用了 fallback 中的方法)
12. 这时再回到 master-server 工程,可以看到调用 C 工程也被监控到了
13. 红色的百分比表示失败率
14. 左边的六个数字对应着页面右上角的六个状态(Success、Short-Circuited..)
service 工程 - 项目结构
把多工程项目使用 IntelliJ IDEA 打开
把项目从 GitHub 中下载到你的本地
打开 IntelliJ IDEA
点击 File -> Open
打开你下载到本地的项目目录
springcloud-turbine -> springcloud-turbine-service(选择打开此工程)
打开 service 工程后
再次点击 File -> Project Structrue
选择 Modules,点击 '+' 符号
点击 Import Module
还是打开你下载到本地的项目目录
springcloud-turbine -> springcloud-turbine-commons -> pom.xml
点击 OK
点击 Next,Finish
点击 Apply,OK
SpringCloud之监控数据聚合Turbine的更多相关文章
- Spring Cloud(六):Hystrix 监控数据聚合 Turbine【Finchley 版】
Spring Cloud(六):Hystrix 监控数据聚合 Turbine[Finchley 版] 发表于 2018-04-17 | 更新于 2018-05-07 | 上一篇我们介绍了使用 H ...
- Spring Cloud学习笔记【六】Hystrix 监控数据聚合 Turbine
上一篇我们介绍了使用 Hystrix Dashboard 来展示 Hystrix 用于熔断的各项度量指标.通过 Hystrix Dashboard,我们可以方便的查看服务实例的综合情况,比如:服务调用 ...
- Hystrix 监控数据聚合 Turbine【Finchley 版】
原文地址:https://windmt.com/2018/04/17/spring-cloud-6-turbine/ 上一篇我们介绍了使用 Hystrix Dashboard 来展示 Hystrix ...
- 白话SpringCloud | 第六章:Hystrix监控面板及数据聚合(Turbine)
前言 前面一章,我们讲解了如何整合Hystrix.而在实际情况下,使用了Hystrix的同时,还会对其进行实时的数据监控,反馈各类指标数据.今天我们就将讲解下Hystrix Dashboard和Tur ...
- Spring Cloud架构教程 (二)Hystrix监控数据聚合
上一篇我们介绍了使用Hystrix Dashboard来展示Hystrix用于熔断的各项度量指标.通过Hystrix Dashboard,我们可以方便的查看服务实例的综合情况,比如:服务调用次数.服务 ...
- Sentinel上生产环境只差一步,监控数据持久化
之前介绍了Sentinel相关的文章,小伙伴在生产实践中不知道有没有这个疑问?我们的Sentinel控制台监控的数据只能看最近5分钟的,如图 那么就导致历史数据是查看不了的,那肯定是不行的,在生产环境 ...
- springcloud(十一):熔断聚合监控Hystrix Turbine
springcloud(十一):熔断聚合监控Hystrix Turbine
- SpringCloud学习笔记(5):Hystrix Dashboard可视化监控数据
简介 上篇文章中讲了使用Hystrix实现容错,除此之外,Hystrix还提供了近乎实时的监控.本文将介绍如何进行服务监控以及使用Hystrix Dashboard来让监控数据图形化. 项目介绍 sc ...
- 数据聚合 & 分组:新一代系统监控的核心功能
遥想 2015 年 8 月 17 日,Cloud Insight 还在梳理功能原型,畅想 Cloud Insight 存在的意义:为什么阿里云用户需要使用 Cloud Insight 来加强管理. 而 ...
随机推荐
- 解决——》java.lang.IllegalArgumentException: Body parameter 0 was null
1.操作2.现象(错误信息)3.原因错误代码:4.解决1)方案一:@RequestBody(required=false)2)方案二:传参数时限制authSession不能为空ody paramete ...
- LeetCode 896. Monotonic Array
原题链接在这里:https://leetcode.com/problems/monotonic-array/ 题目: An array is monotonic if it is either mon ...
- git blame (10)
git blame system_server.c 每一行提交的sha ,作者,提交的日期及提交的信息
- 回溯法 | n皇后问题
今早上看了一篇英语阅读之后,莫名有些空虚寂寞冷.拿出算法书,研读回溯法.我觉得n皇后问题完全可以用暴力方式,即先对n个数进行全排列,得到所有结果的下标组合,问题规模为n!. 全排列花了比较久的时间才编 ...
- haproxy 配置文件详解 之 frontend
配置示例: frontend www bind *: mode http option httplog option forwardfor option httpclose log global #a ...
- python 使用nmap 模块
官网 https://pypi.org/project/python-nmap/ >>> import nmap>>> nm = nmap.PortScannerS ...
- @RequestMapping 用法详解
简介: @RequestMapping RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上.用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径. RequestM ...
- c# winform button文字偏了
winform button文字偏了,解决方案来自 疯狂青蛙: http://www.cnblogs.com/cadlife 要用这个属性
- [笔记] 二级指针(pointer to pointer)
// 1.pointer to pointer.cpp #include "stdafx.h" #include <stdlib.h> int _tmain(int a ...
- Golang检测Linux服务器端口占用
代码实现 func CheckPort(port int) error { checkStatement := fmt.Sprintf(`netstat -anp | grep -q %d ; ech ...