springcloud 服务容错和Hystrix

服务间调用出现故障,而这个服务又被其他服务调用,然后级联效应,又由于请求是同步的,导致服务发生雪崩效应

一、
二、作用

服务降级、依赖隔离、服务熔断、监控

服务降级:

  • 优先核心服务,非核心服务不可用或若可用
  • 通过HystrixCommand注解指定
  • fallbackMethod(回退函数)中具体实现降级逻辑
、三、环境配置
        <dependency> <!--触发服务降级-->
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients//服务调用feign
@EnableCircuitBreaker//开启熔断
public class OrderApplication {

    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class, args);
    }

}

1、触发服务降级

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
//出现问题将回调fallbackMethod方法
@DefaultProperties(defaultFallback = "defaultFallback")
public class HystrixController {

    @GetMapping("hytrix/testBreak")
     @HystrixCommand(commandProperties = {
            //超时时间超过将调用降级方法
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "100")
    }) //这个逻辑出现问题,可以使用降级回调方案
    public String testBreak() {
        try {
            int i = 10 / 0;
        } catch (Exception e) {
            throw new RuntimeException("出现异常,然后降级处理");
        }
        return "testBreak ok 啦";
    }

    //没有标注 @HystrixCommand 出现异常就不会降级
    @GetMapping("hytrix/test")
    public String test() {
        int i = 10 / 0;
        return "test 啦";
    }

    //对于上面的一种补偿方案
    private String fallbackMethod() {
        return "人数过多,请稍后再试!";
    }

    //默认一种补偿方案
    private String defaultFallback() {
        return "默认方案:人数过多,请稍后再试!";
    }
}

Hystrix技术解析
Hystrix 详解

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值