springcloud篇六之 熔断 降级

服务熔断->服务端操作
服务降级->客户端操作
有关详情点击查看

熔断的使用
1、引入依赖
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
            <version>1.4.6.RELEASE</version>
</dependency>
2、开启注解支持
@SpringBootApplication
@EnableEurekaClient
//熔断支持
@EnableCircuitBreaker
public class ApplicationProviederDept_8001 {
    public static void main(String[] args) {
        SpringApplication.run(ApplicationProviederDept_8001.class,args);
    }
}
3 、controller层
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;
    
    //获取单个员工信息
    @GetMapping("/dept/get/{id}")
    //熔断
    @HystrixCommand(fallbackMethod =  "noDept")
    public  Dept getDept(@PathVariable("id")Long id){
        Dept  dept =  deptService.selectById(id);
        if(dept==null){
            throw  new RuntimeException("");
        }
        return dept;
    }
    public  Dept  noDept(@PathVariable("id") Long id){
    	//链式操作
        return  new Dept()
                .setDeptno(id)
                .setDb_name("id==>"+id+",不存在该用户 null")
                .setDb_source("no this database in mysql");
    }
   }
4、测试

测试前开启一个注册中心和一个消费者

直接访问生产者效果在这里插入图片描述
从消费者路由访问结果:
在这里插入图片描述
当断掉注册中心时 访问依旧不会出问题,返回的依然是我们定制的内容

降级

熔断是对单一的请求进行处理,而降级是对一个类进行处理
@FeignClient中有个参数叫fallbackFactory
我们通过继承这个接口实现对出现突然状况时返回我们制定的信息
在这里插入图片描述
service

@FeignClient(value = "SPRINGCLOUD-PROVIEDER-DEPT",fallbackFactory = DeptClientServiceFallBackFactory.class)
@Component
public interface DeptFeignService {

    @GetMapping("/dept/all")
    public List<Dept> getDeptAll();
    //获取单个员工信息
    @GetMapping("/dept/get/{id}")
    public  Dept getDept(@PathVariable("id")Long id);
    //添加员工
    @RequestMapping("/dept/add")
    public  String addDept(@RequestBody Dept dept);
}

factory:

//降级
@Component
public class DeptClientServiceFallBackFactory implements FallbackFactory {
    @Override
    public DeptFeignService create(Throwable throwable) {
        return  new DeptFeignService() {
            @Override
            public List<Dept> getDeptAll() {
                return null;
            }

            @Override
            public Dept getDept(Long id) {
                return new Dept().setDeptno(id).setDb_name("降级").setDb_source("nosql");
            }

            @Override
            public String addDept(Dept dept) {
                return null;
            }
        };
    }
}
5、测试

测试步骤:
1.开启生产者和feign消费者及一个注册中心
2.通过消费者访问指定的部门信息,结果正常
3.关掉生产者,继续通过消费者客户端访问,返回依旧有数据,但是不是数据库信息而是我们制定的信息
在这里插入图片描述

Spring Cloud Alibaba提供了一个名为Sentinel的熔断降级框架,可以方便地与Spring Cloud集成。下面是使用Spring Cloud Alibaba Sentinel进行熔断降级的步骤: 1. 添加依赖 在`pom.xml`文件中添加以下依赖: ```xml <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> <version>2.2.3.RELEASE</version> </dependency> ``` 2. 配置Sentinel 在`application.yml`文件中添加以下配置: ```yaml spring: cloud: sentinel: transport: dashboard: localhost:8080 #Sentinel Dashboard地址 port: 8719 #Sentinel客户端监听端口号 ## 开启Sentinel熔断降级 feign: sentinel: enabled: true ``` 3. 配置熔断降级规则 在`resources`目录下创建一个名为`META-INF`的文件夹,然后在该文件夹下创建一个名为`flowrule`的文件,文件名可以自己定义。在该文件中添加以下内容,这里以配置一个`Hello World`的熔断降级规则为例: ```json [ { "resource": "hello", //资源名称,可以是方法名或URL "count": 5, //阈值 "grade": 0, //熔断策略,0表示平均响应时间,1表示异常比率 "timeWindow": 5, //时间窗口,单位是秒 "minRequestAmount": 5, //最小请求数量 "slowRatioThreshold": 0 //慢调用比例阈值 } ] ``` 4. 使用@SentinelResource注解 在需要进行熔断降级的方法上添加`@SentinelResource`注解,并指定资源名称,例如: ```java @SentinelResource(value = "hello", fallback = "fallback") public String hello() { return "Hello World"; } public String fallback() { return "fallback"; } ``` 其中`fallback`方法为熔断降级时的备选方法。 5. 启动Sentinel Dashboard 在命令行中输入以下命令启动Sentinel Dashboard: ```bash java -jar sentinel-dashboard-1.8.2.jar ``` 访问`http://localhost:8080`即可进入Sentinel Dashboard。 6. 启动应用程序 启动应用程序后,可以在Sentinel Dashboard中看到应用程序的熔断降级情况。当资源的请求次数超过阈值时,Sentinel将自动触发熔断降级策略,调用`fallback`方法,返回备选结果。 以上就是使用Spring Cloud Alibaba Sentinel进行熔断降级的步骤。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值