SpringBoot -- 熔断机制 Circuit Breaker

Circuit Breaker


  • 熔断机制在微服务中必不可少,比如故障发生时怎么处理
  • 熔断:半熔断、熔断打开、熔断关闭

  • 熔断关闭: 熔断关闭不会对服务进行熔断,当请求服务失败次数符合设定的规则则进入熔断机制
  • 半熔断: 部分请求根据规则调用当前服务,如果请求成功且符合规则则认为当前服务恢复正常,关闭熔断;
  • 熔断打开:请求不再进行调用当前服务,内部设置时钟一般为(MTTR:平均故障处理时间),当打开时长达到所设时钟则进入半熔断状态。
  • 基于服务策略触发

服务降级


  • 提到熔断机制还得提下服务降级
  • 服务降级往往因为服务压力过大,比如京东双促之类的
  • 服务降级方式比较多,简单举个列子:在各大电商大促销的时候往往详情页有时候是不会看到推荐之类的信息。
  • 熔断与服务降级关系

  • 都是为实现服务高可用
  • 最终的表现方式类似
  • ……

基于Feign实现

  • Feign 本身支持Hystrix,不再需要引入相关的jar
  • Feign实现只支持类的方式,不支持方法
  • 如果启用 Hytrix则设置 enabled = true
feign:
  hystrix:
    enabled: true

基于上次写的FeignServer module来测试此功能

fallback

简单的fallback应用,在FeignClient中加入 fallback

@FeignClient(value = "ribbonserver" , fallback = FeignServerImpl.class )
public interface FeignServer {
    @RequestMapping(value ="/testRealRibbon",method= RequestMethod.GET)
    String testRealRibbon(@RequestParam("content") String content);
}

创建 FeignServerImpl 实现类,实现FeignClient的 FeignServer

@Component
public class FeignServerImpl implements FeignServer {
    public String testRealRibbon(@RequestParam("content") String content) {
        return content + ", it's fallback with feign";
    }
}

测试验证

  • 启动 discovery 、configserver、apigateway、feignserver
  • 因为feign调用的是ribbonserver的服务,所以ribbonserver不用启动

测试结果为: Hello World, it’s fallback with feign

  • 启动ribbonserver

测试结果为: Hello World, for Spring Boot


fallbackFactory

如果需要触发来进行熔断,则需要用 fallbackFactory

在FeignClient中加入 fallbackFactory

@FeignClient(value = "ribbonserver" , fallbackFactory = FeignServerFactoryImpl.class )
public interface FeignServer {
    @RequestMapping(value ="/testRealRibbon",method= RequestMethod.GET)
    String testRealRibbon(@RequestParam("content") String content);

}

创建 FeignServerFactoryImpl 实现类,实现FeignClient的 FeignServer

@Component
public class FeignServerFactoryImpl implements FallbackFactory<FeignServer> {
    /**
     * Returns an instance of the fallback appropriate for the given cause
     *
     * @param cause corresponds to {@link AbstractCommand#getFailedExecutionException()}
     *              often, but not always an instance of {@link FeignException}.
     */
    public FeignServer create(Throwable cause) {

        return new FeignServer() {
            public String testRealRibbon(String content) {
                return content + ", it's fallback Factory with feign";
            }
        };
    }
}

测试验证

  • 启动 discovery 、configserver、apigateway、feignserver
  • 因为feign调用的是ribbonserver的服务,所以ribbonserver不用启动

测试结果为: Hello World, it’s fallback Factory with feign

  • 启动ribbonserver

测试结果为: Hello World, for Spring Boot

基于Ribbon实现

  • 大致与Feign差不多,但需要引入 Hystrix,spring-cloud-starter-hystrix
  • Feign 因为本身支持 hystrix,所以不需要引入
  • @HystrixCommand 指定 fallback的方法
@Controller
public class RibbonController {
    @Autowired
    RestTemplate restTemplate;
    private final static String serverURI = "http://ribbonserver/";
    @RequestMapping("/test")
    @HystrixCommand(fallbackMethod = "testError")
    public String testRibbon(String content) {

        System.out.println(content);
        restTemplate.getForEntity(serverURI+"testRealRibbon?content="+content,String.class);
        return "index";
    }

    public String testError() {
        return "404";
    }
}

代码

代码请移步 Github参考地址

如有疑问请加公众号(K171),如果觉得对您有帮助请 github start
公众号_k171

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章