【无标题】

一级标题

二级

三级


package com.example.controller;

import com.example.pojo.Personal;
import com.example.service.PersonalService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by IntelliJ IDEA2021.3
 *
 * @Author: Tenghw
 * @Date: 2022/03/21  16:37
 * @Description:
 */
@Slf4j
@Controller
@SuppressWarnings("all")
public class ProviderHystrixController {
    @Autowired
    PersonalService personalService;

    @GetMapping(path = "/getAll")
    @ResponseBody
    public List<Personal> getAllPersonal() {
        log.info("=======进入controller层getAllPersonal()============");
        List<Personal> personals = personalService.getServiceAll();
        return personals;
    }



    /**
     * 说明1:若这个方法没有超时(hystrix默认1秒算超时)、或没有异常(比如1/0运行时异常)时,才正常返回,否则进入errorCallBack()方法。
     * 注意:getAllPersonal()和errorCallBack()的返回类型、入参都要相同。
     * 说明2:在feign开启hystrix(如果没用到feign就不用设置)和@EnableHystrix启用断路器前提下。
     */
    @HystrixCommand(fallbackMethod = "errorCallBack",commandProperties = {
            /**
             * 配置命令执行的超时时间:
             * 我们可以通过注解 @HystrixCommand,在方法上配置只针对该方法的超时时间,这个超时时间要 < 配置文件中的超时时间,
             * 即先判断时间是否超出application.yml配置文件中的(没配置就默认1s),再判断是否超出当前注解里面规定的事件。我这里设置2000,即2秒。
             */
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="2000"),
            // 是否开启断路器
            @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),
            /**
             * 	请求总阈值(默认为20): 在快照时间窗口内(默认10s),必须满足请求总数阈值才有资格熔断。
             * 	意味着在10秒内,如果hystrix的调用总次数不足20次,即使所有请求都超时或者其他原因失败,断路器都不会打开。
            */
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "20"),
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"),
            //错误百分比阈值(默认50%):当请求总数在快照时间窗内超过了阈值,比如发生了30次调用,
            // 并且有15次发生了超时或异常,也就是超过了50的错误百分比,在默认设定的50%阈值情况下,这时候就会将断路器打开
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60")})
    @GetMapping(path = "/getOne/{id}")
    @ResponseBody
    @SneakyThrows
    public Personal getOne(@PathVariable("id") int id) throws InterruptedException {
        log.info("=======进入controller层getOne()============");
        Personal personal = personalService.getServiceOne(id);
        TimeUnit.MILLISECONDS.sleep(3000);
        if (ObjectUtils.isEmpty(personal)) {
            throw new RuntimeException("请求的用户id,在数据库中不存在...");
        }
        return personal;
    }
    //不仅是超时,假设服务内的方法出现了异常,也同样会触发兜底的解决方法
    //兜底的方法处理,作为服务熔断的fallback
    public Personal errorCallBack(@PathVariable("id") int id) {
        log.info("==========进入controller层的errorCallBack()=============");
        Personal personal = new Personal();
        personal.setId(8888);
        personal.setName(null);
        personal.setCamp(null);
        personal.setRemarks("no this database in MySQL"+" : ——服务熔断,请求超时或其他异常...------" + id);
        return personal;
    }

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值