hystrix-dashboard

进行可视化的一个组件,我们来加上


	org.springframework.cloud
	spring-cloud-starter-hystrix-dashboard


名字有点长,写完之后我们刷新一下,大家如果在网站上看一些Hystrix的文章,博客,他可能会让你加这么一个组件,

可能会让你加这么一个组件


	org.springframework.boot
	spring-boot-starter-actuator


但是其实在我们这里其实不用加了,我们复制一下actuator,我们不是已经引了spring-cloud-starter-stream,

他这里面已经用到了,所以你不需要再引了,最开始是spring-cloud-starter-stream-rabbit引进来的,所以大家

灵活用,当然你这个地方引进来也没有错,他不会报错的,就是代码多写了一点,那既然已经有了,那就不要再引了,

写个注释,如果已经有了,就不用再引入,引入了依赖之后,还需要在启动类上加一个注解,@EnableHystrixDashboard

@SpringCloudApplication
@EnableFeignClients
@EnableHystrixDashboard
public class OrderApplication {

	public static void main(String[] args) {
		// Spring应用启动起来
		SpringApplication.run(OrderApplication.class,args);
	}
	
}

启动一下,浏览器里面访问hystrix

localhost:8010/hystrix

访问到了之后你会看到这么一个界面

像个刺猬一样的,这里要填入应用的地址,这里看到有三种情况,有集群,我们这里是单个应用,最后这一种,我们的地址是

http://localhost:8010/hystrix.stream

时间写1秒

1000

我们主要是想监控order服务的断路情况

hystrix-dashboard_第1张图片

不能连接指标

localhost:8010/createOrder

hystrix-dashboard_第2张图片

我们看一下请求,这里在发送请求

Proxy opening connection to: http://localhost:8010/hystrix.stream?delay=1000

这也是一个小小的坑,到了2.0这个地方稍微有点区别,看一下启动的日志,其实他是想让我们

填hystrix.stream这个地址,那你可以看到前面多了一个application,这个application怎么去掉呢,

需要配置一下

management:context-path=/

: Mapped "{[/hystrix.stream/**]}" 

我们写一个斜杠就好了,我们重启一下,待会我们可以看控制台的指标变成什么样子,现在就是loading的状态了,

没有报不能连接上了,我们来访问之前的接口

http://localhost:8010/createOrder

再来看一下这边的指标就已经变了

hystrix-dashboard_第3张图片

localhost:8010/getProductInfoList?number=1

http://localhost:8010/getProductInfoList?number=1

hystrix-dashboard_第4张图片

我们来尝试一下失败,我们只要把超时时间给他配置一下,超时时间给他配成一秒,大家可以看到这里有几个数字,

其实数字是和上面的颜色对应的,这个0就是success,这样子对应过来,这个commandKey默认就是我们的方法名,看

这个,这个是熔断的一个状态,是否熔断,熔断的配置给他打开一下

@HystrixCommand(commandProperties = {
		@HystrixProperty(name = "circuitBreaker.enabled", value = "true"),//设置熔断
		@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
		@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"),
		@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60")
})
@GetMapping("/getProductInfoList")
public  String getProductInfoList(@RequestParam("number") Integer number){
	if(number % 2 == 0){
		return  "success";
	}
	RestTemplate restTemplate = new RestTemplate();
   return restTemplate.postForObject("http://127.0.0.1:7900/product/listForOrder", 
   Arrays.asList("157875196366160022"),String.class);

}

再来访问一下

http://localhost:8010/getProductInfoList?number=1

这里失败了,就发红了,多访问几次,我们要访问10次以上,并且失败率是大于60%,你看他现在的熔断已经是打开的状态了

hystrix-dashboard_第5张图片

如果你觉得在浏览器里手动的刷新比较low的话,其实我们也有相对比较高端的方式,复制这个连接,

http://localhost:8010/getProductInfoList?number=1

使用POSTMAN,POSTMAN有一个功能,可以在一定时间,发送很多的请求,粘贴到这,以前我们是直接发送一个

请求,发送单个的请求,那你是不是要在这个地方狂点,当然不是,这也挺low的,跟在浏览器刷新有什么

区别,粘贴到这以后呢,注意你要保存一下,点这个save,保存到哪呢,新建一个Connection,比如就叫SpringCloud

好了,在最后,保存

hystrix-dashboard_第6张图片

保存之后这里自动有一个run,上面有一个Runner,点击进入到Runner模式

hystrix-dashboard_第7张图片

进来之后再选择SpringCloud,我们这个组,这里有环境,环境不用选,下面这个,这个什么意思呢,

这是你要发多少个请求,比如我要发100个请求,中间延时多少毫秒呢,比如5毫秒好了,点击run,

可以和浏览器里面对比一下

hystrix-dashboard_第8张图片

hystrix-dashboard_第9张图片

hystrix-dashboard_第10张图片

这个工具可以帮我们不停地发请求,发送100次请求,很快就发完了,这里你可以明显的看到

已经发生熔断了,我们可以再来跑一次,大家仔细观察一下这些指标有什么变化,这里有一个红心

圆的,当然他不一定是红心,这个圆是什么意思呢,圆的大小表示流量,流量越大,圆就越大,颜色越

偏向红色,表示这个服务就越不健康,这个百分比通过刚刚的演示,猜也能猜到了,就是错误率,这个指标是

请求的频率,下边的open还是close,是熔断的状态,还有这条在不停的变化的线,我相信也不用我多说,

这条线表示一段时间内,流量的相对变化,大家重点关注的是哪几个指标呢,一个是错误率,了,另外你要看这几个,

看看超时的指标,这个黄色对应的就是这个黄色,还有发生熔断的请求数,对应的是这个颜色,重点关注这几个指标

以上就是hystrix-dashboard组件的使用

hystrix-dashboard_第11张图片


  4.0.0
  com.learn
  order
  0.0.1-SNAPSHOT
  jar
  
  
    cn.learn
    microcloud02
    0.0.1
  
	
	
		UTF-8
		UTF-8
		1.8
		3.0.9.RELEASE
		2.2.2
	
	
	
		
			org.springframework.boot
			spring-boot-starter-amqp
		
		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
		
		  org.projectlombok
		  lombok
		
		
			org.springframework.cloud
			spring-cloud-starter-eureka
		
		
		    org.springframework.cloud
		    spring-cloud-starter-stream-rabbit
		
        
            org.springframework.cloud
            spring-cloud-starter-hystrix
        
		
			org.springframework.cloud
			spring-cloud-starter-feign
		
        
            org.springframework.cloud
            spring-cloud-starter-hystrix-dashboard
        
        
	
	
	
	
	    
	        
	            org.springframework.boot
	            spring-boot-maven-plugin
	        
	    
	
  
  
server.port=8010

eureka.client.serviceUrl.defaultZone=http://admin:[email protected]:8761/eureka

spring.application.name=order
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}

spring.rabbitmq.host=59.110.158.145
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.port=5672

spring.cloud.stream.bindings.myMessage.group=order
spring.cloud.stream.bindings.myMessage.content-type=application/json

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=1000
hystrix.command.getProductInfoList.execution.isolation.thread.timeoutInMilliseconds=5000

feign.hystrix.enabled=true
package com.learn.controller;

import java.util.Arrays;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;

@RestController
@DefaultProperties(defaultFallback = "defaultFallback")
public class HystrixController {
 
    //@HystrixCommand(fallbackMethod = "fallback")
    //2、超时设置
//    @HystrixCommand(commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000") //超时时间设置为3秒
//    })
//    3.
    @HystrixCommand(commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),//设置熔断
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"),
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60")
    })
//    @HystrixCommand
    @GetMapping("/getProductInfoList")
    public  String getProductInfoList(@RequestParam("number") Integer number){
//    public  String getProductInfoList(){
        if(number % 2 == 0){
            return  "success";
        }
        RestTemplate restTemplate = new RestTemplate();
       return restTemplate.postForObject("http://127.0.0.1:7900/product/listForOrder", Arrays.asList("157875196366160022"),String.class);
 
    }
 
    private String fallback(){
        return "太拥挤了,请稍后再试~~";
    }
 
 
    private String defaultFallback(){
        return "默认提示:太拥挤了,请稍后再试~~";
    }
}
package com.learn;

import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

//@SpringBootApplication
//@EnableRabbit
//@EnableEurekaClient
//@EnableCircuitBreaker
@SpringCloudApplication
@EnableFeignClients
@EnableHystrixDashboard
public class OrderApplication {

	public static void main(String[] args) {
		// Spring应用启动起来
		SpringApplication.run(OrderApplication.class,args);
	}
	
}

 

你可能感兴趣的:(hystrix-dashboard)