Spring Cloud Hystrix 可以实现的功能
降级,熔断...
0. 工程中引入jar包
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
1.启动类添加引用
@EnableCircuitBreaker
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableEurekaClient @EnableFeignClients("com.lvlvstart.spring.demo.common.client") @EnableCircuitBreaker //熔断器注解 public class SpringUserApplication { public static void main(String[] args) { SpringApplication.run(SpringUserApplication.class, args); } }
1.降级
方法上添加注解 @HystrixCommand ,除了 HystrixBadRequestException 以外的异常,都会触发降级
@DefaultProperties(defaultFallback = "defaultMsg") : 调用统一处理方法 defaultMsg
@HystrixCommand 需要降级的方法加
import com.lvlvstart.spring.demo.common.client.SchoolClient;
import com.lvlvstart.spring.demo.common.enums.MsgEnum;
import com.lvlvstart.spring.demo.common.msg.BaseWeb;
import com.lvlvstart.spring.demo.common.msg.Result;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @description 类描述
* @create 2019/12/12 13:57
*/
@RestController
@RequestMapping("rest_feign")
@DefaultProperties(defaultFallback = "defaultMsg")
public class OpenFeignExampleWeb extends BaseWeb {
@Autowired
private SchoolClient schoolClient;
@PostMapping("findAllSchool")
public Result findAll(){
return schoolClient.findAll();
}
@HystrixCommand
@PostMapping("findAllSchoolGet")
public Result findAllGet(){
return schoolClient.findAll();
}
public Result defaultMsg(){
return fail(MsgEnum.DOWNGRADE.getCode(),MsgEnum.DOWNGRADE.getMsg());
}
}
2.熔断配置
熔断主要的注解中的配置:
@HystrixCommand(fallbackMethod = "brekerMsg",commandProperties = {@HystrixProperty(name = "circuitBreaker.enabled",value = "true"), @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"), //设置再滚动时间窗口中的最小请求数, @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"),//熔断开启后的时间 @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "50")}) //设置比60%,如果再滚动时间窗口内,大于60%的时候,就会进行熔断 public Result findAllBreaker(){ return schoolClient.findAll(); }
设置失败调用方法:
public Result brekerMsg(){ return fail(MsgEnum.BREAKER.getCode(),MsgEnum.BREAKER.getMsg()); }
配置方式注入属性(方法上还是要有 @HystrixCommand 注解,但是可以不加任何属性)
hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 1000 circuitBreaker: enable: true requestVolumeThreshold: 10 sleepWindowInMilliseconds: 10000 errorThresholdPercentage: 50 findAllGet: execution: isolation: thread: timeoutInMilliseconds: 10000 findAllBreaker: execution: isolation: thread: timeoutInMilliseconds: 1000 circuitBreaker: enable: true requestVolumeThreshold: 10 sleepWindowInMilliseconds: 10000 errorThresholdPercentage: 50
Hystrix Dashboard
开启Hystrix面板能够统计调用失败的情况
1.引用pom
<!--dashboard 界面--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency>
2.开启注解@EnableHystrixDashboard
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.hystrix.EnableHystrix; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.context.annotation.Bean; @SpringBootApplication @EnableEurekaClient @EnableFeignClients("com.lvlvstart.spring.demo.common.client") @EnableHystrix @EnableHystrixDashboard @EnableCircuitBreaker //熔断器注解 public class SpringUserApplication { public static void main(String[] args) { SpringApplication.run(SpringUserApplication.class, args); } //改变访问地址 @Bean public ServletRegistrationBean getServlet() { HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet(); ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet); registrationBean.setLoadOnStartup(1); registrationBean.addUrlMappings("/hystrix.stream"); registrationBean.setName("HystrixMetricsStreamServlet"); return registrationBean; } }
控制面板地址 localhost:8080 按照如下配置
访问带@HystrixCommand注解的方法,界面就会显示统计数值