目標
集成Hystrix容錯機制颖侄,集成hystrix-dashboard進行監(jiān)控管理
簡介
Hystrix是為了防止分布式架構(gòu)中雪崩效應(yīng)問題战得,主要實現(xiàn)功能有以下兩點:
- 網(wǎng)絡(luò)請求設(shè)置超時時間,減少資源浪費
-
斷路器模式襟齿,類似家庭中的保險絲,流程圖如下:
斷路器實現(xiàn)模式
開工集成
- 添加hystrix依賴,同時我們需要開啟hystrix-dashboard監(jiān)控功能享幽,其原理依據(jù)spring-boot-starter-actuator健康檢查,所以同時在client中添加spring-boot-starter-actuator依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- 啟動類添加hystrix注解
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients(basePackages = "open.template.work.udm.client.feign")
@EnableHystrix
public class UdmClientApplication {
public static void main(String[] args) {
SpringApplication.run(UdmClientApplication.class, args);
}
@Bean
public IRule ribbonRoundRobinRule() {
return new RandomRule();
}
}
- application.yml開啟Hystrix功能
feign:
hystrix:
enabled: true
- 在feign接口中添加容錯處理類
package open.template.work.udm.client.feign;
import feign.hystrix.FallbackFactory;
import open.template.work.commons.constants.CloudServerDirectory;
import open.template.work.commons.vo.udm.request.TaskRequestVo;
import open.template.work.commons.vo.udm.resposne.TaskReponseVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
@FeignClient(name = CloudServerDirectory.UDM_SERVER,fallbackFactory = FeignCLientFallbackFactory.class)
public interface UdmServerTaskFegin {
@PostMapping("/task/get")
TaskReponseVo getTaskInfo(@RequestBody TaskRequestVo vo);
}
@Component
class FeignCLientFallbackFactory implements FallbackFactory<UdmServerTaskFegin>{
private static final Logger LOGGER=LoggerFactory.getLogger(FeignCLientFallbackFactory.class);
@Override
public UdmServerTaskFegin create(Throwable throwable) {
return new UdmServerTaskFegin(){
@Override
public TaskReponseVo getTaskInfo(TaskRequestVo vo) {
LOGGER.info("udm server fallback,reason was:{}",throwable==null?"空異常":throwable.getMessage());
TaskReponseVo responseVo=new TaskReponseVo();
responseVo.setStatus("0");
responseVo.setTaskId("0");
return responseVo;
}
};
}
}
-
這樣啟動客戶端后拾弃,關(guān)閉服務(wù)端測試
image.png
在看接口返回結(jié)果
正是我們默認返回的結(jié)果
- 但上面只是單次錯誤的降級值桩,并沒有觸發(fā)斷路器,只有當單位時間內(nèi)達到錯誤閾值時豪椿,斷路器才會真正打開奔坟,我們可以通過/health查看hystrix狀態(tài),注意actuator一些敏感信息不會默認展示砂碉,此時health查看的數(shù)據(jù)可能不完整蛀蜜,需要在application.yml中添加配置才可看到
management:
security:
enabled: false
此時查看/health
image.png
可見hystrix是up狀態(tài),顯示運行正常增蹭,表示斷路器并沒有啟用滴某,我們此時關(guān)閉對應(yīng)的所有提供者,多次調(diào)用測試
斷路器此時
這是斷路器屬于開啟狀態(tài),此時重啟服務(wù)提供者霎奢,過段時間后户誓,斷路器會自動關(guān)閉。
-
下面集成hystrix-dashboard幕侠,hystrix-dashboard是基于spring-boot-starter-actuator的數(shù)據(jù)進行可視化的監(jiān)控帝美,需要我們重新建立個module,
image.png
依賴中添加
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
入口添加支持
@SpringBootApplication
@EnableHystrixDashboard
public class HystrixDashboardApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixDashboardApplication.class, args);
}
}
application.yml指定端口
server:
port: 9000
啟動服務(wù)晤硕,訪問服務(wù)http://localhost:9000/hystrix
image.png
第一行url輸入我們要查看的服務(wù)地址悼潭,uri為hystrix,stream,設(shè)置延時和標題舞箍,啟動
image.png
這樣就可以監(jiān)控到此服務(wù)的錯誤熔斷情況舰褪。
- 上面的dashboard是對單應(yīng)用的監(jiān)控,如果需要同時監(jiān)控多個微服務(wù)就需要turbine技術(shù)疏橄,其原理就是將指定的若干個hystrix.stream進行整合展示占拍,本章不作為重點討論內(nèi)容,有需要可以參考https://www.cnblogs.com/leeSmall/p/8847652.html