Hystrix的介绍:
Hystrix,是spring cloud的一个组件,是一种保护机制,用于隔离访问远程服务,防止出现级联失败(雪崩效应)。
Hystrix处理请求的策略:
服务降级:Fallback,可以理解为备胎,从五星级降到四星级等;
线程隔离:避仓模式,默认的一种策略;例如新冠肺炎在某个地区爆发,将这个地区隔离起来;
信号量模式:设定一个阈值,达到这个阈值后采取相应的措施;
Hystrix的使用:
1. 添加依赖:
在微服务user-consumer端添加依赖:
<!--熔断器-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
2. 使用注解@EnableCircuitBreaker开启熔断机制:
在user-consumer端的启动类中开启熔断机制:
package com.it;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
* ToDo
*
* @author Lyle
* @date 2020/4/3
*/
@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker//开启熔断机制
public class UserConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(UserConsumerApplication.class,args);
}
@Bean
@LoadBalanced//Ribbon集成RestTemplate,实现了负载均衡
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
3. 在user-consumer端的Controller类中的响应方法上使用注解@HystrixCommand(fallbackMethod = "myFallBackMethod"),
myFallBackMethod为定义的熔断处理方法,方法名可以自定义,熔断开启,出现错误后,返回该方法定义的响应数据;
package com.it.controller;
import com.it.pojo.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
/**
* ToDo
*
* @author Lyle
* @date 2020/4/3
*/
@RestController
@RequestMapping("/movie")
public class MovieController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
@RequestMapping("/look/{username}")
@HystrixCommand(fallbackMethod = "myFallBackMethod")
public String look(@PathVariable(name = "username") String username){
//接收用户信息
List<ServiceInstance> instances = discoveryClient.getInstances("user-provider");
ServiceInstance serviceInstance = instances.get(0);
User user = restTemplate.getForObject("http://user-provider/user/findByUser", User.class);
System.out.println(username+"===========>"+user.getName());
return "success";
}
//自定义的熔断处理方法,出现错误后,返回该方法定义的响应数据
//注意该方法的参数一定要和使用注解@HystrixCommand的方法参数保持一致
public String myFallBackMethod(String username){
return "数据是备用的。。。。"+":"+username;
}
}
4. 在user-consumer端的Controller类上面使用注解@DefaultProperties(defaultFallback = "myDefaultFallback"),并自定义
熔断处理方法,返回默认的数据,myDefaultFallback为自定义的熔断处理方法:
1. 在方法使用注解:@HystrixCommand
2. 自定义的熔断处理方法
package com.it.controller;
import com.it.pojo.User;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
/**
* ToDo
*
* @author Lyle
* @date 2020/4/3
*/
@RestController
@RequestMapping("/movie")
@DefaultProperties(defaultFallback = "myDefaultFallback")//定义默认的Fallback,所有的方法都可用
public class MovieController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
@RequestMapping("/look/{username}")
@HystrixCommand
public String look(@PathVariable(name = "username") String username){
//接收用户信息
List<ServiceInstance> instances = discoveryClient.getInstances("user-provider");
ServiceInstance serviceInstance = instances.get(0);
User user = restTemplate.getForObject("http://user-provider/user/findByUser", User.class);
System.out.println(username+"===========>"+user.getName());
return "success";
}
//定义默认的熔断处理方法,所有的方法都可用
//注意该方法不需要给参数
public String myDefaultFallback(){
return "默认的数据。。。。";
}
}
5. 熔断策略的配置:
在微服务user-consumer端的配置文件application.yml中进行配置:
# 配置熔断策略:
hystrix:
command:
default:
circuitBreaker:
# 强制打开熔断器 默认false关闭的。测试配置是否生效
forceOpen: false
# 触发熔断错误比例阈值,默认值50%
errorThresholdPercentage: 50
# 熔断后休眠时长,默认值5秒
sleepWindowInMilliseconds: 10000
# 熔断触发最小请求次数,默认值是20
requestVolumeThreshold: 10
execution:
isolation:
thread:
# 熔断超时设置,默认为1秒
timeoutInMilliseconds: 2000