【121期】面試官:什么是熔斷?什么是服務(wù)降級?
閱讀本文大概需要 5.5?分鐘。
來自:blog.csdn.net/qq_41497111/article/details/92067565
服務(wù)熔斷
服務(wù)降級
熔斷VS降級
目標(biāo)一致 都是從可用性和可靠性出發(fā),為了防止系統(tǒng)崩潰; 用戶體驗(yàn)類似 最終都讓用戶體驗(yàn)到的是某些功能暫時不可用;
觸發(fā)原因不同 服務(wù)熔斷一般是某個服務(wù)(下游服務(wù))故障引起,而服務(wù)降級一般是從整體負(fù)荷考慮;
Hystrix簡介
通過第三方客戶端的庫來為訪問依賴服務(wù)時的潛在故障提供保護(hù)和控制; 防止在復(fù)雜分布式系統(tǒng)中出現(xiàn)級聯(lián)故障; 快速失敗和迅速恢復(fù); 在允許的情況下,提供退路對服務(wù)進(jìn)行優(yōu)雅降級; 提供近實(shí)時的監(jiān)控、報警和操作控制;
使用Hystrix
引入Hystrix依賴
??org.springframework.boot
??spring-boot-starter-parent
??2.0.6.RELEASE
?
?
?
??Finchley.SR2
?
?
?
??
???org.springframework.boot
???spring-boot-starter-web
??
??
??
???org.springframework.cloud
???spring-cloud-starter-netflix-eureka-client
??
??
??
???org.springframework.cloud
???spring-cloud-starter-openfeign
??
??
??
???org.springframework.cloud
???spring-cloud-starter-netflix-hystrix
??
?
?
?
??
???
???
????org.springframework.cloud
????spring-cloud-dependencies
????${spring-cloud.version}
????<type>pomtype>
????import
???
??
?
修改啟動類
@EnableFeignClients
@EnableCircuitBreaker
public class MessageCenterApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(MessageCenterApplication.class).web(WebApplicationType.SERVLET).run(args);
}
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
public?@interface?SpringCloudApplication?{
}
修改Controller
?@GetMapping("/msg/get")
?@HystrixCommand(fallbackMethod?=?"getMsgFallback")
?public?Object?getMsg()?{
??String?msg?=?messageService.getMsg();
??return?msg;
?}
?
?public?Object?getMsgFallback()?{
??return?"祝您 2019 豬年大吉,'豬'事如意!";
?}
?/**
??*?獲取消息詳情
??*/
?@GetMapping("/api/v1/msg/detail/{id}")
?@HystrixCommand(fallbackMethod?=?"getDetailFallback")
?public?MessageEntity?getDetail(@PathVariable(name?=?"id")?Long?id)?{
??return?messageService.getById(id);
?}
?
?/**
??*?獲取消息詳情退路
??*/
?public?MessageEntity?getDetailFallback(Long?id){
??return?null;
?}
Feign結(jié)合Hystrix
修改Feign客戶端
@FeignClient(name?=?"message-service",?fallback?=?MessageServiceFallback.class)
public?interface?MessageServiceClient?{
?
?@GetMapping("/api/v1/msg/get")
?public?String?getMsg();
?
}
創(chuàng)建Fallback處理類
@Component
public?class?MessageServiceFallback?implements?MessageServiceClient?{
?
?@Override
?public?String?getMsg()?{
??System.out.println("調(diào)用消息接口失敗,對其進(jìn)行降級處理!");
??return?"消息接口繁忙,請稍后重試!";
?}
?
}
修改配置
feign:
??hystrix:
????enabled:?true
監(jiān)控Hystrix
啟用健康監(jiān)控
??
??
???org.springframework.boot
???spring-boot-starter-actuator
??
management:
??endpoints:
????web:
??????exposure:
????????include:?hystrix.stream
啟用Hystrix-Dashboard
引入Hystrix-Dashboard依賴
??
??
???org.springframework.cloud
???spring-cloud-starter-netflix-hystrix-dashboard
??
修改啟動類
@EnableFeignClients
@SpringCloudApplication
@EnableHystrixDashboard
public?class?MessageCenterApplication?{
?
?public?static?void?main(String[]?args)?{
??new?SpringApplicationBuilder(MessageCenterApplication.class).web(WebApplicationType.SERVLET).run(args);
?}
?
}
儀表盤界面
參考文章
https://github.com/netflix/hystrix/wiki? https://github.com/netflix/hystrix? https://cloud.spring.io/spring-cloud-netflix/single/spring-cloud-netflix.html
推薦閱讀:
【119期】談?wù)勗陧?xiàng)目中,如何應(yīng)對高并發(fā)流量
【118期】面試官:你真的清楚 i = i++和 i = ++i 的區(qū)別嗎?
微信掃描二維碼,關(guān)注我的公眾號
朕已閱?
評論
圖片
表情

