springcloud(九):熔斷器Hystrix和Feign的應用案例

由於 feign 中已經支持了 Hystrix ,因此在 Feign 中使用 Hystrix 時,不須要導包,也不須要在入口類上面增長額外的註解;html

 Feign 雖然支持了 Hystrix ,可是默認狀況下是關閉的,須要在 配置文件配置java

1.建立項目web

 

 

2. 選擇項目類型spring

 

 3.選擇項目名稱,能夠隨便寫,可是不能有大寫服務器

 

4.在最左側菜單選擇大項,中間列表會選擇須要的組件,右側是已選的組件列表微信

 

 5.輸入項目名和模塊名app

 

 6.項目結構以下ide

 

 

 7. 查看咱們依賴的pom.xml 裏面須要手動添加一個咱們公共組件的依賴,由於controller裏面要用到實體類spring-boot

 <dependencies> <!--引入咱們的公共組件--> <dependency> <groupId>cn.kgc</groupId> <artifactId>eureka-common-school</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>

 

 

8.編輯咱們的屬性文件微服務

#eureka的相關配置 #使用feign時報錯Service id not legal hostname(xx_sss) #緣由是feign不支持下劃線"_",支持"-",改爲xx-sss便可 #spring.application.name表示當前微服務註冊到Eureka Server中的名字,同事須要制定Eureka Server地址 spring.application.name=client-student-findstudata server.port=8764 eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/ # 將feign集成的斷路器設置成有效狀態 feign.hystrix.enabled=true

 

9.在cn.kgc.feign包下編寫StudentFeign業務接口,好多人在這裏添加service註解,木有看懂,可是我這裏也能拿到數據

package cn.kgc.feign; import cn.kgc.vo.Classes; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import java.util.List; import java.util.Map; /*FeignClient的name屬性值和eureka_client_product的appliaction屬性文件找那個的spring.application.name保持一致 fallback屬性指定容錯處理類 springcloud默認已經爲feign整合了hystrix,只要hystrix在項目中,使用feign就會 默認使用熔斷器處理所欲的請求 熔斷器模式相似於生活中的電路保險絲,當電流抄在可能銀帆危險時就會自動斷開,使用熔斷器模式, 若是請求出現異常,全部的請求都會直接返回而不會等待或阻塞,這樣能夠減小資源的浪費。 熔斷器還有一種半開的狀態,當熔斷器發現異常後悔進入半打開狀態,此時會定時接受 一個請求來檢測系統是否恢復,若是請求調用成功,表明系統已經恢復正常,救護關掉熔斷器, 不然繼續打開*/ @FeignClient(name="client-school-provider",fallback = StudentFeignFallBack.class) public interface StudentFeign { //下面的調用接口標準要和eureka-client-provider中的controller請求方法必須保持一致 @RequestMapping("/data.do") public String stuData(); }

 

 

10..在cn.kgc.feign包下編寫StudentFeignFallBack容錯處理類

package cn.kgc.feign; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; //容錯處理類 @Component public class StudentFeignFallBack implements StudentFeign{ @Override public String stuData() { return "服務器異常,請稍後在嘗試登錄"; } }

 

11.在cn.kgc.controller包下編寫StudentController控制器類

package cn.kgc.controller; import cn.kgc.feign.StudentFeign; import cn.kgc.vo.Classes; import cn.kgc.vo.Student; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; import java.util.Map; @RestController public class StudentController { @Autowired private StudentFeign studentFeign; @RequestMapping("/studata.do") public String showOptionsData(){ return studentFeign.stuData(); } }

 

12.編輯啓動類

package cn.kgc; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients; //EnableFeignClient開啓Feign聲明式REST調用 //EnableEurekaClient開啓註冊中心客戶端 @EnableFeignClients @EnableEurekaClient @SpringBootApplication public class EurekaClientFindstudataApplication { public static void main(String[] args) { SpringApplication.run(EurekaClientFindstudataApplication.class, args); } }

 

13.先正常依次啓動服務eureka-server,eureka-provider,eureka-client-findstudata ,而後查看註冊中心,看下是否後2個客戶端註冊到服務中心

看到以下兩個服務就說明啓動註冊成功啦

 

14.因爲數據是由eureka-provider提供,eureka-client-findstudata 調用了eureka-provider 的數據,在以上服務正常啓動的狀況下,能夠獲得以下數據

 

 

 15.同理,因爲數據是由eureka-provider提供,如今停掉eureka-provider服務, 則此時咱們eureka-client-findstudata 的數據調用不到,feign的熔斷器發揮做用

 

 

此時咱們的程序就走通啦!

 總結::其實吧,Holly我的覺的用OOP的思想很好理解,就是咱們在JavaOOP寫代碼的時候有try-cathch  ,容錯類就是調用某個方法報錯是拋出異常的自定義信息類而已!也許這個想法不對,可是我的覺的原理相似,脖子要斷了,回家睡美容覺了,若有問題,請QQ/微信:964918306  

 

 

此帖子爲原創

做者:紅酒人生

轉載請註明出處:https://www.cnblogs.com/holly8/p/11024097.html

相關文章
相關標籤/搜索