SpringCloud 微服务架构(21)- sentinel - 对RestTemplate的支持

1 对RestTemplate的支持

  • 全局
    在这里插入图片描述

  • @SentinelRestTemplate 注解的属性支持限流( blockHandler , blockHandlerClass )和降级( fallback , fallbackClass )的处理。其中 blockHandler 或 fallback 属性对应的方法必须是对应 blockHandlerClass 或fallbackClass 属性中的静态方法。
    该方法的参数跟返回值跟org.springframework.http.client.ClientHttpRequestInterceptor#interceptor方法一
    致,其中参数多出了一个BlockException 参数用于获取 Sentinel 捕获的异常。
    在这里插入图片描述
package com.tzb.restorder.exception;

import com.alibaba.cloud.sentinel.rest.SentinelClientHttpResponse;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.fastjson.JSON;
import com.tzb.restorder.entity.Product;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;

/**
 * @Description 熔断降级
 * @Author tzb
 * @Date 2020/11/7 17:03
 * @Version 1.0
 **/
public class ExceptionUtils {

    //限流熔断业务逻辑
    public static SentinelClientHttpResponse handleBlock(HttpRequest request,
                                                         byte[] body,
                                                         ClientHttpRequestExecution execution,
                                                         BlockException ex) {
        Product product = new Product();
        product.setProductName("block");
        return new SentinelClientHttpResponse(JSON.toJSONString(product));
    }

    //异常降级业务逻辑
    public static SentinelClientHttpResponse handleFallback(HttpRequest request,
                                                            byte[] body,
                                                            ClientHttpRequestExecution execution,
                                                            BlockException ex) {
        Product product = new Product();
        product.setProductName("fallback");
        return new SentinelClientHttpResponse(JSON.toJSONString(product));
    }
}


package com.tzb.restorder;


import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate;
import com.tzb.restorder.exception.ExceptionUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;


@SpringBootApplication
@EntityScan("com.tzb.restorder.entity")
public class RestOrderApplcation {

    /**
     * sentinel 支持对restTemplate 的服务调用
     * 在构造 RestTemplate对象的时候,只需要加载 @SentinelRestTemplate 即可
     * 这样通过 RestTemplate调用的方法都有了保护措施
     * <p>
     * 资源名:
     * httpmethod:schema://host:port/path :协议、主机、端口和路径
     * httpmethod:schema://host:port :协议、主机和端口
     * <p>
     * 异常降级
     * fallback: 降级方法
     * fallbackClass : 降级配置类
     * <p>
     * 限流熔断:
     * blockHandler
     * blockHandlerClass
     *
     * @return
     */
    @SentinelRestTemplate(fallbackClass = ExceptionUtils.class, fallback = "handleFallback",
                            blockHandler = "handleBlock",blockHandlerClass = ExceptionUtils.class)
    @LoadBalanced
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

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

}


在这里插入图片描述
在这里插入图片描述


  • 快速刷新访问
    在这里插入图片描述

2 sentinel 对 feign 组件的支持

  • 引入依赖
<dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
  • 在工程的application.yml中添加sentinel 对 feign 的支持
feign:
  sentinel:
    enabled: true
  • 配置FeignClient
    和使用Hystrix的方式基本一致,需要配置FeignClient接口以及通过fallback 指定熔断降级方法

在这里插入图片描述

package com.tzb.order.feign;

import com.tzb.order.entity.Product;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * 声明需要调用的微服务的名称
 * name: 服务提供者的名称
 * fallback : 配置熔断发生的降级方法实现类
 */
@FeignClient(name = "service-product", fallback = ProductFeignClientCallback.class)
public interface IProductFeignClient {

    // 配置需要调用的微服务接口
    @GetMapping("/product/{id}")
    public Product findById(@PathVariable Long id);
}

package com.tzb.order.feign;

import com.tzb.order.entity.Product;
import org.springframework.stereotype.Component;


@Component
public class ProductFeignClientCallback implements IProductFeignClient {

    /**
     * 熔断降级的方法
     *
     * @param id
     * @return
     */
    @Override
    public Product findById(Long id) {
        Product product = new Product();
        product.setProductName("feign 调用触发了熔断降级的方法");
        return product;
    }
}


在这里插入图片描述

package com.tzb.order.controller;


import com.tzb.order.entity.Product;
import com.tzb.order.feign.IProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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 IProductFeignClient productFeignClient;

    @GetMapping("/buy/{id}")
    public Product findById(@PathVariable Long id) {
        Product product = null;
        product = productFeignClient.findById(id);
        return product;
    }

}


在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 快速刷新
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值