1. 程式人生 > >SpringCloud學習 - Hystrix服務容錯 - 服務熔斷降級

SpringCloud學習 - Hystrix服務容錯 - 服務熔斷降級

書籤:

1、小小例子

2、指定Class回退

 

  1. 小小例子

A服務訪問B服務,B服務可能超時或者響應異常,A服務停止對B的訪問(1小段時間),呼叫自定義Back方法,而不等死或拋異常,堵資源。

pom.xml

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

 

接著eureka-consumer-feign專案時行修改:

啟動類EurekaConsumerFeignApplication.java主要添加註解@EnableCircuitBreaker和@HystrixCommand

package cn.ywj.eurekaconsumerfeign;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
@EnableFeignClients
@EnableCircuitBreaker
public class EurekaConsumerFeignApplication {

   @Autowired
   private FeignTest feignTest;

   @RequestMapping("/t")
   public String t(){
      return feignTest.t();
   }

   @RequestMapping("/t2")
   public String t2(User user){
      User user1 = feignTest.t2(user);
      return user1 == null ? "null" : user1.getName()+":"+user1.getAge();
   }

   @RequestMapping("/t3")
   public String t3(User user){
      User user1 = feignTest.t3(user.getName(), user.getAge());
      return user1 == null ? "null" : user1.getName()+":"+user1.getAge();
   }

   @RequestMapping("/t5")
   public String t5(User user){
      User user1 = feignTest.t5(user);
      return user1 == null ? "null" : user1.getName()+":"+user1.getAge();
   }

   @RequestMapping("/t6")
   @HystrixCommand(fallbackMethod = "fallbackMethod")//服務熔斷後會呼叫fallbackMethod方法
   public String t6(){
      return feignTest.t6();
   }

   public String fallbackMethod(){
      return "t6 from fallbackMethod";
   }

   public static void main(String[] args) {
      SpringApplication.run(EurekaConsumerFeignApplication.class, args);
   }
}

 

訪問t6方法,當provider正常跑時,可以返回相關的結果,當把provider關閉後,返回的資訊是”t6 from fallbackMethod”.

 

  1. 指定Class回退

建立一個類FeiginHystrixTest.java 實現介面 FeignTest.java

package cn.ywj.eurekaconsumerfeign;

import org.springframework.stereotype.Component;

@Component
public class FeiginHystrixTest implements  FeignTest{
    @Override
    public String t() {
        return null;
    }

    @Override
    public User t2(User user) {
        return null;
    }

    @Override
    public User t3(String name, Integer age) {
        return null;
    }

    @Override
    public User t5(User user) {
        return null;
    }

    @Override
    public String t6() { // 回退方法
        return "msg from FeiginHystrixTest";
    }
}

 

FeignTest.java上的@FeignClient註解修改如下:

@FeignClient(value = "eureka-provider",fallback = FeiginHystrixTest.class)// 指定要訪問的服務提供者與降級處理方法

 

原來的Controller變成普通的樣子

@RequestMapping("/t6")
public String t6(){
   return feignTest.t6();
}

 

application.yml新增配置

feign:
  hystrix:
   enabled: true #宣告 Feign 的 Hystrix 支援

 

完畢。

基佬網址:https://github.com/woshiyinweijian/SpringCloudTestV2