Hystrix一般是配置OpenFeign来使用,当然也可以用于Ribbon(自己去研究)
在服务的调用方,添加Hystrix的依赖关系
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-hystrixartifactId>
dependency>
在application.yml配置如下内容:
feign:
client:
config:
micro-base-server: #被调用者的微服务名称
loggerLevel: FULL
hystrix:
enabled: true #开启Feign对熔断器的支持
hystrix:
command:
default:
circuitBreaker:
requestVolumeThreshold: 50 #10S范围内,熔断器可以接收50个请求(默认20)
sleepWindowInMilliseconds: 5 #熔断以后,5S范围内,熔断器的状态都出于半开状态
errorThresholdPercentage: 50 #10S范围内,如果50个请求,超过25个请求都出问题,服务开始熔断
feign.hystrix.enabled Feign调用时支持熔断器
在调用者的启动类身上,添加注解@EnableCircuitBreaker//开启熔断器,@EnableHystrix//使用Hystrix组件,来完成熔断
@EnableFeignClients
@EnableCircuitBreaker//开启熔断器
@EnableHystrix//使用Hystrix组件,来完成熔断
@EnableTransactionManagement
@MapperScan(basePackages = {"com.woniuxy.microuserserver.*mag.mapper"})
@EnableEurekaClient
@SpringBootApplication
public class MicroUserServerApplication {
public static void main(String[] args) {
SpringApplication.run(MicroUserServerApplication.class, args);
}
}
如果大家想针对某一个服务方法,进行熔断规则设置,可以使用@HystrixCommand
/**
* 删除
* @param id
* @return
*/
@DeleteMapping(value = "/{id}",produces = MediaType.APPLICATION_JSON_VALUE)
public String deleteMarket(@PathVariable("id") Integer id){
System.out.println(id);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return JSONObject.toJSONString(ResponseMsg.builder().code(200).msg("操作成功").build());
}
FeignController中,定义熔断规则
/**
* 删除
* @param id
* @return
*/
@HystrixCommand(
fallbackMethod = "deleteMarketFallback",
commandProperties = {
@HystrixProperty(name = "circuitBreaker.enabled",value = "true"),//开启断路器
@HystrixProperty(
name = "circuitBreaker.requestVolumeThreshold",
value = "10"),//设置10S范围内,可以接收10个请求
@HystrixProperty(
name = "circuitBreaker.sleepWindowInMilliseconds",
value = "3000")
,//熔断器的休眠时间为3S,3S范围内:熔断器都是出于半开状态
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",
value = "2000"),//设置超时时间
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "50")
//设置超过50%的比例异常,就开启熔断
}
)
@DeleteMapping(value="/delete",produces = MediaType.APPLICATION_JSON_VALUE)
public String deleteMarket(Integer id){
//调用Base微服务的修改方法
String res = microBaseServerFeign.deleteMarket(id);
return res;
}
circuitBreaker.enabled 开启断路器
circuitBreaker.requestVolumeThreshold 设置10S范围内,可以接收10个请求
circuitBreaker.errorThresholdPercentage 设置超过50%的比例异常,就开启熔断
circuitBreaker.sleepWindowInMilliseconds 熔断器的休眠时间为3S,3S范围内:熔断器都是出于半开状态
execution.isolation.thread.timeoutInMilliseconds 微服务调用的超时时间,只要超过该时间,说该次请求无效,将执行fallbackMethod 中的方法
fallbackMethod 熔断之后,需要执行的方法
/**
* 熔断方法 deleteMarketFallback
* @param id
* @return
*/
public String deleteMarketFallback(Integer id){
System.out.println("进入熔断方法了!!!" +
"");
return JSONObject.toJSONString(ResponseMsg.builder().
code(10000).msg("删除市场服务,已达熔断阈值!").build());
}
但是:上述的这种方式需要每个方法,都定义依次熔断规则,显得很麻烦
有没有解决方案?答案:全局设置
hystrix:
command:
default:
circuitBreaker:
requestVolumeThreshold: 10 #10S范围内,熔断器可以接收10个请求
sleepWindowInMilliseconds: 5000 #熔断以后,5S范围内,熔断器的状态都出于半开状态
errorThresholdPercentage: 50 #10S范围内,超过50%的请求出问题,服务开始熔断
execution:
isolation:
thread:
timeoutInMilliseconds: 6000 #超过6S钟,微服务调用将超时
@Component
public class MicroBaseServerFallbackFactory implements FallbackFactory<MicroBaseServerFeign> {
@Override
public MicroBaseServerFeign create(Throwable throwable) {
throwable.printStackTrace();
//匿名内部类
return new MicroBaseServerFeign() {
@Override
public String findAllByParams(String mName, String telphone, String adminName) {
return JSONObject.toJSONString(ResponseMsg.builder().
code(10000).msg("根据参数查询市场服务,已达熔断阈值!").build());
}
@Override
public String saveMarket(String mName, String telphone, String adminName) {
return JSONObject.toJSONString(ResponseMsg.builder().
code(10000).msg("保存市场服务,已达熔断阈值!").build());
}
@Override
public String saveMarket2(MarketInfo marketInfo) {
return JSONObject.toJSONString(ResponseMsg.builder().
code(10000).msg("保存市场服务,已达熔断阈值!").build());
}
@Override
public String updateMarket(MarketInfo marketInfo, Integer id) {
return JSONObject.toJSONString(ResponseMsg.builder().
code(10000).msg("修改市场服务,已达熔断阈值!").build());
}
@Override
public String deleteMarket(Integer id) {
return JSONObject.toJSONString(ResponseMsg.builder().
code(10000).msg("删除市场服务,已达熔断阈值!").build());
}
@Override
public String findAll() {
return JSONObject.toJSONString(ResponseMsg.builder().
code(10000).msg("查询所有竞拍服务,已达熔断阈值!").build());
}
};
}
}
你的工厂,一定要交给Spring容器
@FeignClient(name = "micro-base-server",fallbackFactory =
MicroBaseServerFallbackFactory.class)
public interface MicroBaseServerFeign {……}
=
MicroBaseServerFallbackFactory.class)
public interface MicroBaseServerFeign {……}
###### 被调用方法,添加Thread.sleep(5000)
[外链图片转存中...(img-o2rmDMSt-1615997131346)]
###### 熔断效果
[外链图片转存中...(img-zYvPiueL-1615997131346)]