介绍雪崩效应 在微服务架构中服务与服务之间可以相互调用,由于网络原因或者自身的原因,服务并不能保证100%可用,如果单个服务出现问题,调用这个服务就会占用越来越多的系统资源,导致服务瘫痪。由于服务与服务之间的依赖性,故障会传播,会对整个微服务系统造成影响,这就是服务故障的“雪崩”效应。断路器 “断路器”是一种开关装置,当某个服务发生故障监控(类似熔断保险丝),向调用方法返回一个备选的响应,而不是长时间的等待或者抛出调用方法无法处理的异常,这样就保证了服务调用方的线程不会被长时间、不必要地占用,从而避免了故障在分布式系统中的蔓延,乃至雪崩。熔断模式 在对某个服务调用不可用达到一个阈值,5秒内20次调用失败就会就会启动熔断模式。熔断器的开关是由服务的健康状况(服务的健康状况 = 请求失败数 / 请求总数)决定的。当断路器开关关闭时请求可以通过,一但服务器健康状况低于设定阈值断路器就会打开,请求会被禁止通过。当断路器处于打开状态时,一段时间后会进入半开状态,这时只允许一个请求通过,如果该请求成功熔断器恢复到关闭状态,如果调用失败断路器继续保持打开。服务降级 服务降级,一般是从整体符合考虑,就是当某个服务熔断之后,服务器将不再被调用,此刻客户端可以自己准备一个本地的fallback回调,返回一个缺省值。Ribbon中使用Hystrix添加maven依赖 在SpringCloud 服务消费者(RestTemplate+Ribbon)基础上对service-ribbon项目进行修改。 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>修改HelloServiceRibbonSer类@Servicepublic 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 //启动Hystrixpublic 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修改项目 修改SpringCloud 服务消费者(Feign)项目。Feign是自带断路器的,只需要在FeignClient的接口的注解中加上fallback的指定类就行了。@FeignClient(value = “say-hello”,fallback = SayHelloFeignSerHiHystric.class)public interface SayHelloFeignSer { @RequestMapping(value = “/sayHello”,method = RequestMethod.GET) String feignSayHelloSer(@RequestParam(value = “helloName”) String helloName);}@Componentpublic 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 {}@Componentpublic 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”; } }; }}