hystrix-dashboard
生活随笔
收集整理的這篇文章主要介紹了
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);}}啟動一下,瀏覽器里面訪問hystrixlocalhost: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=1http://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=8010eureka.client.serviceUrl.defaultZone=http://admin:1234@10.40.8.152:8761/eurekaspring.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=5672spring.cloud.stream.bindings.myMessage.group=order
spring.cloud.stream.bindings.myMessage.content-type=application/jsonhystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=1000
hystrix.command.getProductInfoList.execution.isolation.thread.timeoutInMilliseconds=5000feign.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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: feign-hystrix的使用
- 下一篇: 服务追踪(上)