使用 Turbine 聚合监控
在使用 Hystrix Dashboard 组件监控服务的熔断状况时,每个服务都有一个 Hystrix Dashboard 主页,当服务数量很多时相当不方便。为了同时监控多个服务的熔断器的状态,可以使用 Turbine。Turbine 用于聚合多个 Hystrix Dashboard,将多个 Hystrix Dashboard 组件的数据放在一个页面上展示,进行集中监控。
创建父项目
本例采用 Maven 多模块结构,首先创建一个父项目 spring-cloud-turbine,其 pom 文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.wuychn</groupId><artifactId>spring-cloud-turbine</artifactId><packaging>pom</packaging><version>1.0-SNAPSHOT</version><modules><module>eureka-server</module><module>service-provider</module><module>service-consumer-hi</module><module>service-consumer-hello</module><module>turbine-monitor</module></modules><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.0.RELEASE</version></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><spring.cloud.version>Finchley.RELEASE</spring.cloud.version></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring.cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement></project>
创建服务注册中心
在父项目下新建一个子模块 eureka-server,作为服务注册中心,其 pom 文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>spring-cloud-turbine</artifactId><groupId>com.wuychn</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>eureka-server</artifactId><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
application.yml:
server:port: 9001spring:application:name: eureka-servereureka:instance:hostname: localhostclient:fetch-registry: falseregister-with-eureka: falseserviceUrl:defaultZone: http://localhost:9001/eureka/
程序启动类:
package com.wuychn;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {public static void main(String[] args) {SpringApplication.run(EurekaServerApplication.class, args);}
}
创建服务提供者
在父项目下新建一个子模块 service-provider,作为服务提供者,其 pom 文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>spring-cloud-turbine</artifactId><groupId>com.wuychn</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>service-provider</artifactId><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
application.yml:
server:port: 9003spring:application:name: service-providereureka:client:serviceUrl:defaultZone: http://localhost:9001/eureka/
程序启动类:
package com.wuychn;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication
@EnableEurekaClient
public class ServiceProviderApplication {public static void main(String[] args) {SpringApplication.run(ServiceProviderApplication.class, args);}}
在 service-provider 中,有两个 Controller,一个是 HiController,一个是 HelloController,它们都有一个 REST API 接口,供服务消费者消费,HiController 如下:
package com.wuychn.controller;import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class HiController {@Value("${server.port}")private String port;@GetMapping("/hi")public String hi(String name) {return "hi " + name + ", I am from port:" + port;}}
HelloController 如下:
package com.wuychn.controller;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class HelloController {@GetMapping("/hello")public String hello(String name) {return "Hello, " + name;}}
创建服务消费者 service-consumer-hi
在父项目中新建一个子模块 service-consumer-hi,作为服务消费者,它会使用 Feign 调用 service-provider 的 /hi 接口。其 pom 文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>spring-cloud-turbine</artifactId><groupId>com.wuychn</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>service-consumer-hi</artifactId><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
application.yml:
spring:application:name: service-consumer-hi
server:port: 9004
eureka:client:serviceUrl:defaultZone: http://localhost:9001/eureka/
feign:hystrix:enabled: true
程序启动类:
package com.wuychn;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 ServiceConsumerHiApplication {public static void main(String[] args) {SpringApplication.run(ServiceConsumerHiApplication.class, args);}}
HiController:
package com.wuychn.controller;import com.wuychn.service.HiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;@RestController
public class HiController {@Autowiredprivate HiService hiService;@GetMapping("/hi")public String hi(@RequestParam(required = false, defaultValue = "wuychn") String name) {return hiService.hi(name);}}
HiService:
package com.wuychn.service;import com.wuychn.client.HiFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class HiService {@Autowiredprivate HiFeignClient hiFeignClient;public String hi(String name) {return hiFeignClient.hi(name);}
}
HiFeignClient:
package com.wuychn.client;import com.wuychn.client.config.FeignConfig;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;@FeignClient(value = "service-provider", configuration = FeignConfig.class, fallback = HiFeignFallback.class)
public interface HiFeignClient {// 如果不添加@RequestParam注解,会被转化为POST请求,从而报405@GetMapping("/hi")String hi(@RequestParam(value = "name") String name);
}@Component
class HiFeignFallback implements HiFeignClient {@Overridepublic String hi(String name) {return "sorry, " + name + ", please wait..";}
}
FeignConfig:
package com.wuychn.client.config;import feign.Retryer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import static java.util.concurrent.TimeUnit.SECONDS;@Configuration
public class FeignConfig {@Beanpublic Retryer reignRetryer() {return new Retryer.Default(100, SECONDS.toMillis(1), 5);}
}
HystrixDashboardConfig:
package com.wuychn.config;import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class HystrixDashboardConfig {@Beanpublic ServletRegistrationBean getServlet() {HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);registrationBean.setLoadOnStartup(1);registrationBean.addUrlMappings("/hystrix.stream");registrationBean.setName("HystrixMetricsStreamServlet");return registrationBean;}}
创建服务消费者 service-consumer-hello
service-consumer-hello 和 service-consumer-hi 类似,只不过它是调用 service-provider 的 /hello 接口。
创建 turbine-monitor
最后,在父项目下新建一个子项目 turbine-monitor,作为 Turbine 聚合监控的工程,其完整的 pom 文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>spring-cloud-turbine</artifactId><groupId>com.wuychn</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>turbine-monitor</artifactId><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-turbine</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId></dependency></dependencies></project>
application.yml:
server:port: 9006spring:application:name: turbine-monitorturbine:aggregator:clusterConfig: defaultappConfig: service-consumer-hi,service-consumer-helloclusterNameExpression: new String("default")instanceUrlSuffix: /hystrix.stream
eureka:client:serviceUrl:defaultZone: http://localhost:9001/eureka/
其中,turbine.aggregatorappConfig 配置了需要监控的服务名,本例中,需要监控的服务有 service-consumer-hi 和 service-consumer-hello。clusterNameExpression 默认为服务名的集群,使用默认就可以。instanceUrlSuffix 和被监控服务中的 registrationBean.addUrlMappings(“/hystrix.stream”) 指定的路径一致。
程序启动类如下:
package com.wuychn;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.turbine.EnableTurbine;@SpringBootApplication
@EnableTurbine
public class TurbineMonitorApplication {public static void main(String[] args) {SpringApplication.run(TurbineMonitorApplication.class, args);}
}
到这里,代码就写完了,整个项目的结构如图:
依次启动 eureka-server、service-provider、service-consumer-hi、service-consumer-hello 和 turbine-monitor,在浏览器中访问 http://localhost:9004/hystrix,在界面上依次输入监控流的地址 http://localhost:9006/turbine.stream、监控间隔时间 2000 毫秒和 title,单击 Monitor Stream,可以看到如下界面:
至此,使用 Turbine 聚合监控完成。
使用 Turbine 聚合监控相关推荐
- SpringCloud系列七:Hystrix 熔断机制(Hystrix基本配置、服务降级、HystrixDashboard服务监控、Turbine聚合监控)...
声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:Hystrix 熔断机制 2.具体内容 所谓的熔断机制和日常生活中见到电路保险丝是非常相似的,当出现了问题之后,保险丝 ...
- SpringCloud:Hystrix 熔断机制(基本配置、服务降级、HystrixDashboard服务监控、Turbine聚合监控)
所谓的熔断机制和日常生活中见到电路保险丝是非常相似的,当出现了问题之后,保险丝会自动烧断,以保护我们的电器, 那么如果换到了程序之中呢?当现在服务的提供方出现了问题之后整个的程序将出现错误的信息显示, ...
- SpringCloud配置之:Turbine聚合监控
Turbine聚合监控 一.搭建监控模块 1. 创建监控模块 创建hystrix-monitor模块,使用Turbine聚合监控多个Hystrix dashboard功能, 2. 引入Turbine聚 ...
- Turbine聚合监控
Turbine聚合监控 一.搭建监控模块 1. 创建监控模块 创建hystrix-monitor模块,使用Turbine聚合监控多个Hystrix dashboard功能, 2. 引入Turbine聚 ...
- 使用Turbine聚合监控
Turbine概述 Turbine真正做的,就是将每一个(指定)服务的Hystrix/stream中的状态信息取出,并集中处理(计算与展示),应该说,它是具有自己独立的调度的,服务(实例)发现,服务连 ...
- Hystrix Turbine聚合监控
之前,我们针对的是一个微服务实例的Hystrix数据查询分析,在微服务架构下,一个微服务的实例往往是多个(集群化). 比如自动投递微服务 实例1(hystrix) ip1:port1/actuator ...
- 使用Turbine聚合监控数据
一.使用Turbine聚合监控数据 使用/hystrix.stream端点监控单个微服务实例.然而,使用微服务架构的应用系统一般会包含若干微服务,每个微服务通常都会部署多个实例.如果每次只能查看单个实 ...
- spring cloud之Turbine断路器聚合监控(十二)
一.博客背景 上一章讲解了针对一个微服务的断路器监控,但是微服务通常会是多个实例组成的一个集群. 倘若集群里的实例比较多,难道要挨个挨个去监控这些实例吗? 何况有时候,根据集群的需要,会动态增加或者减 ...
- turbine 集群聚合监控
目录 一.turbine介绍 二.turbine配置 1.当前项目环境说明 2.turbine环境配置 (1)创建项目 (2)编辑pom (3)编辑yml (4)编辑主程序 (5)测试集群监控 3.项 ...
最新文章
- Codeforces Beta Round #2 B. The least round way
- 如何在Ruby中求和数字数组?
- SqlSessionFactoryBuilder、SqlSessionFactory、SqlSession作用域(Scope)和生命周期
- 去年3545款恶意App遭下架
- html制作水晶状态导航栏,HTML5 CSS3水晶风格的页面头部
- 看我如何基于PythonFacepp打造智能监控系统
- C++描述杭电OJ 2010.水仙花数 ||
- 解决vscode中Module ‘torch‘ has no ‘xxx‘ member的烦人错误
- android中界面布局文件放在,android界面布局详解.doc
- Hadoop原理和特性
- Elasticsearch 实战(四、分词与IK分词器)
- “拖延症”的良方——对于追求完美,自制力差,情绪化的人很受用。 【谨以此文共勉。】 来源: 胡野的日志
- 罗永浩是偏执,还是骗子?
- 如果不知道这4种缓存模式,敢说懂缓存吗?
- iis mysql密码_mysql忘记root密码与root帐号被删除处理方法
- 英文单词缩写规则(转自天涯)
- 利用Host-only模式使用虚拟机静态IP上网
- python怎么处理通达信ctp接口数据?
- Lucene学习——IKAnalyzer中文分词(一)
- socket通信简介(概念、函数、原理)