介绍
- 雪崩效应
在微服务架构中服务与服务之间能够相互调用,因为网络缘由或者自身的缘由,服务并不能保证100%可用,若是单个服务出现问题,调用这个服务就会占用愈来愈多的系统资源,致使服务瘫痪。因为服务与服务之间的依赖性,故障会传播,会对整个微服务系统形成影响,这就是服务故障的“雪崩”效应。
- 断路器
“断路器”是一种开关装置,当某个服务发生故障监控(相似熔断保险丝),向调用方法返回一个备选的响应,而不是长时间的等待或者抛出调用方法没法处理的异常,这样就保证了服务调用方的线程不会被长时间、没必要要地占用,从而避免了故障在分布式系统中的蔓延,乃至雪崩。
- 熔断模式
在对某个服务调用不可用达到一个阈值,5秒内20次调用失败就会就会启动熔断模式。熔断器的开关是由服务的健康情况(服务的健康情况 = 请求失败数 / 请求总数
)决定的。当断路器开关关闭时请求能够经过,一但服务器健康情况低于设定阈值断路器就会打开,请求会被禁止经过。当断路器处于打开状态时,一段时间后会进入半开状态,这时只容许一个请求经过,若是该请求成功熔断器恢复到关闭状态,若是调用失败断路器继续保持打开。
- 服务降级
服务降级,通常是从总体符合考虑,就是当某个服务熔断以后,服务器将再也不被调用,此刻客户端能够本身准备一个本地的fallback回调,返回一个缺省值。
Ribbon中使用Hystrix
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
@Service
public class HelloServiceRibbonSer {
@Autowired
RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "helloError")
public String helloServiceRibbon(String helloName) {
return restTemplate.getForObject("http://say-hello/sayHello?helloName="+helloName,String.class);
}
public String helloError(String helloName) {
return "hello,"+helloName+",error!";
}
}
@Autowired
private HelloServiceRibbonSer helloServiceRibbonSer;
@GetMapping("/ribbonHello")
public String ribbonHelloCtr(@RequestParam("helloName")String helloName){
return helloServiceRibbonSer.helloServiceRibbon(helloName);
}
在helloServiceRibbon方法的头上加了@HystrixCommand(fallbackMethod = "helloError")注解,表明对这个方法使用了Hystrix相关的功能,fallbackMethod属性是调用回调以后的处理方法。
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient //向服务中心注册
@EnableHystrix //启动Hystrix
public class ServiceRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceRibbonApplication.class, args);
}
}
- 启动项目
启动注册中心和service-ribbon,不启动say-hello项目(服务提供者),在浏览器地址栏访问http://localhost:3333/ribbonHello?helloName=aaa发现调用了fallbackMethod中的方法。
Feign中使用Hystrix
@FeignClient(value = "say-hello",fallback = SayHelloFeignSerHiHystric.class)
public interface SayHelloFeignSer {
@RequestMapping(value = "/sayHello",method = RequestMethod.GET)
String feignSayHelloSer(@RequestParam(value = "helloName") String helloName);
}
@Component
public class SayHelloFeignSerHiHystric implements SayHelloFeignSer{
@Override
public String feignSayHelloSer(String helloName) {
return "发生错误 !"+helloName;
}
}
#打开断路器,D版本以后默认关闭
feign.hystrix.enabled=true
- 启动项目
启动注册中心,service-feign,不启动say-hello项目。在浏览器地址栏访问:http://localhost:4444/feignSayHello?helloName=adaad,发现调用了fallback中的方法。
- fallbackFactory使用
若是要熔断的功能或者业务比较复杂可使用一个工厂来封装,而后直接使用fallbackFactory来标注。
修改SayHelloFeignSer
@FeignClient(value = "say-hello",fallbackFactory = HystrixFallBackFactory.class/*fallback = SayHelloFeignSerHiHystric.class*/)
public interface SayHelloFeignSer {
@RequestMapping(value = "/sayHello",method = RequestMethod.GET)
String feignSayHelloSer(@RequestParam(value = "helloName") String helloName);
}
public interface UserFeignWithFallBackFactoryClient extends SayHelloFeignSer {
}
@Component
public class HystrixFallBackFactory implements FallbackFactory<SayHelloFeignSer> {
@Override
public SayHelloFeignSer create(Throwable throwable) {
return new SayHelloFeignSer(){
@Override
public String feignSayHelloSer(String helloName) {
return "error";
}
@Override
public String manyParamsSer(String userName, String userPassword) {
return "error";
}
@Override
public Object objParamsCtr(Object user) {
return "error";
}
};
}
}