1. 程式人生 > >在eclipse上部署springcloud小例子--第三篇:斷路器(Hystrix)

在eclipse上部署springcloud小例子--第三篇:斷路器(Hystrix)

 

 

 

 

 

 

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

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


一、斷路器簡介

 

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

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

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

二、準備工作

這篇文章基於上一篇文章的工程,首先啟動上一篇文章的工程,啟動eureka-server 工程;啟動service-hi工程,它的埠為8762。

三、在ribbon使用斷路器

改造serice-ribbon 工程的程式碼,首先在pox.xml檔案中加入spring-cloud-starter-netflix-hystrix的起步依賴:
 

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

 

在程式的啟動類ServiceRibbonApplication 加@EnableHystrix註解開啟Hystrix:

package com.springcloud;

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

/**
 * 在工程的啟動類中,通過@EnableDiscoveryClient向服務中心註冊;
 * 並且向程式的ioc注入一個bean: restTemplate;
 * 並通過@LoadBalanced註解表明這個restRemplate開啟負載均衡的功能。
 * @author fengjinzhu
 *
 */
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableHystrix 
public class ServiceRibbonApplication {

	public static void main(String[] args) {
		SpringApplication.run(ServiceRibbonApplication.class, args);
	}
	
	@Bean
	@LoadBalanced
	RestTemplate restTemplate(){
		return new RestTemplate();
	}
}

 改造HelloService類,在hiService方法上加上@HystrixCommand註解。該註解對該方法建立了熔斷器的功能,並指定了fallbackMethod熔斷方法,熔斷方法直接返回了一個字串,字串為"hi,"+name+",sorry,error!",程式碼如下:

package com.springcloud;

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;


/**
 * 寫一個測試類HelloService,
 * 通過之前注入ioc容器的restTemplate來消費eureka-client服務的“/hi”介面,
 * 在這裡我們直接用的程式名替代了具體的url地址,
 * 在ribbon中它會根據服務名來選擇具體的服務例項,
 * 根據服務例項在請求的時候會用具體的url替換掉服務名
 * @author fengjinzhu
 *
 */
@Service
public class HelloService {

	@Autowired
	RestTemplate restTemplate;
	
	@HystrixCommand(fallbackMethod="hiError")
	public String hiService(String name){
		//return restTemplate.getForObject("http://Eureka-Client/hi?name="+name, String.class);
		return restTemplate.getForObject("http://eureka-client/hi?name="+name, String.class);
	}
	
	public String hiError(String name){
		return "hi,"+name+"!sorry,error";
	}
}

 啟動:service-ribbon 工程,當我們訪問http://localhost:8764/hi?name=springcloud,瀏覽器顯示:

hi ! springcloud,i am from port:8762

 當關閉eureka-client 工程,當我們訪問http://localhost:8764/hi?name=springcloud,瀏覽器顯示:

hi ,springcloud! sorry,error

這就說明當 service-hi 工程不可用的時候,service-ribbon呼叫 eureka-client的API介面時,會執行快速失敗,直接返回一組字串,而不是等待響應超時,這很好的控制了容器的執行緒阻塞。

四、Feign中使用斷路器

Feign是自帶斷路器的,在D版本的Spring Cloud之後,它沒有預設開啟。需要在配置檔案中配置開啟它,在配置檔案加以下程式碼:

# 開啟Feign開關  
feign:
  hystrix:
    enabled: true

 

 基於service-feign工程進行改造,只需要在FeignClient的SchedualEurekaClient介面的註解中加上fallback的指定類就行了:

package com.springcloud;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(value="eureka-client",fallback=SchedualEurekaClientHystric.class)
public interface SchedualEurekaClient {

	@RequestMapping(value="/hi",method=RequestMethod.GET)
	String sayHiFromClientOne(@RequestParam(value="name") String name);
}

 SchedualEurekaClientHystric需要實現SchedualEurekaClient介面,並注入到Ioc容器中,程式碼如下:

package com.springcloud;

import org.springframework.stereotype.Component;

@Component
public class SchedualEurekaClientHystric implements SchedualEurekaClient {

	@Override
	public String sayHiFromClientOne(String name) {
		// TODO Auto-generated method stub
		return "sorry "+name+"!";
	}

}

 

 

啟動servcie-feign工程,瀏覽器開啟http://localhost:8765/hi?name=springcloud,注意此時eureka-client工程沒有啟動,網頁顯示:

sorry springcloud! 

開啟eureka-client工程,再次訪問,瀏覽器顯示:

hi ! springcloud,i am from port:8762

這證明斷路器起到作用了