Spring Cloud Alibaba Sentinel整合Feign

服务提供方

创建一个sentinel-feign-provider服务并注册到Nacos

@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class FeignProviderApplication {

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

    @GetMapping("hello")
    public String hello() {
        return "hello";
    }

}

服务消费方

1. 引入依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- feigns -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!-- sentinel -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
    </dependencies>

2. application.yml

server:
  port: 8060

spring:
  application:
    name: sentinel-feign-consumer
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848

feign:
  sentinel:
    # 开启feign sentinel熔断降级支持
    enabled: true

3. Feign
使用方法和hystrix熔断差不多,需要指定fallback属性,fallback属性的类必须实现feign接口

  • 接口
@FeignClient(value = "sentinel-feign-provider", fallback = TestFeignImpl.class)
public interface TestFeign {

    @GetMapping("hello")
    String hello();
}
  • 实现类
@Component
public class TestFeignImpl implements TestFeign{
    @Override
    public String hello() {
        return "feign fallback";
    }
}

4. controller

@RestController
public class TestController {

    @Autowired
    private TestFeign testFeign;

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("hello-feign")
    public String helloFeign(){
        return testFeign.hello();
    }

    // fallback属性指定降级的方法,默认为当前类的方法
    @SentinelResource(value = "hello-rest", fallback = "helloRestFallback")
    @GetMapping("hello-rest")
    public String helloRest(){
        return restTemplate.getForObject("http://sentinel-feign-provider/hello", String.class);
    }

    public String helloRestFallback(){
        return "rest fallback";
    }
}

5. 启动类

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients //开启feign
public class FeignConsumerApplication {

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

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

测试

启动服务提供方和消费方

curl http://localhost:8060/hello-rest
hello

curl http://localhost:8060/hello-feign
hello

关闭服务提供方

再次curl http://localhost:8060/hello-rest
rest fallback

再次curl http://localhost:8060/hello-feign
feign fallback

如果feign接口指定了fallback,并且@SentinelResource注解中也指定了fallback,优先使用feign接口中指定的


示例代码

作者博客

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring CloudSpring Cloud Alibaba都是基于Spring Framework的开源框架,用于构建分布式系统和微服务架构。它们都提供了一系列的组件和工具来简化微服务开发和管理。 Spring Cloud是一个由Pivotal团队维护的项目,它集成了Netflix开源的一些组件,如Eureka作为注册中心、Ribbon作为负载均衡器、Feign作为服务调用工具等。同时,Spring Cloud还提供了其他功能,如Config Server用于动态管理配置、Gateway用于构建API网关等。 而Spring Cloud Alibaba则是阿里巴巴开源的项目,它在Spring Cloud的基础上进行了扩展和定制,增加了一些阿里巴巴自己的组件和工具。比如,它使用Nacos作为注册中心和配置中心,使用Sentinel作为熔断降级工具。 总的来说,Spring CloudSpring Cloud Alibaba都是用于构建微服务架构的框架,它们的区别在于Spring Cloud集成了Netflix组件,而Spring Cloud Alibaba集成了阿里巴巴的一些组件。根据具体的需求和技术栈选择使用哪个框架可以更好地满足开发和管理微服务的需求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Spring Cloud AlibabaSpring Cloud的区别](https://blog.csdn.net/weixin_43888891/article/details/126653270)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值