Hystrix--熔斷
服務熔斷是指當服務訪問頻繁被拒絕後會呼叫服務降級的fallbackMethod方法拒絕訪問, 當過一段時間後會進入半開狀態先允許一部分請求進行訪問如果可以則調整為全開狀態
1.主啟動
@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
public class PaymentHystrixMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentHystrixMain8001.class,args);
}
/*
* 此配置是為了服務監控而配置,與服務容錯本身無關,spring cloud升級後的坑
* servletRegistrationBean因為springboot的預設路徑不是"/.hystrix.stream"
* 只要在自己的專案裡配置上下面的servlet就可以了
* */
@Bean
public ServletRegistrationBean getsServlet(){
HystrixMetricsStreamServlet streamServlet=new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean=new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
}
二、service層
@Service
public class PaymentService {
public String paymentInfo_OK(Integer id){
return "執行緒池: "+Thread.currentThread().getName()+" paymentInfo_OK,id: "+id+"\t"+"O(∩_∩)O哈哈";
}
@HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler",commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "5000")
})
public String paymentInfo_TimeOut(Integer id){
int timenumbe=3000;
// int age=10/0;
try {
TimeUnit.MILLISECONDS.sleep(timenumbe);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "執行緒池: "+Thread.currentThread().getName()+" paymentInfo_TimeOut,id: "+id+"\t"+"O(∩_∩)O哈哈"+"耗時(毫秒 ): "+timenumbe;
}
public String paymentInfo_TimeOutHandler(Integer id){
return "執行緒池: "+Thread.currentThread().getName()+" 8001系統忙,請稍後再試,id: "+id+"\t"+"┭┮﹏┭┮";
}
//服務熔斷
@HystrixCommand(fallbackMethod = "paymentCircuitBreaker_fallback",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"),//失敗率達到多少後跳閘
})
public String paymentCircuitBreaker(@PathVariable("id") Integer id){
if (id<0){
throw new RuntimeException("**********id 不能負數");
}
String serialNumber=IdUtil.simpleUUID();//等價於UUID.randomUUID().toString()
return Thread.currentThread().getName()+"\t"+"呼叫成功,流水號: "+serialNumber;
}
public String paymentCircuitBreaker_fallback(@PathVariable("id") Integer id){
return "id 不能負數,請稍後再試,┭┮﹏┭┮ id: "+id;
}
}
三、controller
@RestController
@Slf4j
public class PaymentController {
@Resource
private PaymentService paymentService;
@Value("${server.port}")
private String serPort;
@GetMapping("/payment/hystrix/ok/{id}")
public String paymentInfo_OK(@PathVariable("id") Integer id){
String result=paymentService.paymentInfo_OK(id);
log.info("******result: "+result);
return result;
}
@GetMapping("/payment/hystrix/timeout/{id}")
public String paymentInfo_timeOut(@PathVariable("id") Integer id){
String result=paymentService.paymentInfo_TimeOut(id);
log.info("******result: "+result);
return result;
}
//===服務熔斷
@GetMapping("/payment/circuit/{id}")
public String paymentCircuitBreaker(@PathVariable("id")Integer id){
String result=paymentService.paymentCircuitBreaker(id);
log.info("*******result: "+result);
return result;
}
}
相關文章
- Envoy熔斷限流實踐(一)基於Rainbond外掛實現熔斷AI
- Sentinel限流熔斷降級
- 熔斷原理與實現
- springcloud之Hystrix熔斷器SpringGCCloud
- Spring cloud(4)-熔斷(Hystrix)SpringCloud
- springcloud(四):熔斷器HystrixSpringGCCloud
- 微服務SpringCloud之熔斷器微服務SpringGCCloud
- springcloud(六):熔斷監控TurbineSpringGCCloud
- 微服務分散式系統熔斷實戰-為何我們需要API級別熔斷?微服務分散式API
- 微服務熔斷限流Hystrix之Dashboard微服務
- 微服務熔斷限流Hystrix之流聚合微服務
- springcloud(五):熔斷監控Hystrix DashboardSpringGCCloud
- 12.SpringCloudAlibabaSentinel實現熔斷和限流SpringGCCloud
- 《SpringCloud專題17》-Hystrix熔斷器案例SpringGCCloud
- SpringCloud-Hystrix 服務降級、熔斷SpringGCCloud
- .NET Core 微服務之Polly熔斷策略微服務
- 五. SpringCloud服務降級與熔斷SpringGCCloud
- 分散式熔斷降級平臺aegis分散式
- go-kit微服務:服務熔斷Go微服務
- Spring Cloud Feign 熔斷機制填坑SpringCloud
- 一個故事理解限流熔斷降級
- Spring Cloud Alibaba:Sentinel實現熔斷與限流SpringCloud
- 面試官:說說降級、熔斷、限流面試
- SpringCloud原始碼學習之Hystrix熔斷器SpringGCCloud原始碼
- Spring Cloud實戰系列(四) - 熔斷器HystrixSpringCloud
- SpringCloud學習筆記:熔斷器Hystrix(5)SpringGCCloud筆記
- 微服務元件之限流器與熔斷器微服務元件
- Sentinel入門到實操 (限流熔斷降級)
- (24)SpringCloud-Hystrix(熔斷器)介紹及使用SpringGCCloud
- Istio實踐(4)- 故障注入、熔斷及ServiceEntry
- .Net Core微服務——Ocelot(3):超時、熔斷、限流微服務
- 服務的熔斷和降級的區別
- 跟我學SpringCloud | 第四篇:熔斷器HystrixSpringGCCloud
- 從kratos分析breaker熔斷器原始碼實現原始碼
- 面試官:熔斷降級原理是什麼?面試
- 9.Spring Cloud Alibaba Sentinel流控熔斷元件SpringCloud元件
- springcloud之hystrix熔斷器-Finchley.SR2版SpringGCCloud
- 使用springcloud gateway搭建閘道器(分流,限流,熔斷)SpringGCCloudGateway