SpringCloud Sentinel 使用restTemplate的两种配置介绍

@SentinelResource 主要属性介绍

value:自定义的资源名称。不设置默认为当前类全名.方法名。

blockHandler:降级的处理方法名,默认在当前类中匹配。如果指定了blockHandlerClass(降级处理的类名) ,则在指定类中匹配。

fallback:异常处理的处理方法名,默认在当前类中匹配。如果指定了fallbackClass(异常处理的类名) ,则在指定类中匹配。

 

一、RestTemplate使用@SentinelResource 接入sentinel

在接口方法上使用@SentinelResource注解,并指定其降级处理方法,以及异常处理方法。

当设置控流阈值之后,请求场景超过阈值则超过的部分将进入降级处理方法。

当服务接口发生异常时,没有超过降级配置的阈值时进入异常处理方法。如果超过降级配置的阈值时,则全部进入降级处理方法。

package com.xiaohui.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.xiaohui.springcloud.entities.CommonResult;
import com.xiaohui.springcloud.entities.Payment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/consumer")
public class RestOrderController {

    @Autowired
    private RestTemplate restTemplate;

    @SentinelResource(value = "consumer/product/get4",blockHandler = "getPaymentBlockHandler",fallback = "getPaymentFallback")
    @GetMapping("/product/get4")
    public CommonResult getPaymentById4(@RequestParam(value = "id",required = true)  Long id){
        CommonResult commonResult = null;
        try{Thread.sleep(200);}catch (Exception e){e.printStackTrace();}
        commonResult = restTemplate.getForObject("http://cloud-payment-service/payment/get/"+id,CommonResult.class);
        return  commonResult;
    }

    public CommonResult getPaymentBlockHandler(Long id, BlockException be){
        return new CommonResult<>(402,"熔断降级方法",null);
    }
    public CommonResult getPaymentFallback(Long id, Throwable te){
        return new CommonResult<>(403,"异常降级方法",null);
    }
}

二、RestTemplate使用@SentinelRestTemplate接入sentinel

改造文件:

  1. controller 不使用@SentinelResource 注解
  2. 调整 RestTemplate配置文件
  3. 创建fallbackClass 编写失败处理函数
  4. 创建blockHandlerClass 编写降级处理函数 

如果测试后唯有进入到降级方法中,有可能需要将RestTemplate类配置对象放在主启动类中,在配置文件中添加

resttemplate:
  sentinel:
    enabled: true

如果在不行,有可能为SpringBoot、SpringCloud、SpringCloud Alibaba 版本不匹配导致。

1,com.xiaohui.springcloud.controller.RestOrderController2.java

package com.xiaohui.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.xiaohui.springcloud.entities.CommonResult;
import com.xiaohui.springcloud.entities.Payment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/consumer2")
public class RestOrderController2 {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/product/get")
    public CommonResult getPaymentById4(@RequestParam(value = "id",required = true)  Long id){
        CommonResult commonResult = null;
        try{Thread.sleep(200);}catch (Exception e){e.printStackTrace();}
        commonResult = restTemplate.getForObject("http://cloud-payment-service/payment/get/"+id,CommonResult.class);
        return  commonResult;
    }
}

2,com.xiaohui.springcloud.config.ApplicationContextConfig.java

package com.xiaohui.springcloud.config;

import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate;
import com.xiaohui.springcloud.exception.ExceptionUtil;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
 *
 * 相当于 spring的 application.xml bean注解相当于 bean 标签
 */
@Configuration
public class ApplicationContextConfig {

    @Bean
    @LoadBalanced
    @SentinelRestTemplate(fallbackClass = ExceptionUtil.class,fallback = "fallBack",
            blockHandlerClass = ExceptionUtil.class,blockHandler = "handleBlock")
    public RestTemplate getRestTemplate(){
        return  new RestTemplate();
    }
}

3, 降级与异常处理类编写在一个类中。

package com.xiaohui.springcloud.exception;

import com.alibaba.cloud.sentinel.rest.SentinelClientHttpResponse;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.fastjson.JSON;
import com.xiaohui.springcloud.entities.CommonResult;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;

public class ExceptionUtil {

    public static SentinelClientHttpResponse handleBlock(HttpRequest request, byte[] body, ClientHttpRequestExecution execution, BlockException be){
        CommonResult commonResult = new CommonResult(500,"降级处理函数。。。。。");
        return new SentinelClientHttpResponse(JSON.toJSONString(commonResult));
    }

    public static SentinelClientHttpResponse fallBack(HttpRequest request, byte[] body, ClientHttpRequestExecution execution, BlockException be){
        CommonResult commonResult = new CommonResult(500,"异常处理函数。。。。。");
        return new SentinelClientHttpResponse(JSON.toJSONString(commonResult));
    }
}

 

你可能感兴趣的:(SpringCloud,sentinel)