首页 > 其他分享 >Sentinel整合Feign对远程调用限流并降级

Sentinel整合Feign对远程调用限流并降级

时间:2022-10-14 14:33:12浏览次数:40  
标签:Feign spring boot springframework 限流 starter Sentinel org cloud

微服务提供者demo-pay

第一步:创建模块demo-pay添加依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--热部署相关-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

第二步:修改application.yml:

server:
  port: 8801
  servlet:
    context-path: /pay
spring:
  application:
    name: nacos-pay
  cloud:
    nacos:
      discovery:
        server-addr: http://localhost:8848  # 配置Nacos地址
management:
  endpoints:
    web:
      exposure:
        include: '*' # 对外暴露出所有的端点

第三步:项目主启动类添加注解

@EnableDiscoveryClient

第四步:提供控制器PayController:

@RestController
public class PayController {

    @GetMapping("/fun/{id}")
    public String fun(@PathVariable("id") Long id) {
        System.out.println("1111111111111111");
        return "支付 id: " + id;
    }
}

微服务消费者demo-sentinel-openfeign

第一步:创建模块demo-sentinel-openfeign并添加依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--热部署相关-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

<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>

<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-datasource-nacos</artifactId>
</dependency>


<!--避免出现警告信息:Spring Cloud LoadBalancer is currently working with the default cache. You can switch to using Caffeine cache, by adding it and org.springframework.cache.caffeine.CaffeineCacheManager to the classpath.-->
<dependency>
    <groupId>com.github.ben-manes.caffeine</groupId>
    <artifactId>caffeine</artifactId>
    <version>3.1.1</version>
</dependency>
<!--  nacos自从2020版本之后不再整合的是Netflix,也就没有ribbon了 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-loadbalancer</artifactId>
    <version>3.0.1</version>
</dependency>

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

第二步:修改application.yml

server:
  port: 6601
  servlet:
    context-path: /sentinelfeign

spring:
  application:
    name: sentinel-feign
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 # 配置nacos地址
    sentinel:
      transport:
        # 配置sentinel dashboard地址
        dashboard: localhost:8080
        # 默认端口8719,假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口
        port: 8719

management:
  endpoints:
    web:
      exposure:
        include: '*' #暴露出所有的端点
# 激活Sentinel对Feign的支持
feign:
  sentinel:
    enabled: true

第三步:在项目主启动类上添加注解:

@EnableFeignClients
@EnableDiscoveryClient

第四步:创建Feign接口:

@Service
//name:指定调用Rest接口所对应的服务名
//path:指定要调用的Rest接口所在的Controller指定的RequestMapping,如果Rest接口所在的Controller没有指定RequestMapping,则不用指定
@FeignClient(name = "nacos-pay", path = "/pay", fallback = PayFeignServiceFallback.class)
public interface PayFeignService {
    @GetMapping("/fun/{id}")
    public String fun(@PathVariable("id") Long id);
}

第五步:创建兜底类:

@Component
public class PayFeignServiceFallback implements PayFeignService {
    @Override
    public String fun(Long id) {
        return "兜底 服务降级: " + id;
    }
}

第六步:创建测试Controller

@RestController
public class DemoCtroller {
    @Resource
    private PayFeignService payFeignService;

    @GetMapping("/fun1")
    public String fun1(Long a) {
        String res = payFeignService.fun(a);
        return "fun1 "+res;
    }
}

测试:

第一步:依次启动软件Nacos和Sentinel,然后再分别启动项目demo-pay和demo-sentinel-openfeign。

第二步:结果: 在这里插入图片描述

添加流控规则: 在这里插入图片描述

在这里插入图片描述

请求消费者中的示例控制器中的api接口: 低频访问: 在这里插入图片描述

高频访问:

在这里插入图片描述

标签:Feign,spring,boot,springframework,限流,starter,Sentinel,org,cloud
From: https://blog.51cto.com/lianghecai/5755864

相关文章

  • 电气防火限流式保护器在可燃性粉尘危险场所的应用
    安科瑞陈盼摘要:文章阐述了可燃性粉尘的特点及可燃性粉尘环境粉尘爆炸的危害性,结合国家现行的电气产品规范的要求,通过一个提取车间内部粉尘爆炸危险区域的电气设计实例,系统阐......
  • springboot整合feign的接口抽离
    前言现在很多微服务框架使用feign来进行服务间的调用,需要在服务端和消费端两边分别对接口和请求返回实体进行编码,维护起来也比较麻烦。那有木有一种可能,只用服务端编......
  • 微服务12:系统服务熔断、限流
    微服务1:微服务及其演进史微服务2:微服务全景架构微服务3:微服务拆分策略微服务4:服务注册与发现微服务5:服务注册与发现(实践篇)微服务6:通信之网关微服务7:通信之RPC微服务8:通信之......
  • 电气防火限流式保护器在员工宿舍和民租房的应用
    陈盼(安科瑞电气股份有限公司,上海,嘉定201801)摘要:随着城市化的发展,我国高层民用建筑如雨后春笋般不断出现在城市之中,高层建筑的火灾问题凸显出来。一旦高层建筑发生火灾事故......
  • Sentinel 兜底方法
    方式一:将兜底方法和控制器请求写在一起@GetMapping("/fun3")@SentinelResource(value="fun3",blockHandler="fun3Handler")publicStringfun3(){return"fun3......
  • 【云原生】Spring Cloud Alibaba 之 Feign 远程调用 实战
    文章目录​​一、什么是远程调用?​​​​⛅远程调用的原理​​​​二、RestTemplate与Feign的区别​​​​三、Feign远程调用实战开发​​​​⏳Feign替代RestTempla......
  • OpenFeign3入门
    简介OpenFeign是SpringCloud家族的一个成员,它最核心的作用是为HTTP形式的RestAPI提供了非常简洁高效的RPC调用方式示例第一步:添加依赖<dependency><gr......
  • Failed to bind properties under 'spring.cloud.sentinel.datasource.dsl.nacos.rule
    错误信息:Failedtobindpropertiesunder'spring.cloud.sentinel.datasource.dsl.nacos.rule-type'tocom.alibaba.cloud.sentinel.datasource.RuleType:Property:......
  • Spring cloud alibaba--Feign微服务调用组件
    Springcloudalibaba--Feign微服务调用组件 https://blog.csdn.net/ZHANGLIZENG/article/details/119058973?spm=1001.2101.3001.6650.3&utm_medium=distribute.pc_rel......
  • sentinel 流控规则持久化
    1\.在项目pom.xml文件中加入sentinel-datasource-nacos依赖<dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-datasource-nacos</artifac......