天天看點

Spring cloud學習筆記5-斷路器Hystrix1. 概述2. Ribbon的斷路器3. Feign的斷路器

斷路器Hystrix

  • 1. 概述
  • 2. Ribbon的斷路器
    • 2.1 引入包
    • 2.2 啟動類引入hystrix
    • 2.3 服務調用類配置
  • 3. Feign的斷路器
    • 3.1 啟用配置
    • 3.2 feign接口配置
    • 3.3 後備類
    • 3.4 配置說明

1. 概述

在微服務架構中,根據業務來拆分成一個個的服務,服務與服務之間可以互相調用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign來調用。為了保證其高可用,單個服務通常會叢集部署。由于網絡原因或者自身的原因,服務并不能保證100%可用,如果單個服務出現問題,調用這個服務就會出現線程阻塞,此時若有大量的請求湧入,Servlet容器的線程資源會被消耗完畢,導緻服務癱瘓。服務與服務之間的依賴性,故障會傳播,會對整個微服務系統造成災難性的嚴重後果,這就是服務故障的“雪崩”效應。為了解決這個問題,spring cloud提供斷路器模型hystrix。

Spring cloud的hystrix有3種模式的斷路器:

1) 斷路器模式:遠端服務不可用時,快速失敗。

2) 後備模式:遠端服務不可用時,使用替代方案。

3) 艙壁模式:不同遠端服務使用不同線程池,不會導緻一個遠端服務不可用,整個服務挂掉。

2. Ribbon的斷路器

2.1 引入包

利用前面service-ribbon工程《服務消費方Ribbon》中,在pom中引入hystrix的jar。

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

           

2.2 啟動類引入hystrix

在啟動類ServiceRibbonApplication.java中,引入注解@EnableHystrix即可。下面代碼紅色标記處。

package org.service.ribbon;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableHystrix
public class ServiceRibbonApplication {
    public static void main(String[] args) {
        SpringApplication.run( ServiceRibbonApplication.class, args );
        
        HelloService s = new HelloService();
       System.out.println(s.hiService("linwu"));
    }
    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

           

2.3 服務調用類配置

服務調用類中,在調用方法上面增加注解@HystrixCommand,可以配置包括後備模式、艙壁模式等屬性,下面代碼使用一個後備模式(屬性fallbackMethod,配置後備模式的方法,方面入參和出參要與服務調用一樣。本例子中後備方法為:hiError):

package org.service.ribbon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
@Service
public class HelloService {
    @Autowired
    RestTemplate restTemplate;
    @HystrixCommand(fallbackMethod = "hiError")
    public String hiService(String name) {
        return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
    }
    public String hiError(String name) {
        return "hi,"+name+",sorry,error!" +";this feign!";
    }
}

           

3. Feign的斷路器

3.1 啟用配置

Feign是自帶斷路器的,在D版本的Spring Cloud之後,它沒有預設打開。需要在配置檔案中配置打開它。利用前面service-feign工程《服務消費方Feign》在配置檔案application.yml加以下代碼:

feign: 
  hystrix: 
enabled: true //feign開啟hystrix支援

           

3.2 feign接口配置

在接口的注解上面增加fallback屬性即可。Fallback屬性對應一個java類,此類實作feign接口。代碼如下:

package org.service.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(value = "service-hi",fallback = SchedualServiceHiHystric.class)
public interface SchedualServiceHi {
    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    String sayHiFromClientOne(@RequestParam(value = "name") String name);
}

           

3.3 後備類

編寫一個後備類SchedualServiceHiHystric.java。實作feign接口。代碼如下:

package org.service.feign;
import org.springframework.stereotype.Component;
@Component
public class SchedualServiceHiHystric implements SchedualServiceHi {
    @Override
    public String sayHiFromClientOne(String name) {
        return "sorry "+name +";this feign!";
    }
}

           

3.4 配置說明

1) 使用commandProperties屬性可以配置斷路器的屬性(逾時、次數等)

2) 後備模式可以使用fallback的配置。

3) 艙壁模式使用threadPoolKey和threadPoolProperties屬性配置線程池。

4) 使用commandPoolProperties配置線程池的斷路屬性。

本文參考文獻:https://blog.csdn.net/forezp/article/details/70148833/

繼續閱讀