一、Hystrix簡介
Hystrix是Netflix開源的一款容錯框架,包含常用的容錯方法:線程隔離、信號量隔離、降級策略、熔斷技術。在高併發訪問下,系統所依賴的服務的穩定性對系統的影響非常大,依賴有很多不可控的因素,比如網絡連接變慢,資源突然繁忙,暫時不可用,服務脫機等。我們要構建穩定、可靠的分佈式系統,就必須要有這樣一套容錯方法。
本篇文章基於前一篇文章的工程
二:使用Feign調用服務中使用Hystrix
首先要導入pom依賴
假如你的SpringBoot的版本是2.0以下的版本,那麼pom文件當中只需要導入如下依賴:
<!--spring-cloud-starter-hystrix的起步依賴-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
假如你的版本是SpringBoot2.0+的版本,那麼,我們需要導入的pom依賴如下,否則會出現找不到類的情況,比如出現@HystrixCommand找不到類的情況。
<!--spring-cloud-starter-hystrix的起步依賴-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-javanica</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-core</artifactId>
<version>RELEASE</version>
</dependency>
Feign是自帶斷路器的,但是它沒有默認打開。需要在配置文件中配置打開它,在application.yml文件中加以下代碼:
#feign.hystrix.enabled=true
feign:
hystrix:
enabled: true
原來我們調用服務只需要寫一個接口,但是現在我們需要在接口上進行改造
@FeignClient(value = "mall-wechatservice",fallback = HelloServiceHystrix.class)
public interface HelloServiceRestful {
/**
* 請求mall_wechatservice模塊的helloA
* @return
*/
@RequestMapping("/helloA")
public String getInfoFromWechatA();
/**
* 請求mall_wechatservice模塊的helloB
* @return
*/
@RequestMapping("/helloB")
public String getInfoFromWechatB(@RequestBody String json);
/**
* 請求mall_wechatservice模塊的helloC
* @return
*/
@RequestMapping("/helloC")
public String getInfoFromWechatC(@RequestBody Map<String, Object> params);
}
fallback=xxx.class,這個class就是基於這個接口的實現類
@Component
public class HelloServiceHystrix implements HelloServiceRestful{
@Override
public String getInfoFromWechatA() {
return "sorry,errorgetInfoFromWechatA";
}
@Override
public String getInfoFromWechatB(String json) {
return "sorry,errorgetInfoFromWechatB";
}
@Override
public String getInfoFromWechatC(Map<String, Object> params) {
return "sorry,errorgetInfoFromWechatC";
}
}
我們基於這個接口有一個實現類,並且我們打開了熔斷機制,當服務器檢測到服務無法正常執行之後,就會返回這個方法裏面的返回值。我們先正常開啓服務,然後再關閉服務,可以看到打印的信息如下:說明剛開始正常返回,關閉服務之後按照我們事先的方法返回。
三:使用RestTemplate調用服務中使用Hystrix
首先按照上一步的要求導入pom文件
在主類上加@EnableHystrix註解開啓Hystrix
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableEurekaClient
@SpringBootApplication
@EnableFeignClients
@EnableHystrix
public class MallManagerServiceApplication {
public static void main(String[] args) {
SpringApplication.run(MallManagerServiceApplication.class, args);
}
}
在Mall_ManagerService模塊中編寫服務調用方法,調用Mall_WechatService裏面的三個服務。我們可以看到方法上加上了@HystrixCommand註解。該註解對該方法創建了熔斷器的功能,並指定了fallbackMethod熔斷方法。
@Service
public class HelloService {
private final Logger logger= LoggerFactory.getLogger(this.getClass());
@Autowired
private RestTemplate restTemplate;
String host = "http://mall-wechatservice";
@HystrixCommand(fallbackMethod = "hystrixError")
public String hystrix() {
String url = host+"/helloA";
String text = restTemplate.getForObject(url,String.class);
logger.info("接口正常返回的信息是:"+text);
return text;
}
public String hystrixError() {
logger.info("服務已經發生了中斷了");
return "sorry,error!";
}
}
在Controller當中使用該類去調用服務
@Controller
public class HelloController {
private final Logger logger= LoggerFactory.getLogger(this.getClass());
@Autowired
private HelloService helloService;
@ResponseBody
@RequestMapping("/sayHelloAA")
public String getFromWechatA(){
String text = helloService.hystrix();
return text;
}
}
我們先正常開啓Mall_WechatService和Mall_ManagerService,請求/sayHelloAA,然後再關閉Mall_WechatService服務,再請求一次/sayHelloAA,可以看到控制檯打印如下信息:第一次正常返回,第二次提示出現了服務中斷。