springcloud系列文章已經出到hystrix,中間知識追尋者跑去學了其它知識,回來感受spingcloud系列出的也很多了;須要徹底理解這篇文章對於初學者須要有必定的基礎知識,若是看不懂請移步知識追尋者的springcloud專欄進行學習;學完本篇,你將得到學會使用Hstrix進行服務容錯,學會Hystrix熔斷監控;能學完本篇內容請留下贊呦!!!!java
首先須要搭建工程hystrix-client
引入eureka和 hystrix依賴git
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> </dependencies>
在service層定義一個得到用戶的方法getUser()用於提供服務,方法內容對用戶名進行判斷,若是是zszxz
, 返回回用戶說明,不然拋出異常;定義默認用戶方法defaultUser()用於容錯處理,在得到用戶方法上面添加註釋@HystrixCommand
而且將容錯回調指向defaultUser()方法;有關注釋詳細使用請讀者參考以下官方文檔;github
/** * @Author lsc * <p> </p> */ @Component public class UserService { // 爲getUser方法添加容錯回調處理 @HystrixCommand(fallbackMethod = "defaultUser") public String getUser(String params){ if (params.equals("zszxz")){ return "the user is zszxz"; }else { // 拋出異常時會直接調用fallbackMethod中指定的方法 throw new RuntimeException(); } } /* * * @Author lsc * <p> 出錯回調處理</p> * @Param [params] * @Return java.lang.String */ public String defaultUser(String params){ return "it is the user thar is not exist in project"; } }
表現層直接調用服務層方法,參數爲用戶名username
spring
/** * @Author lsc * <p> </p> */ @RestController public class UserController { @Autowired UserService userService; @GetMapping("user") public String getUser(String username){ return userService.getUser(username); } }
配置文件中指定端口8094
, 而且指定應用名稱爲 hystrix-client
, 後面是向eureka-server進行服務註冊;app
server: port: 8094 spring: application: name: hystrix-client # 應用名稱 eureka: client: service-url: # 服務註冊地址 defaultZone: http://peer1:10081/eureka/,http://peer2:10082/eureka/,http://peer3:10083/eureka/
在啓動類上添加註釋@EnableHystrix
,表示啓用 hystrix功能;ide
/** * @Author lsc * <p> </p> */ @SpringBootApplication @EnableHystrix // 啓用 hystrix @EnableDiscoveryClient// 服務註冊發現 public class HystrixApp { public static void main(String[] args) { SpringApplication.run(HystrixApp.class, args); } }
啓動eureka-server, eureka-client, hystrix-client工程;訪問地址http://localhost:8094/user?user=zszxz;得出正確訪問結果以下;函數
修改用戶名參數不是zszxz 發現報的不是異常,而是defualtuser中默認的容錯處理,說明使用hystrix成功;spring-boot
學會了簡單使用hystrix, 但遠遠不夠,一般Feign中自帶了hystrix功能,這邊須要使用Feign進行開啓hystrix,而後進行容錯處理;學習
在以前的eureka-client
表現層中添加一個 getFH() 方法用於Feign客戶端調用;
/* * * @Author lsc * <p> feign-hystix 集成測試</p> * @Param [username] * @Return java.lang.String */ @GetMapping("zszxz/fh") public String getFH(String username){ if (username.equals("zszxz")){ return "the user is zszxz on fh project"; }else { throw new RuntimeException(); } }
新建工程feign-hystrix
用於集成測試,依賴引入 openfeign 便可;
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies>
service層使用FeignClient進行遠程過程調用,而且指定 回調類,其內容在3.4節
/** * @Author lsc * <p> </p> */ @FeignClient( name = "eureka-client", value = "eureka-client", fallback = FHServiceFallback.class) public interface FHService { @GetMapping("zszxz/fh") public String getFeign(String username); }
回調函數實現FHService接口,而且在類上添加註解@Component
表示會被spirng IO容器掃描注入;
/** * @Author lsc * <p> </p> */ @Component public class FHServiceFallback implements FHService { @Override public String getFeign(String username) { return "sorry feilure,you can try it again"; } }
controller層直接能夠調用service層接口;
/** * @Author lsc * <p> feign 表現層 </p> */ @RestController public class FHController { @Autowired FHService fhService; @GetMapping("zszxz/feign") public String getFeign(String username){ // 調用 getFeign方法 return fhService.getFeign(username); } }
配置文件中指定端口,應用名稱,關鍵是要開啓feign自帶的hystix 功能
server: port: 8095 spring: application: name: hystrix-feign # 應用名稱 eureka: client: service-url: # 服務註冊地址 defaultZone: http://peer1:10081/eureka/,http://peer2:10082/eureka/,http://peer3:10083/eureka/ # 開啓hystix feign: hystrix: enabled: true
啓動類中使用註解 @EnableFeignClients
開啓Feign功能;
/** * @Author lsc * <p> </p> */ @SpringBootApplication @EnableFeignClients public class FeignHystrixApp { public static void main(String[] args) { SpringApplication.run(FeignHystrixApp.class,args); } }
正常訪問結果正確,結果以下
輸入用戶名非zszxz,進行容錯處理友好提示以下,說明feign集成使用hystix成功;
學會使用hystix仍是皮毛中的皮毛,咱們還須要對hystrix進行容錯監控;
在feign-hystrix
工程中添加 actuator
, hystrix
依賴;
pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
appication.yml配置文件中指定暴露端點;
management: endpoints: web: exposure: include: hystrix.stream
啓動類中添加@EnableHystrix
註解表示開啓Hystrix
/** * @Author lsc * <p> </p> */ @SpringBootApplication @EnableFeignClients @EnableHystrix public class FeignHystrixApp { public static void main(String[] args) { SpringApplication.run(FeignHystrixApp.class,args); } }
新建工程hystrix-dashboard 用於專門的容錯監控;引入依賴以下;
pom.xml
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency> </dependencies>
application.yml 指定當前應用名稱和端口;
server: port: 8096 spring: application: name: hystrix-dashboard # 應用名稱 eureka: client: service-url: # 服務註冊地址 defaultZone: http://peer1:10081/eureka/,http://peer2:10082/eureka/,http://peer3:10083/eureka/
啓動類上表名註解 @EnableHystrixDashboard
表示啓動容錯監控;
/** * @Author lsc * <p> </p> */ @SpringBootApplication @EnableDiscoveryClient @EnableHystrixDashboard//開啓監控 public class HystixDashboard { public static void main(String[] args) { SpringApplication.run(HystixDashboard.class,args); } }
訪問 http://localhost:8096/hystrix 出現一頭豪豬圖頁面表示初步配置成功;
頁面中的輸入url有以下三種:
在頁面輸入地址 http://localhost:8095/actuator/hystrix.stream 訪問單體應用,頁面會處於loading狀態
當咱們訪問 http://localhost:8095/zszxz/feign 會出現儀表盤,其具體的監控指標內容意義請讀者參考官方文檔,再也不詳細說明。
在第四節中讀者已經學會如何使用hystix dashboard進行容錯監控;可是缺點也很明顯,那就是一個單體應用須要開啓一個dashboard 頁面;若是使用turbine進行集成管理,那麼全部hystix應用都會顯示在一個 dashboard上,方便咱們查閱,排查;
新建 hystrix-feign2 工程;修改配置文件端口爲8098,應用名稱爲hystrix-feign2,其它內容跟 hystrix-feign 工程同樣;
新建turbine-monitor工程,添加新依賴 turbine
pom.xml
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-turbine</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency> </dependencies>
application.xml 中內容以下 指定了監控的hystrix應用是hystrix-feign
和 hystrix-feign
, 而且採用默認名稱方式;
server: port: 8097 spring: application: name: turbine-monitor # 應用名稱 eureka: client: service-url: # 服務註冊地址 defaultZone: http://peer1:10081/eureka/,http://peer2:10082/eureka/,http://peer3:10083/eureka/ turbine: appConfig: hystrix-feign,hystrix-feign2 clusterNameExpression: "'default'"
啓動類中添加新的註解@EnableTurbine
表示開啓turbine 功能;
/** * @Author lsc * <p> </p> */ @SpringBootApplication @EnableDiscoveryClient @EnableTurbine @EnableHystrixDashboard public class TurbineApp { public static void main(String[] args) { SpringApplication.run(TurbineApp.class, args); } }
關於源碼請移步專欄說明便可得到知識追尋者springcloud所有學習內容;