1. 程式人生 > >Spring Cloud構建微服務架構(四)斷路器(Hystrix)

Spring Cloud構建微服務架構(四)斷路器(Hystrix)

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

為了解決這個問題,業界提出了斷路器模型。

1.斷路器簡介

Netflix開源了Hystrix元件,實現了斷路器模式,SpringCloud對這一元件進行了整合。 在微服務架構中,一個請求需要呼叫多個服務是非常常見的,如下圖:


較底層的服務如果出現故障,會導致連鎖故障。當對特定的服務的呼叫的不可用達到一個閥值(Hystric 是5秒20次) 斷路器將會被開啟。


斷路開啟後,可用避免連鎖故障,fallback方法可以直接返回一個固定值。

2.前期準備

啟動eureka-server 工程;啟動service-hi工程,它的埠為8762   

3.在Ribbon中使用斷路器

3.1加入依賴:

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

3.2在服務啟動類加入@EnableHystrix註解

package test;

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.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
public class ServiceRibbonApplication { public static void main(String[] args) { SpringApplication.run(ServiceRibbonApplication.class, args); } @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } }
3.3改造HelloService類
在hiService方法上加上@HystrixCommand註解。該註解對該方法建立了熔斷器的功能,並指定了fallbackMethod熔斷方法,熔斷方法直接返回了一個字串,字串為”hi,”+name+”,sorry,error!”,程式碼如下
package test;

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!";
	    }
}
訪問:
http://localhost:8764/hi?name=heidou
出現:
hi heidou,i am from port:8762
關閉service-hi服務後再次訪問出現:
hi ,heidou,sorry,error!
4.Feign中使用斷路器

4.1Feign是自帶斷路器的,需要在配置檔案裡面開啟:

feign:
hystrix:
enabled: true

4.2在FeignClient的SchedualServiceHi介面的註解中加上fallback的指定類

package test;
import org.springframework.cloud.netflix.feign.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);

}
package test;
import org.springframework.stereotype.Component;

@Component
public class SchedualServiceHiHystric implements SchedualServiceHi {
    
	public String sayHiFromClientOne(String name) {
		// TODO Auto-generated method stub
		return "sorry "+name;
	}
 
}
4.3測試

⑴當不啟動service-hi服務時,
輸出:

sorry heidou
⑵啟動service-hi服務時:

輸出:

hi heidou,i am from port:8762

5.Hystrix Dashboard (斷路器:Hystrix 儀表盤)
我們在Ribbion主啟動類加上@EnableHystrixDashboard註解

package test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
@EnableHystrixDashboard
public class ServiceRibbonApplication {
	 public static void main(String[] args) {
	        SpringApplication.run(ServiceRibbonApplication.class, args);
	    }

	    @Bean
	    @LoadBalanced
	    RestTemplate restTemplate() {
	        return new RestTemplate();
	    }
}
然後訪問http://localhost:8764/hystrix,介面如下:

進去後頁面: