天天看點

Spring Cloud微服務(5)之Hystrix斷路器

1.介紹

熔斷器 Hystrix 是容錯管理工具,作用是通過隔離、控制服務進而對延遲和故障提供更強大的容錯能力,避免整個系統被拖垮。

複雜分布式架構通常都具有很多依賴,當一個引用高度耦合其他服務時非常危險且容易導緻失敗,這種傷害很容易傷害服務調用者,最後導緻一個接一個的連續錯誤,應用本

身就處在被拖垮的風險中,最後失去控制,就像在一個高流量的網站中,某個單一的後端一旦發生延遲,将會在很短時間内導緻所有應用資源被耗盡。如何處理這些問題是有關

系統性能和效率的關鍵性問題。

當在系統高峰時期,大量對微服務的調用可能會堵塞遠端伺服器的線程池,如果這個線程池沒有和主伺服器的線程池隔離,就可能會拖垮整個伺服器。Hystrix 使用自己的

線程池,這樣和主應用伺服器線程池隔離,如果調用會花費很長時間,會終止調用,不同的指令和指令組能夠被使用它們各自的線程池,隔離不同服務。

2.如何使用

通過 Netflix Hystrix 實作斷路器容錯的功能,使用Fallback方法為熔斷或異常提供備用方案進行處理。

涉及到的工程:eureka-sever、product-service、order-service-hystrix。(前兩個工程前面章節已講解,最後一個自行建立)

驗證使用 order-service-hystrix 調用 product-service,正常情況下傳回産品服務的結果,當product-service服務無法通路時,立刻調用復原方法傳回結果。

order-service-hystrix目錄結構如圖:

Spring Cloud微服務(5)之Hystrix斷路器

(1)pom.xml增加 Hystrix依賴。

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

(2)啟動類增加注解 @EnableCircuitBreaker,開啟斷路器功能。

package com.hole;

import com.hole.web.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
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.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@EnableDiscoveryClient
@SpringBootApplication
@RestController
@EnableCircuitBreaker
public class OrderServiceHystrixApplication {
	
	@Autowired
	private ProductService productService;
	
	@Bean
	@LoadBalanced
	RestTemplate restTemplate(){
		return new RestTemplate();
	}

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

	@RequestMapping(value = "/hello",method = RequestMethod.GET)
	public ResponseEntity<String> hello(){
		return  new ResponseEntity<String>("hello order service hystrix", HttpStatus.OK);
	}

	@RequestMapping(value = "/product/hello",method = RequestMethod.GET)
	public String helloProductService(){
		return productService.helloService();
	}
}
           

(3)增加 ProductService 類,通過它來調用 product-service服務,當調用不通過立即調用復原方法。

package com.hole.web;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * Created by helei on 2017/7/16.
 */
@Service
public class ProductService {
    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "productServiceFallback")
    public String helloService(){
        return restTemplate.getForEntity("http://PRODUCT-SERVICE/hello",String.class).getBody();
    }

    public String productServiceFallback(){
        return "product服務不見了,稍後再試";
    }
}
           

(4)啟動。

三個工程啟動後,檢視監控界面。

Spring Cloud微服務(5)之Hystrix斷路器

(5)驗證。

通路order-service-hystrix,這時通過order-service-hystrix可以調用 product-service,并且傳回正确結果。如圖:

Spring Cloud微服務(5)之Hystrix斷路器

然後停掉 product-service 服務,再次通路。這次通路會得到復原方法的結果。

Spring Cloud微服務(5)之Hystrix斷路器

繼續閱讀