如題,本篇我們介紹下服務消費方Hystrix斷路器的使用。
注意,前面我們介紹的ribbon、feign都是基于用戶端代理(服務消費方)的負載均衡器,而伺服器端的負載均衡器在實際中可能會采用硬體負載,如F5或 LVS等裝置,本篇中介紹的Hystrix也是基于用戶端的技術,hystrix中實作了斷路器、線程隔離、信号隔離等功能,使用Hystirx可以有效地避免因某一個伺服器故障而導緻所有依賴該服務的服務都發生阻塞的"雪崩"效應。
好了,現在回到正題,本篇主要介紹下hystrix在Ribbon消費方和Feign消費方中的使用,以及hystrix-dashboard的使用
一、Ribbon消費方中使用hystrix
1、在消費方工程中,本例是sim-consumer工程中,pom.xml中引入hystrix斷路器起步依賴
<!-- hystrix 斷路器 起步依賴 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
2、在springboot啟動類中添加@EnableHystrix注解啟動Hystrix用戶端。
package com.tingcream.simConsumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
@ImportResource({"classpath:/spring.xml"})
@EnableDiscoveryClient
@EnableFeignClients //啟用Feign用戶端
@EnableHystrix//啟動hystrix用戶端
@EnableHystrixDashboard //啟動hystrixDashbord 儀表闆
public class SimConsumerApp extends SpringBootServletInitializer{
@Override
protected SpringApplicationBuilder configure(
SpringApplicationBuilder builder) {
return builder.sources(SimConsumerApp.class);
}
public static void main(String[] args) {
SpringApplication.run(SimConsumerApp.class, args);
}
}
3、在消費方接口方法中,使用@HystrixCommand(fallbackMethod = "xxx") 标記為一個hystrix方法,并指定失敗回退的掉用方法,如
在 HelloConsumerController 類中使用@HystrixCommand(fallbackMethod = "xxx") 标記其中hello、hello2方法
@GetMapping("/hello")
@HystrixCommand(fallbackMethod = "helloError")
public String hello(){
return restTemplate.getForObject(PRE_URL+"/hello", String.class);
}
public String helloError(){
return "抱歉,伺服器通路中斷了!";
}
@GetMapping("/hello2")
@HystrixCommand(fallbackMethod = "helloError2")
public String hello2(String name){
return restTemplate.getForObject(PRE_URL+"/hello?name="+name, String.class);
}
public String hello2Error(String name){
return "你好:"+name+",抱歉,伺服器通路中斷了!";
}
配置完成後,消費方通過hystrix通路服務提供方,當服務提供方服務不可用時,就會執行快速失敗(而不是等待線程、阻塞線程)的回退方法,即fallbackMethod 所指定的方法。
服務不可用時,通路
二、Feign消費方中使用hystrix
1、 application.yml中配置 feign.hystrix.enabled=true
Feign消費方中使用hystrix 比Ribbon的更簡單,因為Feign中本身就内置了hystrix斷路器,我們隻需要使用feign.hystrix.enabled=true 開啟之即可。
#feign用戶端啟動hystrix斷路保護
feign:
hystrix:
enabled: true
2、跟在Ribbon中一樣,在springboot啟動類上标記 @EnableHystrix注解,啟用hystrix 。
3、在消費方接口方法中,使用@FeignClient注解标記為一個hystrix方法,并指定value服務應用名稱、fallback失敗回退的調用的實作類。如 HelloFeignApi.java 和HelloFeignApiError.java
HelloFeignApi.java
package com.tingcream.simConsumer.clientApi;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(value = "sim-provider",fallback =HelloFeignApiError.class)
public interface HelloFeignApi {
@GetMapping( "/hello")
public String hello();
@GetMapping( "/hello2")
public String hello2(@RequestParam("name") String name);
}
HelloFeignApiError.java
package com.tingcream.simConsumer.clientApi;
import org.springframework.stereotype.Component;
import com.tingcream.simConsumer.clientApi.HelloFeignApi;
/**
* HelloFeignApi 失敗退回的調用方法
* @author jelly
*
*/
@Component
public class HelloFeignApiError implements HelloFeignApi {
@Override
public String hello() {
return "FeignApi 抱歉,伺服器通路中斷了!";
}
@Override
public String hello2(String name) {
return "FeignApi 你好:"+name+",抱歉,伺服器通路中斷了!";
}
}
測試通路的效果與Ribbon類似,當依賴的服務不可用時,會自動調用指定的接口實作類中的實作的方法。
三、hystrix-dashboard儀表闆
1、在通路消費方pom.xml中加入hystrix-dashboard的起步依賴
<!-- hystrix-dashboard 起步依賴 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
2、在springboot啟動類中,添加 @EnableHystrixDashboard 注解,啟動hystrixDashbord 儀表闆
3、通路消費方位址/hystrix ,本例中是http://localhost/hystrix/ 即可看到儀表闆首頁。