hystrix-dashboard

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

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>

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

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

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

但是其实在我们这里其实不用加了,我们复制一下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服务的断路情况

不能连接指标

localhost:8010/createOrder

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

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

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

localhost:8010/getProductInfoList?number=1

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

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

其实数字是和上面的颜色对应的,这个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%,你看他现在的熔断已经是打开的状态了

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

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

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

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

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

好了,在最后,保存

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

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

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

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

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

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

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

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

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

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

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

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

<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.learn</groupId>
  <artifactId>order</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  
  <parent>
    <groupId>cn.learn</groupId>
    <artifactId>microcloud02</artifactId>
    <version>0.0.1</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>
		<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
		<thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>
	</properties>
	
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-amqp</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
		  <groupId>org.projectlombok</groupId>
		  <artifactId>lombok</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
		<dependency>
		    <groupId>org.springframework.cloud</groupId>
		    <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
		</dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-feign</artifactId>
		</dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
        </dependency>
        <!-- <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency> -->
	</dependencies>
	
	<!-- 这个插件,可以将应用打包成一个可执行的jar包 -->
	<build>
	    <plugins>
	        <plugin>
	            <groupId>org.springframework.boot</groupId>
	            <artifactId>spring-boot-maven-plugin</artifactId>
	        </plugin>
	    </plugins>
	</build>
  
  
</project>
server.port=8010

eureka.client.serviceUrl.defaultZone=http://admin:1234@10.40.8.152: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);
	}
	
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值