Spring Cloud Alibaba 教程(基于首个毕业版)(九):RestTemplate和Feign整合Sentinel

RestTemplate整合Sentinel

第一步:打开@SentinelRestTemplate注解配置 yml文件

resttemplate:
  sentinel:
    # 打开@SentinelRestTemplate注解
    enabled: true

第二步:新建BlockHandler类和FallBackHandler类处理限流降级

@Slf4j
public class BlockHandler {

    public static SentinelClientHttpResponse handleException(HttpRequest request,
                                                             byte[] body, 
                                                             ClientHttpRequestExecution execution,
                                                             BlockException ex){
        log.info("block Handler");
        return new SentinelClientHttpResponse();
    }
}
@Slf4j
public class FallBackHandler {

    public static SentinelClientHttpResponse fallBackHandle(HttpRequest request,
                                                             byte[] body, 
                                                            ClientHttpRequestExecution execution, 
                                                            BlockException ex){
        log.info("fallBack Handler");
        return new SentinelClientHttpResponse("异常出现");
    }
}

第三步:RestTemplate 添加 @SentinelRestTemplate 注解

@Bean
    //整合Ribbon 的注解
    @LoadBalanced
    //整合Sentinel和RestTemplate
    @SentinelRestTemplate(blockHandler = "handleException",blockHandlerClass = BlockHandler.class,
    fallback = "fallBackHandle", fallbackClass = FallBackHandler.class)
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

第四步:测试类

@Autowired
    private RestTemplate restTemplate;

    @GetMapping("/test-sentinel-rest-templeate/{userId}")
    public UserDTO testSentinelRestTemplate(@PathVariable Integer userId){
        return this.restTemplate.getForObject("http://center-user/users/{userId}",
        UserDTO.class,userId);
    }

第五步:在控制台设置限流降级策略进行测试
测试结果省略

Feign整合Sentinel

第一步:开启sentinel对feign的支持

feign:
  sentinel:
    enabled: true

第二步:两种处理限流降级发生情况可根据自己业务需要选择其一

  1. 限流降级发生时,定制自己的处理逻辑

在 @FeignClient 加上fallback

@FeignClient(name = "center-user",
        fallback = UserCenterFeignClientFallback.class)
public interface CenterUserFeignClient {

    @GetMapping("/users/{id}")
    UserDTO findById(@PathVariable Integer id);
}
@Component
public class UserCenterFeignClientFallback implements CenterUserFeignClient {
    @Override
    public UserDTO findById(Integer id) {
        // 自己处理业务逻辑
        UserDTO userDTO = new UserDTO();
        userDTO.setWxNickname("一个默认用户");
        return userDTO;
    }
}


     2​​​​​​. 限流降级发送时,获取异常
在 @FeignClient 加上 fallbackFactory

@FeignClient(name = "center-user",
        fallbackFactory = UserCenterFeignClientFallbackFactory.class)
public interface CenterUserFeignClient {

    @GetMapping("/users/{id}")
    UserDTO findById(@PathVariable Integer id);
}
@Component
@Slf4j
public class UserCenterFeignClientFallbackFactory implements FallbackFactory<CenterUserFeignClient> {
    @Override
    public CenterUserFeignClient create(Throwable throwable) {
        return new CenterUserFeignClient() {
            @Override
            public UserDTO findById(Integer id) {
                log.warn("远程调用被限流/降级了",throwable);
                UserDTO userDTO = new UserDTO();
                userDTO.setWxNickname("一个默认用户");
                return userDTO;
            }
        };
    }
}

第三步:请求接口http://127.0.0.1:8010/shares/1 设置限流降级策略,进行测试
测试结果省略

 

Sentinel 使用姿势总结

使用名称使用方式使用方法
编码方式APItry...catch..finally..
注解方式SentinelResourceblockHandler/fallback
RestTemplateSentinelRestTemplateblockHandler/fallback
Feignfeign.sentinel.enabled=truefallback/fallbackFactory


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值