1. 程式人生 > >Java後端愛上SpringCloud 第三節:熔斷器 Hystrix

Java後端愛上SpringCloud 第三節:熔斷器 Hystrix

Java後端愛上SpringCloud 第三節:熔斷器 Hystrix


PS:本來我都把這篇都寫過了,不過沒有儲存,現在重寫一遍。還有就是Hystrix這個東西,已經沒有人繼續升級了,但是重在穩定,而且BUG還是在修復的。

一些連結

為什麼要用熔斷器?

Ribbon結合Hystrix

繼續My-Spring-Ribbon工程。引入依賴:

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

我們再改造一下RibbonControler類。

package com.hyn.cloud.ribbon.
controler; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.
ResponseBody; import org.springframework.web.bind.annotation.RestController; import com.hyn.cloud.ribbon.service.RibbonService; @RestController @RequestMapping("/ribbon") public class RibbonControler { @Autowired RibbonService iRibbonService; @RequestMapping(value = "/name",method=RequestMethod.GET) @ResponseBody public String ribbon(@RequestParam String name){ return iRibbonService.sendFeignServer(name); } @RequestMapping(value = "/name2",method=RequestMethod.GET) @ResponseBody public String ribbon2(@RequestParam String name){ String respResult="I am ribbon,My name2 is "+name; return respResult; } }

再來改造一下:

package com.hyn.cloud.ribbon.service;

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;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;

@Service
public class RibbonService {

    /**
     * 注入RestTemplate
     */
    @Autowired
    RestTemplate restTemplate;
	
	 @HystrixCommand(fallbackMethod = "errorSendFeignServer",
	            commandProperties = {
	            		////指定多久超時,單位毫秒。超時進fallback
	                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "4000"),
	                    //判斷熔斷的最少請求數,預設是10;只有在一個統計視窗內處理的請求數量達到這個閾值,才會進行熔斷與否的判斷
	                    @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
	                    //判斷熔斷的閾值,預設值50,表示在一個統計視窗內有50%的請求處理失敗,會觸發熔斷
	                    @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "10"),
	            }
	    )
	    public String sendFeignServer(String name)
	    {
	    	String respResult="I am ribbon,My name is "+name;
	    	String url="http://SPRING-BOOT-FEIGN/feign/name?name=";
	        return restTemplate.getForObject(url+respResult, String.class);
	    }

	    
	    public String errorSendFeignServer(String name)
	    {
	        return "feign server is die!";
	    }	
}

依次啟動一下:My-Spring-Eureka,My-Spring-Admin,My-Spring-Feign,My-Spring-Ribbon
正常現在請求:
http://127.0.0.1:8765/ribbon/name?name=hyn
在這裡插入圖片描述
關閉My-Spring-Feign工程:
在這裡插入圖片描述

Feign結合Hystrix

feign不用引入Hystrix依賴,因為本身就整合這。但是feign預設是不開啟熔斷功能的要在application.yml新增以下配置:

feign:
  hystrix:
    enabled: true

改造FeignControler

package com.hyn.cloud.feign.controler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hyn.cloud.feign.service.FeignService;

@RestController
@RequestMapping("/feign")
public class FeignControler {

	@Autowired
	FeignService feignService;
	
    @RequestMapping(value = "/name",method=RequestMethod.GET)
    public String feign(@RequestParam String name){
		return feignService.getName(name);
    }
}

改造FeignService

package com.hyn.cloud.feign.service;

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 = "SPRING-BOOT-RIBBO",fallback=FeignServiceHystric.class,configuration=FeignDisableHystrixConfiguration.class )
public interface FeignService {
	
    @RequestMapping(value = "/ribbon/name2",method = RequestMethod.GET)
	String getName(@RequestParam("name")String name);

}

新增HystrixFeignConfiguration

package com.hyn.cloud.feign.service;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import com.netflix.hystrix.HystrixCommand;

import feign.Feign;
import feign.hystrix.HystrixFeign;

@Configuration
public class FeignDisableHystrixConfiguration {

	@Configuration
	@ConditionalOnClass({ HystrixCommand.class, HystrixFeign.class })
	protected static class HystrixFeignConfiguration {
	    @Bean
	    @Scope("prototype")
	    public Feign.Builder feignHystrixBuilder() {
	        return HystrixFeign.builder();
	    }
	}
	
}

新增FeignServiceHystric

package com.hyn.cloud.feign.service;

import org.springframework.stereotype.Component;

@Component
public class FeignServiceHystric implements FeignService{
	@Override
	public String getName(String name) {
        return "ribbon server is die!";
	}
}

測試一下:
依次啟動一下:My-Spring-Eureka,My-Spring-Admin,My-Spring-Feign,My-Spring-Ribbon
正常請求:
在這裡插入圖片描述
關閉My-Spring-Ribbon:
在這裡插入圖片描述

有興趣的同學,可以自行測一下超時的請求。異常的請求。