溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Open-Feign整合hystrix降級熔斷的示例分析

發(fā)布時間:2021-09-05 10:51:43 來源:億速云 閱讀:194 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹Open-Feign整合hystrix降級熔斷的示例分析,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!

一、服務(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ò)狀況正常的情況下,兩端連接所用的時間,處理請求的超時時間,默認(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

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)建一個類實(shí)現(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實(shí)現(xiàn)類

@FeignClient(name = "my-test2", url = "${my-test2.url}", fallback = MyHystrix1.class)
public interface TestService1 {
    @RequestMapping("/myTestBuy2")
    String myTestBuy2();
}

三、測試

1、場景一服務(wù)正常調(diào)用

Open-Feign整合hystrix降級熔斷的示例分析

2、場景二當(dāng)被調(diào)服務(wù)停止運(yùn)行時

只給兩秒的時間,則自動啟動熔斷

Open-Feign整合hystrix降級熔斷的示例分析
Open-Feign整合hystrix降級熔斷的示例分析

3、場景三當(dāng)調(diào)取服務(wù)超時時

熔斷時間根據(jù)hystrix設(shè)置的時間,我這里設(shè)置的是5秒

Open-Feign整合hystrix降級熔斷的示例分析

Open-Feign整合hystrix降級熔斷的示例分析

4、其他

Open-Feign整合hystrix降級熔斷的示例分析

以上是“Open-Feign整合hystrix降級熔斷的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(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)容。

AI