SpringCloud——断路器Hystrix、启用Hystrix仪表盘

SpringCloud默认监控服务之间的调用为2s,如果超过这个时间,就会根据你的配置使用其他方式进行响应。

一、消费者工程引入依赖
spring-cloud-starter-netflix-hystrix (spring-cloud-starter-hystrix)

二、启动断路器

@SpringBootApplication(scanBasePackages="com.springboot.chapter.product")
@EnableFeignClients(basePackages="com.springboot.chapter.product")
@EnableCiruitBreaker //启用断路机制
public class ChapterProductApplication{
	
}

三、调用测试断路器的Feign接口

@GetMapping("/timeout")
public String testTimeout();

四、使用断路机制

//Ribbon断路
@GetMapping("/circuitBreaker1")
@HystrixCommand(fallbackMethod="error") //表示将在该方法上启动断路机制,属性error表示降级方法
public String circuitBreaker1(){
	return restTemplate.getForObject("http://USER/timeout",String.class);
}

//Feign断路测试
@GetMapping("/circuitBreaker2")
@HystrixCommand(fallbackMethod="error")//commandProperties={}
public String circuitBreaker2(){
	return userServcie.testTimeout();
}

//降级服务方法
public String error(){
	return "超时出错";
}

启用Hystrix仪表盘

在这里插入图片描述

一、新建工程dashboard,导入仪表盘依赖
spring-cloud-starter-netflix-hystrix-dashboard (spring-cloud-starter-hystrix-dashboard + spring-boot-starter-actuator)

二、启用仪表盘

@SpringBootApplication
@EnableHystrixDashboard //启用仪表盘
public class ChapterDashboardApplication{
	public static void main(String[] args) 
		SpringApplication.run(ChapterDashboardApplication.class, args);
	}
}

三、配置文件properties

server.port=6001
spring.application.name=hystrix_dashboard

四、浏览器访问
http://localhost:6001/hystrix

在这里插入图片描述
点击"Monitor Stream"按钮,进入监控页面,显式"loading…"
在浏览器进行刷新断路请求
在这里插入图片描述
在这里插入图片描述


集群监控

一、导入依赖
spring-cloud-starter-turbine spring-boot-starter-actuator

二、@EnableTurbine开启Turbine

@EnableTurbine 
@EnableDiscoveryClient 
@SpringBootApplication
public class TurbineApplication { 
	public static void main(String[] args) { 
		SpringApplication.run(TurbineApplication.class, args);
	}
}

三、配置文件

spring:
	application:
		name: turbine
server:
	port: 8989
management:
	port: 8990
enreka:
	client:
		serviceUrl:
			defaultZone: http://localhost:1111/eureka/
turbine:
	app-config: RIBBON-CONSUMER #指定需要收集监控信息的服务名
	cluster-name-expression: default 制定了集群名称,当服务数量多,可以启动多turbine构建不同的集群
	combine-host-port: true #让同一主机上的服务通过主机名和端口号的组合进行区分,默认情况下以host来区分不同服务

四、分别启动eureka-server(注册中心)、 HELLO-SERVICE(服务生产者)、 RIBBON-CONSUMER(服务消费者)、 Turbine(集群监控) 以及 Hystrix Dashboard(监控页面)

五、http://localhost:8989/turbine.stream
在这里插入图片描述

在这里插入图片描述
监控页面分析(七色+一圈+一线)
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值