SpringCloudAlibaba集成Sentinel——熔断

摘要:

  熔断作为Sentinel除了限流的另一大作用,就相当于Hystrix了,它会对微服务调用链上某个不稳定、宕机、异常、超时的资源作出请求限制并快速失败,避免因其他服务的错误导致整个微服务崩盘,主要就是通过@SentinelResource注解的fallback属性去指定降级方法和降级类

一:本类降级方法

  设置熔断接口并设置本类的降级方法

  1.引入Sentinel依赖:

<!--sentinel依赖-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>

  2.在需要熔断的接口上面打上注解@SentinelResource指定降级方法并编写服务降级方法

	package cn.ybl.controller;
	
	import cn.ybl.domain.User;
	import cn.ybl.feign.UserFeign;
	import com.alibaba.csp.sentinel.annotation.SentinelResource;
	import org.springframework.beans.factory.annotation.Autowired;
	import org.springframework.web.bind.annotation.GetMapping;
	import org.springframework.web.bind.annotation.PathVariable;
	import org.springframework.web.bind.annotation.RequestMapping;
	import org.springframework.web.bind.annotation.RestController;
	
	@RestController
	@RequestMapping("/order")
	public class OrderController {
	
	    @Autowired
	    private UserFeign userFeign;
	
	    @GetMapping("/{id}")
	    //Sentinel限流熔断注解,通过fallback指定熔断后的降级方法
	    @SentinelResource(value="user-server-getById", fallback = "fuseDegradation")
	    public User getUser(@PathVariable("id")Long id){
	        return userFeign.getUser(id);
	    }
	
	    /*
	     * @Description: 熔断降级方法
	     * @param e: 降级异常
	     */
	    public User fuseDegradation(@PathVariable("id") Long id, Throwable  e){
	        e.printStackTrace();
	        return new User(-1L, "熔断服务降级触发!", "服务不可用,请稍后重试!");
	    }
	}

二:降级类降级

  1.编写降级类,必须为static:

package cn.ybl.downgrade;

import cn.ybl.domain.User;
import org.springframework.web.bind.annotation.PathVariable;

/*
 * @Description: 服务降级类
 */
public class FuseDowngradeUtil {

    /*
     * @Description: 熔断降级方法
     */
    public static User fuseDegradation(@PathVariable("id") Long id, Throwable  e){
        e.printStackTrace();
        return new User(-1L, "熔断服务降级触发!", "服务不可用,请稍后重试!");
    }
}

  2.在需要熔断的接口上打上注解@SentinelResource指定熔断类和熔断方法

package cn.ybl.controller;

import cn.ybl.domain.User;
import cn.ybl.downgrade.FuseDowngradeUtil;
import cn.ybl.feign.UserFeign;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private UserFeign userFeign;

    @GetMapping("/{id}")
    //Sentinel限流熔断注解,通过fallback指定熔断后的降级方法
    @SentinelResource(value="user-server-getById", fallback = "fuseDegradation", fallbackClass = FuseDowngradeUtil.class)
    public User getUser(@PathVariable("id")Long id){
        return userFeign.getUser(id);
    }
}

如此便完成了降级类进行熔断降级,当此接口触发熔断【异常、超时等】的时候就会返回我们的托底数据

 

posted @ 2022-08-23 19:06  yyybl  阅读(59)  评论(0)    收藏  举报