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
监控页面分析(七色+一圈+一线)