前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Cloud Alibaba之服务容错组件 - Sentinel对Feign的支持(十七)

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

作者头像
用户1212940
发布2022-04-13 16:59:45
5940
发布2022-04-13 16:59:45
举报
文章被收录于专栏:LambdaLambda

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

Sentinel 集成 Feign使用

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

代码语言:javascript
复制
    <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 的支持

代码语言:javascript
复制
#打开 sentinel 对 feign 的支持
feign.sentinel.enabled=true

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

代码语言:javascript
复制
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class SentinelFeignApplication {

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

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

代码语言:javascript
复制
@FeignClient(name = "user-service", fallback = UserFeignClientFallback.class)
public interface UserFeignClient {

    @GetMapping("/user/{id}")
    public String getUser(@PathVariable("id") Long id);
}
代码语言:javascript
复制
@Component
public class UserFeignClientFallback implements UserFeignClient {

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

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

FallbackFactory

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

代码语言:javascript
复制
@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";
              }
        }
      }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-07-16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Sentinel 集成 Feign使用
    • FallbackFactory
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档