Spring Cloud Alibaba之服务容错组件 - Sentinel对Feign的支持(十七)

Spring Cloud Alibaba Sentinel 除了对 RestTemplate 做了支持,同样对于 Feign 也做了支持,如果我们要从 Hystrix 切换到 Sentinel 是非常方便的,下面介绍下如何使用 Feign 支持以及实现原理。

Sentinel 集成 Feign使用

第一步: 新建Spring Boot web应用 alibaba-sentinel-feign 在pom.xml,加入 openfeign starter 依赖使 sentinel starter 中的自动化配置类生效:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>0.2.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>


        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.2</version>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

第二步: 需要在配置文件 application.properties 中开启 sentinel 对 feign 的支持


#打开 sentinel 对 feign 的支持
feign.sentinel.enabled=true

第三步: 编写启动类和FeignClient测试接口

@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class SentinelFeignApplication {

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

在启动类上添加 @EnableFeignClients注解开启对Feign支持,同时不要忘了@EnableDiscoveryClient

@FeignClient(name = "user-service", fallback = UserFeignClientFallback.class)
public interface UserFeignClient {

    @GetMapping("/user/{id}")
    public String getUser(@PathVariable("id") Long id);
}
@Component
public class UserFeignClientFallback implements UserFeignClient {

    @Override
    public String getUser(Long id) {
        return "fallback";
    }
}

你可以将这个 Client 对应的 user-service 停掉,然后就可以看到输出的内容是 "fallback"

FallbackFactory

使用 fallback 方式是无法获取异常信息的,如果想要获取异常信息,可以使用 fallbackFactory参数

@Slf4j
@Component
public class UserFeignClientFallbackFactory implements FallbackFactory {
    @Override
     public String create(Throwable cause) {
          return  new  UserFeignClient (Long id) {
              @Override
              public String getUser(Long id) {
                log.warn("远程调用被限流/降级了", cause);
                return "fallback";
              }
        }
      }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值