1. 程式人生 > >【微服務架構】SpringCloud之Hystrix斷路器(六)

【微服務架構】SpringCloud之Hystrix斷路器(六)

SpringCloud Hystrix

一:什麼是Hystrix

在分散式環境中,許多服務依賴項中的一些將不可避免地失敗。Hystrix是一個庫,通過新增延遲容差和容錯邏輯來幫助您控制這些分散式服務之間的互動。Hystrix通過隔離服務之間的訪問點,停止其間的級聯故障以及提供回退選項,從而提高系統的整體彈性。

Hystrix旨在執行以下操作

1:對通過第三方客戶端庫訪問(通常通過網路)的依賴關係提供保護並控制延遲和故障。

2:隔離複雜分散式系統中的級聯故障。

3:快速發現故障,儘快恢復。

4:回退,儘可能優雅地降級。

5:啟用近實時監控,警報和操作控制。

二:為什麼需要Hystrix?

大型分散式系統中,一個客戶端或者服務依賴外部服務,如果一個服務宕了,那麼由於我們設定了服務呼叫系統超時時間,勢必會影響相應時間,在高併發的情況下大多數伺服器的執行緒池就出現阻塞(BLOCK),影響整個線上服務的穩定性。

(圖片官方圖片)

當一切都健康時,請求可以看起來像這樣

當許多後端服務系統中的一個宕掉時,整個使用者請求:


如果多個客戶端呼叫同一個異常服務的時候,出現的情況是:

三:Hystrix解決什麼問題?

分散式架構中的應用程式具有幾十個依賴關係,每個依賴關係在某個時刻將不可避免的出現異常。如果應用程式不與這些外部故障隔離,則可能出現執行緒池阻塞,引起系統雪崩。

例如,對於依賴30個服務的應用程式,每個服務的正常執行時間為99.99%,您可以:
99.99%的30次方 = 99.7%正常執行時間
0.3%的10億次請求= 3,000,000次故障
2+小時宕機/月,即使所有依賴關係正常執行時間。

當使用Hystrix進行熔斷後,每個依賴關係彼此隔離了,限制了當發生延遲時的阻塞。

四:Hystrix結合Feign使用

建立一個工程eureka_feign_hystrix_client

pom.xml檔案內容

<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-feign</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Brixton.SR5</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

建立啟動檔案

FeignHystrixApplication

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class FeignHystrixApplication {

    public static void main(String[] args) {
        SpringApplication.run(FeignHystrixApplication.class, args);
    }

}

UserClient類

@FeignClient(value = "biz-service-0",fallback = UserClientHystrix.class)
public interface UserClient {

    @RequestMapping(method = RequestMethod.GET, value = "/getuser")
    public User getuserinfo();
    
    @RequestMapping(method = RequestMethod.GET, value = "/getuser")
    public String getuserinfostr();
    
    @RequestMapping(method = RequestMethod.GET, value = "/info")
    public  String  info();

}

建立熔斷類UserClientHystrix

@Service
public class UserClientHystrix implements UserClient {

    @Override
    public User getuserinfo() {
        throw new NullPointerException(" User getuserinfo() 服務不可用。。");
    }

    @Override
    public String getuserinfostr() {

        return " UserClientHystrix getuserinfostr() is fallback 服務不可用。。";
    }

    @Override
    public String info() {

        return " UserClientHystrix info() is fallback 服務不可用。。";
    }

}

當網路出現異常的時候或直接跳轉到這裡實現類裡面

建立action類

UserController

@Autowired
    UserClient userClient;

    @RequestMapping(value = "/getuserinfo", method = RequestMethod.GET)
    public User getuserinfo() {
        return userClient.getuserinfo();
    }
    
    @RequestMapping(value = "/getuserinfostr", method = RequestMethod.GET)
    public String getuserinfostr() {
        return userClient.getuserinfostr();
    }
    
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public String info() {
        return userClient.info();
    }

先啟動:eureka_register_service(註冊中心)工程

然後執行我們寫好的FeignHystrixApplication

這個時候我們明顯發現沒有執行biz-service-0 服務,那麼我們 開啟 http://127.0.0.1:8005/getuserinfostr

出現

UserClientHystrix getuserinfostr() is fallback 服務不可用。。

這個就是我們自定義的熔斷返回結果

如果不用熔斷 頁面會出現這個

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Wed Mar 22 14:32:21 CST 2017
There was an unexpected error (type=Internal Server Error, status=500).
getuserinfo failed and fallback failed.

本人也看了一些Hystrix相關原理,由於沒有全部看完所以暫時沒有寫上去,本文是結合Feign使用和學習。

有興起的可以看下官方的Hystrix很詳細,就是看起來有點費勁,

文章來源:http://bbs.tulingxueyuan.cn/forum.php?mod=viewthread&tid=25&extra=