您好,登錄后才能下訂單哦!
這篇文章主要介紹Open-Feign整合hystrix降級熔斷的示例分析,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!
application.yml
server: port: 9000 spring: application: name: my-test2 #服務(wù)的名稱
@RestController public class ShoppingController { @RequestMapping("/myTestBuy2") public String myTestBuy2(){ //用來模擬服務(wù)超時 try { Thread.sleep(6000); } catch (InterruptedException e) { e.printStackTrace(); } return "購買成功——時間:"+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); } }
<!--Open-Feign依賴--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <!--hystrix依賴--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
server: port: 8000 spring: application: name: my-test1 #服務(wù)的名稱 #允許服務(wù)降級配置 feign: hystrix: enabled: true #自定義ribbon的超時時間 設(shè)置的要比hystrix-timeoutInMilliseconds超時時間大 ribbon: #指的是建立連接后從服務(wù)器讀取到可用資源所用的時間。 ReadTimeout: 10000 #指的是建立連接所用的時間,適用于網(wǎng)絡(luò)狀況正常的情況下,兩端連接所用的時間,處理請求的超時時間,默認(rèn)為5秒。 ConnectTimeout: 10000 hystrix: command: default: execution: isolation: thread: #feign整合hystrix 光設(shè)置Hystrix超時沒用的 要配合ribbon超時 timeoutInMilliseconds: 5000 my-test2: url: http://127.0.0.1:9000
@SpringBootApplication @EnableFeignClients//開啟open-feign @EnableHystrix//開啟降級熔斷服務(wù) public class MyTestApplication1 { public static void main(String[] args) { SpringApplication.run(MyTestApplication1.class,args); } }
@RestController public class TestController1 { @Autowired TestService1 testService1; @RequestMapping("/myTestBuy1") public String myTestBuy2(){ return testService1.myTestBuy2(); } }
@Component public class MyHystrix1 implements TestService1 { @Override public String myTestBuy2() { return "調(diào)用失敗,該服務(wù)被熔斷——時間:"+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); } }
@FeignClient(name = "my-test2", url = "${my-test2.url}", fallback = MyHystrix1.class) public interface TestService1 { @RequestMapping("/myTestBuy2") String myTestBuy2(); }
只給兩秒的時間,則自動啟動熔斷
熔斷時間根據(jù)hystrix設(shè)置的時間,我這里設(shè)置的是5秒
以上是“Open-Feign整合hystrix降級熔斷的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。