Spring Cloud 關於:Spring Cloud Netflix Hystrix
服務短路(CircuitBreaker)
QPS:Query Per Second
TPS:Transaction Per Second
QPS:經過全鏈路壓測,計算單機極限QPS,叢集QPS=單機PQS*叢集機器數量*可靠性比率
全鏈路壓測,除了壓極限QPS,還有錯誤數量
全鏈路:一個完整的業務流程操作
JMeter:可調整型比較靈活
Spring Cloud Hystrix Client
官網:
Reactive Java框架:
java9 Flow API
Reactor
RxJava(Reactive X)
啟用Hystrix
透過@EnableHystrix啟用
配置資訊wiki:https//github.com/Netflix/Hystrix/wiki/Configuration
Hystrix
1. 註解方式
@HystrixCommand(defaultFallback= "errorContent",commandProperties =
{@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value= "100")})
@GetMapping("/user/count")
public String userCount() throws InterruptedException {
Random random = new Random();
int nextInt = random.nextInt(200);
System.out.println("random time > "+nextInt);
Thread.sleep(nextInt);
return userService.userCount();
}
public String errorContent() {
System.out.println("超時了");
return "-1";
}
2. 程式設計方式
@GetMapping("/user/count2")
public String userCount2() {
return new MyHystrixCommand().execute();
}
private class MyHystrixCommand extends com.netflix.hystrix.HystrixCommand{
protected MyHystrixCommand() {
super(HystrixCommandGroupKey.Factory.asKey(""), 100);
}
@Override
protected String run() throws Exception {
Random random = new Random();
int nextInt = random.nextInt(200);
System.out.println("random time > "+nextInt);
Thread.sleep(nextInt);
return "OK";
}
@Override
protected String getFallback() {
return UserServiceProviderRestApiController.this.errorContent();
}
}
對比其他Java執行方式:
Feature
public class FutrueDemo {
public static void main(String[] args) {
Random random = new Random();
ExecutorService service = Executors.newFixedThreadPool(1);
Future futrue = service.submit(() -> {
int value = random.nextInt(200);
System.out.print("睡眠"+value+"ms.");
Thread.sleep(value);
return "OK";
});
try {
futrue.get(100,TimeUnit.MILLISECONDS);
}catch (Exception e) {
System.out.println("超時保護...");
}
service.shutdown();
}
}
Health Endpoint (/actuator/health)
{
"status": "UP",
"details": {
"diskSpace": {
"status": "UP",
"details": {
"total": 140382552064,
"free": 7376781312,
"threshold": 10485760
}
},
"refreshScope": {
"status": "UP"
},
"discoveryComposite": {
"status": "UP",
"details": {
"discoveryClient": {
"status": "UP",
"details": {
"services": ["user-service-consumer", "user-service-provider", "eureka-server"]
}
},
"eureka": {
"description": "Remote status from Eureka server",
"status": "UP",
"details": {
"applications": {
"USER-SERVICE-CONSUMER": 1,
"EUREKA-SERVER": 2,
"USER-SERVICE-PROVIDER": 2
}
}
}
}
},
"hystrix": {
"status": "UP"
}
}
}
啟用熔斷保護 無錫婦科醫院排行
@EnableCircuitBreaker 啟用:@EnableHystrix + Spring Cloud功能
@EnableHystrix 啟用,沒有一些Spring Cloud功能,如 /hystrix.stream
hystrix Endpoint(/actuator/hystrix.stream)
data: {
"type": "HystrixCommand",
"name": "MyHystrixCommand",
"group": "",
"currentTime": 1563720628038,
"isCircuitBreakerOpen": false,
"errorPercentage": 50,
"errorCount": 2,
"requestCount": 4,
"rollingCountBadRequests": 0,
"rollingCountCollapsedRequests": 0,
"rollingCountEmit": 0,
"rollingCountExceptionsThrown": 0,
"rollingCountFailure": 0,
"rollingCountFallbackEmit": 0,
"rollingCountFallbackFailure": 0,
"rollingCountFallbackMissing": 0,
"rollingCountFallbackRejection": 0,
"rollingCountFallbackSuccess": 1,
"rollingCountResponsesFromCache": 0,
"rollingCountSemaphoreRejected": 0,
"rollingCountShortCircuited": 0,
"rollingCountSuccess": 2,
"rollingCountThreadPoolRejected": 0,
"rollingCountTimeout": 1,
"currentConcurrentExecutionCount": 0,
"rollingMaxConcurrentExecutionCount": 1,
"latencyExecute_mean": 0,
"latencyExecute": {
"0": 0,
"25": 0,
"50": 0,
"75": 0,
"90": 0,
"95": 0,
"99": 0,
"99.5": 0,
"100": 0
},
"latencyTotal_mean": 0,
"latencyTotal": {
"0": 0,
"25": 0,
"50": 0,
"75": 0,
"90": 0,
"95": 0,
"99": 0,
"99.5": 0,
"100": 0
},
"propertyValue_circuitBreakerRequestVolumeThreshold": 20,
"propertyValue_circuitBreakerSleepWindowInMilliseconds": 5000,
"propertyValue_circuitBreakerErrorThresholdPercentage": 50,
"propertyValue_circuitBreakerForceOpen": false,
"propertyValue_circuitBreakerForceClosed": false,
"propertyValue_circuitBreakerEnabled": true,
"propertyValue_executionIsolationStrategy": "THREAD",
"propertyValue_executionIsolationThreadTimeoutInMilliseconds": 100,
"propertyValue_executionTimeoutInMilliseconds": 100,
"propertyValue_executionIsolationThreadInterruptOnTimeout": true,
"propertyValue_executionIsolationThreadPoolKeyOverride": null,
"propertyValue_executionIsolationSemaphoreMaxConcurrentRequests": 10,
"propertyValue_fallbackIsolationSemaphoreMaxConcurrentRequests": 10,
"propertyValue_metricsRollingStatisticalWindowInMilliseconds": 10000,
"propertyValue_requestCacheEnabled": true,
"propertyValue_requestLogEnabled": true,
"reportingHosts": 1,
"threadPool": ""
}
Spring Cloud Hystrix Dashboard
使用@EnableHystrixDashboard啟用
@SpringBootApplication
@EnableHystrixDashboard
public class SpringCloudHystrixDashboardApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudHystrixDashboardApplication.class, args);
}
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69945560/viewspace-2655714/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Spring Cloud Netflix—如何加入HystrixSpringCloud
- 7、Spring Cloud HystrixSpringCloud
- Spring Cloud正式移除Hystrix、Zuul等Netflix OSS元件SpringCloudZuul元件
- Spring Cloud Netflix—斷路器:Hystrix客戶端SpringCloud客戶端
- Spring Cloud 之 Hystrix.SpringCloud
- Spring cloud(4)-熔斷(Hystrix)SpringCloud
- 關於Spring Cloud EurekaSpringCloud
- Spring Cloud Hystrix應用篇(十一)SpringCloud
- Spring Cloud Hystrix原碼篇(十一)SpringCloud
- Spring Cloud Hystrix 容錯保護SpringCloud
- Spring Cloud Netflix—使用EurekaClientSpringCloudclient
- spring cloud微服務分散式雲架構-Spring Cloud NetflixSpringCloud微服務分散式架構
- Spring Boot整合Spring Cloud Netflix元件Spring BootCloud元件
- Spring Cloud 2021.0.1 移除了Hystrix、Zuul等Netflix元件SpringCloudZuul元件
- Spring Cloud Hystrix的請求合併SpringCloud
- spring cloud之斷路器hystrix(五)SpringCloud
- 關於Spring Cloud的核心特性SpringCloud
- Spring Cloud Netflix的3大元件應用 Eureka&Ribbon&HystrixSpringCloud元件
- Spring Cloud Hystrix 服務容錯保護SpringCloud
- Spring Cloud Hystrix:服務容錯保護SpringCloud
- Spring Cloud實戰系列(四) - 熔斷器HystrixSpringCloud
- springcloud學習筆記(四)Spring Cloud HystrixSpringGCCloud筆記
- Spring Cloud 原始碼學習之 Hystrix 入門SpringCloud原始碼
- Spring Cloud(五)斷路器監控(Hystrix Dashboard)SpringCloud
- Spring Cloud在Netflix後時代的走向?SpringCloud
- Spring Cloud(二):Spring Cloud ConfigSpringCloud
- Spring cloudSpringCloud
- spring cloud:Edgware.RELEASE版本hystrix超時新坑SpringCloud
- 我是如何替換Spring Cloud Netflix的?SpringCloud
- Spring Cloud相關專案SpringCloud
- 基於Spring Cloud和Netflix OSS構建微服務,Part 2SpringCloud微服務
- 基於Spring Cloud和Netflix OSS 構建微服務-Part 1SpringCloud微服務
- Spring Cloud與Spring Boot版本匹配關係CloudSpring Boot
- spring cloud 和 阿里微服務spring cloud AlibabaSpringCloud阿里微服務
- Spring Cloud介紹 Spring Cloud與Dubbo對比SpringCloud
- 瘋狂Spring Cloud連載(1)Spring Cloud概述SpringCloud
- 微服務Spring Cloud17_Spring Cloud概述3微服務SpringCloud
- spring cloud微服務分散式雲架構--hystrix的使用SpringCloud微服務分散式架構