Open-Feign整合hystrix降級熔斷實戰(zhàn)記錄
一、服務(wù)端
1、配置文件
application.yml
server:
port: 9000
spring:
application:
name: my-test2 #服務(wù)的名稱
2、控制層
@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());
}
}
二、客戶端
1、依賴
<!--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>
2、配置文件
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ò)狀況正常的情況下,兩端連接所用的時間,處理請求的超時時間,默認為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
3、啟動類
@SpringBootApplication
@EnableFeignClients//開啟open-feign
@EnableHystrix//開啟降級熔斷服務(wù)
public class MyTestApplication1 {
public static void main(String[] args) {
SpringApplication.run(MyTestApplication1.class,args);
}
}
4、在控制層當(dāng)中調(diào)用
@RestController
public class TestController1 {
@Autowired
TestService1 testService1;
@RequestMapping("/myTestBuy1")
public String myTestBuy2(){
return testService1.myTestBuy2();
}
}
5、創(chuàng)建一個類實現(xiàn)服務(wù)FeignClient接口
@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());
}
}
6、在服務(wù)FeignClient接口上配置FallBack實現(xiàn)類
@FeignClient(name = "my-test2", url = "${my-test2.url}", fallback = MyHystrix1.class)
public interface TestService1 {
@RequestMapping("/myTestBuy2")
String myTestBuy2();
}
三、測試
1、場景一服務(wù)正常調(diào)用

2、場景二當(dāng)被調(diào)服務(wù)停止運行時
只給兩秒的時間,則自動啟動熔斷


3、場景三當(dāng)調(diào)取服務(wù)超時時
熔斷時間根據(jù)hystrix設(shè)置的時間,我這里設(shè)置的是5秒


4、其他

到此這篇關(guān)于Open-Feign整合hystrix降級熔斷實戰(zhàn)記錄的文章就介紹到這了,更多相關(guān)Open-Feign整合hystrix降級熔斷內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Session實現(xiàn)分布式session的簡單示例
本篇文章主要介紹了Spring Session實現(xiàn)分布式session的簡單示例,具有很好的參考價值。下面跟著小編一起來看下吧2017-05-05
深入解析JVM之內(nèi)存結(jié)構(gòu)及字符串常量池(推薦)
Java作為一種平臺無關(guān)性的語言,其主要依靠于Java虛擬機——JVM,接下來通過本文給大家介紹JVM之內(nèi)存結(jié)構(gòu)及字符串常量池的相關(guān)知識,需要的朋友可以參考下2020-07-07
IDEA下Servlet可能出現(xiàn)404的一些情況
相信有很多小伙伴遇到報錯都不知道怎么處理,今天特地整理了這篇文章,文中對IDEA下Servlet可能出現(xiàn)404的一些情況作了詳細的介紹,需要的朋友可以參考下2021-06-06
SpringBoot 使用Prometheus采集自定義指標(biāo)數(shù)據(jù)的方案
這篇文章主要介紹了SpringBoot 使用Prometheus采集自定義指標(biāo)數(shù)據(jù),我們在k8s集群成功搭建了Prometheus服務(wù),今天,我們將在springboot2.x中使用prometheus記錄指標(biāo),需要的朋友可以參考下2022-10-10

