1. 程式人生 > 實用技巧 >springcloud之Hystrix

springcloud之Hystrix

Hystrix斷路器

什麼是hystrix?

Hystrix是一個用來處理分散式系統的延遲和容錯的開源庫,在分散式系統裡,很多依賴會不可避免的呼叫失效,比如超時、異常。Hystrix能夠保證一個依賴出現問題時,不會導致服務整體失效,避免級聯故障,以提高分散式系統的彈性。

“斷路器”本身是一種開關裝置,當某個服務單元發生故障之後,通過斷路器的故障監控,向呼叫方法返回一個服務預期的,可處理的備選響應(fallback),而不是長時間的等待或是跑出異常,這樣可以保證服務呼叫的執行緒不會長時間的被佔用。不必要的佔用,從而避免了故障在分散式系統中的蔓延。

Hystrix的功能

Hystrix的功能主要有:服務降級、服務熔斷、服務限流以及接近實時的監控。

服務降級:服務降級是指在服務壓力劇增時,根據當前業務的情況有選擇性的對一些服務有策略的降級,減輕伺服器的壓力,保證核心任務的執行。

服務熔斷:服務熔斷是指由於某些原因使服務出現故障,為了防止整個系統故障,從而採取的一種保護措施,也稱之為過載保護。

服務限流:服務限流是指對併發訪問進行限速來保護系統。

監控:HystrixCommand和HystrixObservableCommand在執行時,會生成執行結果和執行指標,比如每秒執行的請求數和成功數。

springcloud關於hystrix的應用

(1)consumer模組中新增依賴

    <dependency>
            <
groupId>com.netflix.hystrix</groupId> <artifactId>hystrix-core</artifactId> </dependency> <dependency> <groupId>com.netflix.hystrix</groupId> <artifactId>hystrix-javanica</artifactId> </
dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>com.netflix.hystrix</groupId> <artifactId>hystrix-metrics-event-stream</artifactId> </dependency>

(2)consumer01模組中建立IndexApi介面

@FeignClient(value = "SEARCH")
public interface IndexApi {
    @RequestMapping(value = "/index")
    public String index();
}

(3)該介面的具體的實現在producet01模組下的ProducerController中,此時使用@HystrixCommand(fallbackMethod = "index2")註解可實現服務熔斷功能。即當index()方法出現異常時,可呼叫index2()進行處理。

@RestController
public class ProducerController {
    @RequestMapping(value = "/index")
    @HystrixCommand(fallbackMethod = "index2")//熔斷 呼叫fun2
    public String index(){
        int a= 1/0;  //服務熔斷 處理過程在服務端  ,服務降級處理邏輯在客戶端。
        String index = "producer01.......";
        System.out.println("producer01.......");
        return index;
    }

    public String index2(){
        System.out.println("index2");
        return "index2";
    }

啟動以上服務,訪問localhost:8086/index可以看見訪問了index2方法。

(4)在IndexApi介面中新增引數fallbackFactory = IndexApiHystrix.class

@FeignClient(value = "SEARCH",fallbackFactory = IndexApiHystrix.class)
public interface IndexApi {
    @RequestMapping(value = "/index")
    public String index();
}

(5)服務降級實現,在consumer01模組中,建立IndexApiHystrix類實現FallbackFactory介面。

@Component
public class IndexApiHystrix implements FallbackFactory {
    @Override
    public IndexApi create(Throwable throwable) {

        IndexApi indexApiHystrix  =  new IndexApi() {
           @Override
           public String index() {
               return "服務降級呼叫index方法";
           }
       };
        return indexApiHystrix ;
    }

(6)consumer01啟動類,新增@EnableCircuitBreaker註解

@SpringBootApplication
@EnableFeignClients //feign服務呼叫
@EnableDiscoveryClient
@EnableCircuitBreaker
public class Consumer01Application {

    public static void main(String[] args) {
        SpringApplication.run(Consumer01Application.class, args);
    }
    @Bean
    public ServletRegistrationBean hys(){
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
        servletRegistrationBean.addUrlMappings("/actuator/hystrix.stream");
        return servletRegistrationBean;
    }
}

註釋掉ProducerController的註解@HystrixCommand(fallbackMethod = "index2")啟動服務,訪問url得到結果: