Sentinel整合OpenFeign

1、配置文件

feign:
  sentinel:
    enabled: true

2、 编写一个工厂类

import com.cart.cartservice.client.ItemClient;
import com.cart.cartservice.entity.Item;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.http.ResponseEntity;

@Slf4j
public class ItemClientFallbackFactory implements FallbackFactory<ItemClient> {

    @Override
    public ItemClient create(Throwable cause) {
        return new ItemClient() {
            @Override
            public ResponseEntity<Item> queryById(Integer id) {
                log.error("查询对象失败", cause);
                //返回一个空对象
                return ResponseEntity.ok(new Item());
            }
        };
    }
}

需要实现FallbackFactory接口,其中ItemClient为客户端接口。 

3、声明这个类

import com.cart.cartservice.factory.ItemClientFallbackFactory;
import org.springframework.context.annotation.Bean;

public class DefaultFeignConfig {

    @Bean
    public ItemClientFallbackFactory getItemClientFallbackFactory(){
        return new ItemClientFallbackFactory();
    }
}

4、ItemClient上添加openFeign注解的属性fallbackFactory = ItemClientFallbackFactory.class)

import com.cart.cartservice.entity.Item;
import com.cart.cartservice.factory.ItemClientFallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient(value = "item-service",fallbackFactory = ItemClientFallbackFactory.class)
public interface ItemClient {

    @GetMapping("item/{id}")
    ResponseEntity<Item> queryById(@PathVariable("id") Integer id);
}

这样就可以在Sentinel里面配置限流了。

相关推荐

  1. Sentinel整合OpenFeign

    2024-01-08 22:10:01       53 阅读
  2. openfeign整合sentinel进行降级

    2024-01-08 22:10:01       34 阅读
  3. Sentinel与SpringBoot整合

    2024-01-08 22:10:01       56 阅读
  4. SpringCloudAlibaba-整合sentinel(四)

    2024-01-08 22:10:01       36 阅读
  5. SpringCloudAlibaba-整合openfeign和loadbalence(三)

    2024-01-08 22:10:01       35 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-08 22:10:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-08 22:10:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-08 22:10:01       82 阅读
  4. Python语言-面向对象

    2024-01-08 22:10:01       91 阅读

热门阅读

  1. Spring IOC

    2024-01-08 22:10:01       58 阅读
  2. python冒泡排序

    2024-01-08 22:10:01       67 阅读
  3. Spring之AOP大体流程

    2024-01-08 22:10:01       58 阅读
  4. 基于SpringBoot的乡村养老服务管理系统

    2024-01-08 22:10:01       68 阅读
  5. 在网址URL中隐藏数据的一些方案

    2024-01-08 22:10:01       60 阅读
  6. vue 中 computed 和 watch 的区别

    2024-01-08 22:10:01       56 阅读
  7. js 对于一些脚本中对于url的一些参数获取

    2024-01-08 22:10:01       59 阅读
  8. 使用什么实现跨域的?

    2024-01-08 22:10:01       65 阅读
  9. 【docker】Dockerfile 指令详解

    2024-01-08 22:10:01       58 阅读
  10. 容器相关笔记

    2024-01-08 22:10:01       48 阅读